Skip to content

Commit

Permalink
Merge pull request #481 from rao-aneesh/no-trim
Browse files Browse the repository at this point in the history
Added a no-trim flag for SET operations
  • Loading branch information
karimra authored Jul 22, 2024
2 parents c14b441 + a0c3068 commit d443ac0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions pkg/app/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (a *App) InitSetFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&a.Config.LocalFlags.SetRequestVars, "request-vars", "", "", "set request variables file")
cmd.Flags().BoolVarP(&a.Config.LocalFlags.SetDryRun, "dry-run", "", false, "prints the set request without initiating a gRPC connection")
cmd.Flags().StringArrayVarP(&a.Config.LocalFlags.SetRequestProtoFile, "proto-file", "", []string{}, "set request from prototext file")
cmd.Flags().BoolVarP(&a.Config.LocalFlags.SetNoTrim, "no-trim", "", false, "won't trim the input files")
//
cmd.Flags().StringArrayVarP(&a.Config.LocalFlags.SetReplaceCli, "replace-cli", "", []string{}, "a cli command to be sent as a set replace request")
cmd.Flags().StringVarP(&a.Config.LocalFlags.SetReplaceCliFile, "replace-cli-file", "", "", "path to a file containing a list of commands that will be sent as a set replace request")
Expand Down
20 changes: 17 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const (
configName = ".gnmic"
configLogPrefix = "[config] "
envPrefix = "GNMIC"
trimChars = " \r\n\t"
)

var ErrInvalidConfig = errors.New("invalid configuration")
Expand Down Expand Up @@ -157,6 +158,7 @@ type LocalFlags struct {
SetRequestVars string `mapstructure:"set-request-vars,omitempty" json:"set-request-vars,omitempty" yaml:"set-request-vars,omitempty"`
SetRequestProtoFile []string `mapstructure:"set-proto-request-file,omitempty" yaml:"set-proto-request-file,omitempty" json:"set-proto-request-file,omitempty"`
SetDryRun bool `mapstructure:"set-dry-run,omitempty" json:"set-dry-run,omitempty" yaml:"set-dry-run,omitempty"`
SetNoTrim bool `mapstructure:"set-no-trim,omitempty" json:"set-no-trim,omitempty" yaml:"set-no-trim,omitempty"`
SetReplaceCli []string `mapstructure:"set-replace-cli,omitempty" yaml:"set-replace-cli,omitempty" json:"set-replace-cli,omitempty"`
SetReplaceCliFile string `mapstructure:"set-replace-cli-file,omitempty" yaml:"set-replace-cli-file,omitempty" json:"set-replace-cli-file,omitempty"`
SetUpdateCli []string `mapstructure:"set-update-cli,omitempty" yaml:"set-update-cli,omitempty" json:"set-update-cli,omitempty"`
Expand Down Expand Up @@ -686,9 +688,13 @@ func (c *Config) CreateSetRequest(targetName string) ([]*gnmi.SetRequest, error)
c.logger.Printf("error reading data from file '%s': %v", c.LocalFlags.SetUpdateFile[i], err)
return nil, err
}
trim := ""
if !c.LocalFlags.SetNoTrim {
trim = trimChars
}
updOpt = api.Update(
api.Path(strings.TrimSpace(p)),
api.Value(string(bytes.Trim(updateData, " \r\n\t")), c.Encoding),
api.Value(string(bytes.Trim(updateData, trim)), c.Encoding),
)

} else {
Expand All @@ -708,9 +714,13 @@ func (c *Config) CreateSetRequest(targetName string) ([]*gnmi.SetRequest, error)
c.logger.Printf("error reading data from file '%s': %v", c.LocalFlags.SetReplaceFile[i], err)
return nil, err
}
trim := ""
if !c.LocalFlags.SetNoTrim {
trim = trimChars
}
replaceOpt = api.Replace(
api.Path(strings.TrimSpace(p)),
api.Value(string(bytes.Trim(replaceData, " \r\n\t")), c.Encoding),
api.Value(string(bytes.Trim(replaceData, trim)), c.Encoding),
)

} else {
Expand All @@ -730,9 +740,13 @@ func (c *Config) CreateSetRequest(targetName string) ([]*gnmi.SetRequest, error)
c.logger.Printf("error reading data from file '%s': %v", c.LocalFlags.SetUnionReplaceFile[i], err)
return nil, err
}
trim := ""
if !c.LocalFlags.SetNoTrim {
trim = trimChars
}
unionReplaceOpt = api.UnionReplace(
api.Path(strings.TrimSpace(p)),
api.Value(string(bytes.Trim(replaceData, " \r\n\t")), c.Encoding),
api.Value(string(bytes.Trim(replaceData, trim)), c.Encoding),
)

} else {
Expand Down

0 comments on commit d443ac0

Please sign in to comment.