Skip to content

Commit

Permalink
Merge pull request #24 from MrLYC/develop
Browse files Browse the repository at this point in the history
refactor: mange user config only
  • Loading branch information
MrLYC authored May 13, 2023
2 parents 75c9d31 + a1b5624 commit 5fe668e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
26 changes: 17 additions & 9 deletions cmd/config/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},
}

Expand Down
6 changes: 5 additions & 1 deletion core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func SetConfiguration(cfg Configuration) {
globalConfiguration = cfg
}

func NewConfiguration() *viper.Viper {
return viper.New()
}

const (
// cmdr
CfgKeyCmdrRootDir = "core.root_dir"
Expand Down Expand Up @@ -77,5 +81,5 @@ const (
)

func init() {
SetConfiguration(viper.GetViper())
SetConfiguration(NewConfiguration())
}

0 comments on commit 5fe668e

Please sign in to comment.