From b27a7dc11b194490d8b8742fc8a04078a5bdbbd5 Mon Sep 17 00:00:00 2001 From: eeeXun Date: Mon, 23 Jan 2023 16:36:24 +0800 Subject: [PATCH] fix: check if config value is missing If config value is missing, set it with default value --- config.go | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/config.go b/config.go index fab6f6e..ddf3260 100644 --- a/config.go +++ b/config.go @@ -12,6 +12,16 @@ var ( config = viper.New() style = color.NewStyle() hideBelow bool + // default config + defaultConfig = map[string]interface{}{ + "transparent": false, + "theme": "Gruvbox", + "source.language": "English", + "source.borderColor": "red", + "destination.language": "Chinese (Traditional)", + "destination.borderColor": "blue", + "hide_below": false, + } ) // Search XDG_CONFIG_HOME or $HOME/.config @@ -29,18 +39,26 @@ func configInit() { config.AddConfigPath("$HOME/.config/gtt") // Create config file if not exists + // Otherwise check if config value is missing if err := config.ReadInConfig(); err != nil { - config.Set("transparent", false) - config.Set("theme", "Gruvbox") - config.Set("source.language", "English") - config.Set("source.borderColor", "red") - config.Set("destination.language", "Chinese (Traditional)") - config.Set("destination.borderColor", "blue") - config.Set("hide_below", false) + for key, value := range defaultConfig { + config.Set(key, value) + } if _, err = os.Stat(defaultConfigPath); os.IsNotExist(err) { os.MkdirAll(defaultConfigPath, os.ModePerm) } config.SafeWriteConfig() + } else { + missing := false + for key, value := range defaultConfig { + if config.Get(key) == nil { + config.Set(key, value) + missing = true + } + } + if missing { + config.WriteConfig() + } } // setup