From 8c43afa675ce63985665ac288aeaebe6699dbc17 Mon Sep 17 00:00:00 2001 From: Philipp Rosenkranz Date: Mon, 1 Apr 2019 13:35:03 +0200 Subject: [PATCH] config: merge new config keys into existing one before writing --- cmd-config.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cmd-config.go b/cmd-config.go index 045935c..6aa3c6b 100644 --- a/cmd-config.go +++ b/cmd-config.go @@ -27,6 +27,10 @@ var configCommand = &cobra.Command{ firstArgument := args[0] configKey := firstArgument containsEquals := strings.Index(firstArgument, "=") > 0 + err := viper.ReadInConfig() + if err != nil { + _, _ = fmt.Fprintln(os.Stderr, err) + } if containsEquals { // write to config configKeyValue := strings.Split(firstArgument, "=") @@ -37,16 +41,16 @@ var configCommand = &cobra.Command{ if err != nil { _, _ = fmt.Fprintln(os.Stderr, err) } + err = viper.MergeInConfig() + if err != nil { + _, _ = fmt.Fprintln(os.Stderr, err) + } err = viper.WriteConfigAs(filepath.Join(dir, configFileName)) if err != nil { _, _ = fmt.Fprintln(os.Stderr, err) } return } - err := viper.ReadInConfig() - if err != nil { - _, _ = fmt.Fprintln(os.Stderr, err) - } configValue := viper.Get(configKey) switch v := configValue.(type) { case string: