Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a no-trim flag for SET operations #481

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 16 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,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 @@ -685,9 +686,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
}
trimChars := " \r\n\t"
if c.LocalFlags.SetNoTrim {
trimChars = ""
}
karimra marked this conversation as resolved.
Show resolved Hide resolved
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, trimChars)), c.Encoding),
)

} else {
Expand All @@ -707,9 +712,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
}
trimChars := " \r\n\t"
if c.LocalFlags.SetNoTrim {
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, trimChars)), c.Encoding),
)

} else {
Expand All @@ -729,9 +738,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
}
trimChars := " \r\n\t"
if c.LocalFlags.SetNoTrim {
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, trimChars)), c.Encoding),
)

} else {
Expand Down