diff --git a/cmd/config/set.go b/cmd/config/set.go index c78d438..64e3798 100644 --- a/cmd/config/set.go +++ b/cmd/config/set.go @@ -16,22 +16,30 @@ var setCmd = &cobra.Command{ Short: "Set configuration", Run: func(cmd *cobra.Command, args []string) { logger := core.GetLogger() - cfg := core.GetConfiguration() + configFile := cfg.ConfigFileUsed() + + userCfg := core.NewConfiguration() + userCfg.SetConfigFile(configFile) + err := userCfg.ReadInConfig() + if err != nil { + logger.Warn("failed to read user configuration file", map[string]interface{}{ + "file": configFile, + }) + + configDir := filepath.Dir(configFile) + utils.PanicOnError("create configuration dir", os.MkdirAll(configDir, 0644)) + } + key := cfg.GetString(core.CfgKeyXConfigSetKey) value := cfg.GetString(core.CfgKeyXConfigSetValue) - cfg.Set(key, value) - cfg.Set("_", nil) - - configFile := cfg.ConfigFileUsed() - configDir := filepath.Dir(configFile) - utils.PanicOnError("create config dir", os.MkdirAll(configDir, 0644)) + userCfg.Set(key, value) - logger.Info("writing configuration", map[string]interface{}{ + logger.Info("writing user configuration", map[string]interface{}{ "file": configFile, }) - utils.PanicOnError("write config", cfg.WriteConfig()) + utils.PanicOnError("write user configuration", userCfg.WriteConfig()) }, } diff --git a/core/config.go b/core/config.go index 9a631d1..2e96ce4 100644 --- a/core/config.go +++ b/core/config.go @@ -16,6 +16,10 @@ func SetConfiguration(cfg Configuration) { globalConfiguration = cfg } +func NewConfiguration() *viper.Viper { + return viper.New() +} + const ( // cmdr CfgKeyCmdrRootDir = "core.root_dir" @@ -77,5 +81,5 @@ const ( ) func init() { - SetConfiguration(viper.GetViper()) + SetConfiguration(NewConfiguration()) }