From fdc5756a7239d3bb8f17cad3cce0a2575ac0f018 Mon Sep 17 00:00:00 2001 From: Aneesh Rao <73494487+rao-aneesh@users.noreply.github.com> Date: Thu, 11 Jul 2024 13:10:39 +0530 Subject: [PATCH 1/3] Update config.go --- pkg/config/config.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index f2f0d359..dd9948ae 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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"` @@ -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 = "" + } 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 { @@ -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 { @@ -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 { From 63ebf9134b3718be07c91637ea0df83eecff6807 Mon Sep 17 00:00:00 2001 From: Aneesh Rao <73494487+rao-aneesh@users.noreply.github.com> Date: Thu, 11 Jul 2024 13:10:59 +0530 Subject: [PATCH 2/3] Update set.go --- pkg/app/set.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/app/set.go b/pkg/app/set.go index ba089d8b..0540dd7b 100644 --- a/pkg/app/set.go +++ b/pkg/app/set.go @@ -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") From a0c306872096047185ccd2af36d1aab9fe4ab5e6 Mon Sep 17 00:00:00 2001 From: Aneesh Rao Date: Wed, 17 Jul 2024 13:30:49 +0530 Subject: [PATCH 3/3] Added a const and inverted the if condition --- pkg/config/config.go | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index dd9948ae..f443ecad 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -42,6 +42,7 @@ const ( configName = ".gnmic" configLogPrefix = "[config] " envPrefix = "GNMIC" + trimChars = " \r\n\t" ) var ErrInvalidConfig = errors.New("invalid configuration") @@ -686,13 +687,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 = "" + trim := "" + if !c.LocalFlags.SetNoTrim { + trim = trimChars } updOpt = api.Update( api.Path(strings.TrimSpace(p)), - api.Value(string(bytes.Trim(updateData, trimChars)), c.Encoding), + api.Value(string(bytes.Trim(updateData, trim)), c.Encoding), ) } else { @@ -712,13 +713,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 = "" + trim := "" + if !c.LocalFlags.SetNoTrim { + trim = trimChars } replaceOpt = api.Replace( api.Path(strings.TrimSpace(p)), - api.Value(string(bytes.Trim(replaceData, trimChars)), c.Encoding), + api.Value(string(bytes.Trim(replaceData, trim)), c.Encoding), ) } else { @@ -738,13 +739,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 = "" + trim := "" + if !c.LocalFlags.SetNoTrim { + trim = trimChars } unionReplaceOpt = api.UnionReplace( api.Path(strings.TrimSpace(p)), - api.Value(string(bytes.Trim(replaceData, trimChars)), c.Encoding), + api.Value(string(bytes.Trim(replaceData, trim)), c.Encoding), ) } else {