Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
(GH-15) Add utils.WriteConfig method
Browse files Browse the repository at this point in the history
This commit adds the `utils.WriteConfig` method, which is called
after each `prm set <PARAM>` command is run. This calls
`viper.WriteConfig` to persist the config on disk.

Also some minor refactoring of `cmd/set`
  • Loading branch information
sanfrancrisko committed Nov 30, 2021
1 parent b63290b commit fbd6520
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
2 changes: 2 additions & 0 deletions cmd/set/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/puppetlabs/prm/pkg/prm"
"github.com/puppetlabs/prm/pkg/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -45,4 +46,5 @@ func (sc *SetCommand) setBackendPreRunE(cmd *cobra.Command, args []string) (err

func (sc *SetCommand) setBackendType(cmd *cobra.Command, args []string) error {
return sc.Utils.SetAndWriteConfig(prm.BackendCfgKey, string(SelectedBackend))
utils.WriteConfig()
}
10 changes: 5 additions & 5 deletions cmd/set/puppet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import (

"github.com/Masterminds/semver"
"github.com/puppetlabs/prm/pkg/prm"
"github.com/puppetlabs/prm/pkg/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var PuppetSemVer *semver.Version

func createSetPuppetCommand() *cobra.Command {
tmp := &cobra.Command{
Use: "puppet <VERSION>",
Expand All @@ -22,7 +21,7 @@ func createSetPuppetCommand() *cobra.Command {
return tmp
}

func setPuppetVersion(cmd *cobra.Command, args []string) (err error) {
func setPuppetVersion(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return fmt.Errorf("only a single Puppet version can be set")
}
Expand All @@ -31,12 +30,13 @@ func setPuppetVersion(cmd *cobra.Command, args []string) (err error) {
return fmt.Errorf("please specify a Puppet version after 'set puppet'")
}

PuppetSemVer, err = semver.NewVersion(args[0])
puppetSemVer, err := semver.NewVersion(args[0])
if err != nil {
return fmt.Errorf("'%s' is not a semantic (x.y.z) Puppet version: %s", args[0], err)
}

viper.Set(prm.PuppetVerCfgKey, PuppetSemVer.String)
viper.Set(prm.PuppetVerCfgKey, puppetSemVer.String())
utils.WriteConfig()

return err
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import (

func CreateSetCommand() *cobra.Command {
tmp := &cobra.Command{
// TODO: Add Backend as valid arg
Use: fmt.Sprintf("set %s", prm.PuppetCmdFlag),
Use: fmt.Sprintf("set <%s|%s> value", prm.BackendCmdFlag, prm.PuppetCmdFlag),
Short: "Sets the specified configuration to the specified value",
Long: "Sets the specified configuration to the specified value",
DisableFlagsInUseLine: true,
// TODO: Add Backend as valid arg
ValidArgs: []string{prm.PuppetCmdFlag},
ValidArgs: []string{prm.BackendCmdFlag, prm.PuppetCmdFlag},
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
},
Expand Down
12 changes: 12 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package utils

import (
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)

func WriteConfig() {
if err := viper.WriteConfig(); err != nil {
log.Error().Msgf("could not write config to %s: %s", viper.ConfigFileUsed(), err)
}
}

0 comments on commit fbd6520

Please sign in to comment.