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

Commit

Permalink
(GH-36) Fix NPE when puppet version unset in cfg on disk
Browse files Browse the repository at this point in the history
Prior to this commit, if a config value is being set
(e.g.` prm set backend`), but the Puppet version is still unset
and using the viper default value, when the config is being written,
viper still sees an empty `semver.Version` as a non-empty value
and writes it to config as:

```yaml
puppetversion: {}
```

This causes an NPE when the `.String()` method is called on
`prm.RunningConfig.PuppetVersion`.

This commit adds a check when we load the puppet ver config value
from Viper and assigns the default puppet version if the string
returned is empty.
  • Loading branch information
sanfrancrisko committed Dec 2, 2021
1 parent 6d0c9e0 commit eb126bc
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/prm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,13 @@ func GenerateDefaultCfg() {
}

func LoadConfig() error {
// Load Puppet version from config
pupperSemVer, err := semver.NewVersion(viper.GetString(PuppetVerCfgKey))
// If the scenario where any other config value has been set AND the Puppet version is unset, a '{}' is written
// to the config file on disk. This causes issues when attempting to call semver.NewVersion.
puppetVer := viper.GetString(PuppetVerCfgKey)
if puppetVer == "" {
puppetVer = DefaultPuppetVer
}
pupperSemVer, err := semver.NewVersion(puppetVer)
if err != nil {
return fmt.Errorf("could not load '%s' from config '%s': %s", PuppetVerCfgKey, viper.GetViper().ConfigFileUsed(), err)
}
Expand Down

0 comments on commit eb126bc

Please sign in to comment.