Skip to content

Commit

Permalink
refactor: split SetValueByPath into two functions (#47)
Browse files Browse the repository at this point in the history
* split `SetValueByPath` into `SetValueByPath` and `WriteConfig`
* introduce new `SetValueByPathWrite` to combine the two
* use new function for config set
  • Loading branch information
xx4h authored Oct 14, 2024
1 parent 1086a37 commit 0310999
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cmd/config_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func newConfigSetCmd(h *pkg.Hctl, _ io.Writer) *cobra.Command {
return compListConfig(toComplete, args, h)
},
Run: func(_ *cobra.Command, args []string) {
if err := h.SetConfigValue(args[0], args[1]); err != nil {
if err := h.SetConfigValueWrite(args[0], args[1]); err != nil {
o.PrintError(err)
}
o.PrintSuccess(args[0])
Expand Down
52 changes: 32 additions & 20 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,38 @@ func (c *Config) GetValueByPath(p string) (string, error) {
}
}

// Same as SetValueByPath, but also writes to config file
func (c *Config) SetValueByPathWrite(p string, val any) error {
if err := c.SetValueByPath(p, val); err != nil {
return err
}
if err := c.WriteConfig(); err != nil {
return err
}
return nil
}

func (c *Config) WriteConfig() error {
// convert current config to byte slice
b, err := json.Marshal(c)
if err != nil {
return err
}
// create new io.Reader from byte slice config
reader := bytes.NewReader(b)

// read in the byte slice config to viper instance
if err := c.Viper.ReadConfig(reader); err != nil {
return err
}

// finally write updated viper instance to file
if err := c.Viper.WriteConfig(); err != nil {
return err
}
return nil
}

func (c *Config) SetValueByPath(p string, val any) error {
// set config element by path p and value v
log.Info().Msgf("Setting `%v` to `%v`", p, val)
Expand Down Expand Up @@ -268,25 +300,5 @@ func (c *Config) SetValueByPath(p string, val any) error {
default:
return fmt.Errorf("unexpected type: %v", v.Type())
}

log.Debug().Caller().Msgf("Config after change: %+v", c)

// convert current config to byte slice
b, err := json.Marshal(c)
if err != nil {
return err
}
// create new io.Reader from byte slice config
reader := bytes.NewReader(b)

// read in the byte slice config to viper instance
if err := c.Viper.ReadConfig(reader); err != nil {
return err
}

// finally write updated viper instance to file
if err := c.Viper.WriteConfig(); err != nil {
return err
}
return nil
}
5 changes: 5 additions & 0 deletions pkg/hctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func (h *Hctl) SetConfigValue(p string, v string) error {
return err
}

func (h *Hctl) SetConfigValueWrite(p string, v string) error {
err := h.cfg.SetValueByPathWrite(p, v)
return err
}

func (h *Hctl) GetConfigOptionsAsPaths() []string {
return h.cfg.GetOptionsAsPaths()
}
Expand Down

0 comments on commit 0310999

Please sign in to comment.