From 23c4515256b91d193103601da3f3b4ed75b73105 Mon Sep 17 00:00:00 2001 From: eeeXun Date: Fri, 30 Jun 2023 20:19:35 +0800 Subject: [PATCH] refactor: set config, themeConfig, keyMapConfig in for loop --- config.go | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/config.go b/config.go index 2c7b990..5d3ec61 100644 --- a/config.go +++ b/config.go @@ -6,15 +6,20 @@ import ( "github.com/eeeXun/gtt/internal/style" "github.com/eeeXun/gtt/internal/translate" - config "github.com/spf13/viper" + "github.com/spf13/viper" +) + +var ( + // Main config + config = viper.New() ) // Search XDG_CONFIG_HOME or $HOME/.config func configInit() { var ( defaultConfigPath string - themeConfig = config.New() - keyMapConfig = config.New() + themeConfig = viper.New() + keyMapConfig = viper.New() defaultKeyMaps = map[string]string{ "exit": "C-c", "translate": "C-j", @@ -54,22 +59,22 @@ func configInit() { ) config.SetConfigName("gtt") - config.SetConfigType("yaml") themeConfig.SetConfigName("theme") - themeConfig.SetConfigType("yaml") keyMapConfig.SetConfigName("keymap") - themeConfig.SetConfigType("yaml") + for _, c := range []*viper.Viper{config, themeConfig, keyMapConfig} { + c.SetConfigType("yaml") + } if len(os.Getenv("XDG_CONFIG_HOME")) > 0 { defaultConfigPath = os.Getenv("XDG_CONFIG_HOME") + "/gtt" - config.AddConfigPath(defaultConfigPath) - themeConfig.AddConfigPath(defaultConfigPath) - keyMapConfig.AddConfigPath(defaultConfigPath) + for _, c := range []*viper.Viper{config, themeConfig, keyMapConfig} { + c.AddConfigPath(defaultConfigPath) + } } else { defaultConfigPath = os.Getenv("HOME") + "/.config/gtt" } - config.AddConfigPath("$HOME/.config/gtt") - themeConfig.AddConfigPath("$HOME/.config/gtt") - keyMapConfig.AddConfigPath("$HOME/.config/gtt") + for _, c := range []*viper.Viper{config, themeConfig, keyMapConfig} { + c.AddConfigPath("$HOME/.config/gtt") + } // Import theme if file exists if err := themeConfig.ReadInConfig(); err == nil {