v/GTT
1
0
mirror of https://github.com/eeeXun/GTT.git synced 2025-05-16 07:40:44 -07:00

refactor: set config, themeConfig, keyMapConfig in for loop

This commit is contained in:
eeeXun 2023-06-30 20:19:35 +08:00
parent d5fcc90037
commit 23c4515256

View File

@ -6,15 +6,20 @@ import (
"github.com/eeeXun/gtt/internal/style" "github.com/eeeXun/gtt/internal/style"
"github.com/eeeXun/gtt/internal/translate" "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 // Search XDG_CONFIG_HOME or $HOME/.config
func configInit() { func configInit() {
var ( var (
defaultConfigPath string defaultConfigPath string
themeConfig = config.New() themeConfig = viper.New()
keyMapConfig = config.New() keyMapConfig = viper.New()
defaultKeyMaps = map[string]string{ defaultKeyMaps = map[string]string{
"exit": "C-c", "exit": "C-c",
"translate": "C-j", "translate": "C-j",
@ -54,22 +59,22 @@ func configInit() {
) )
config.SetConfigName("gtt") config.SetConfigName("gtt")
config.SetConfigType("yaml")
themeConfig.SetConfigName("theme") themeConfig.SetConfigName("theme")
themeConfig.SetConfigType("yaml")
keyMapConfig.SetConfigName("keymap") 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 { if len(os.Getenv("XDG_CONFIG_HOME")) > 0 {
defaultConfigPath = os.Getenv("XDG_CONFIG_HOME") + "/gtt" defaultConfigPath = os.Getenv("XDG_CONFIG_HOME") + "/gtt"
config.AddConfigPath(defaultConfigPath) for _, c := range []*viper.Viper{config, themeConfig, keyMapConfig} {
themeConfig.AddConfigPath(defaultConfigPath) c.AddConfigPath(defaultConfigPath)
keyMapConfig.AddConfigPath(defaultConfigPath) }
} else { } else {
defaultConfigPath = os.Getenv("HOME") + "/.config/gtt" defaultConfigPath = os.Getenv("HOME") + "/.config/gtt"
} }
config.AddConfigPath("$HOME/.config/gtt") for _, c := range []*viper.Viper{config, themeConfig, keyMapConfig} {
themeConfig.AddConfigPath("$HOME/.config/gtt") c.AddConfigPath("$HOME/.config/gtt")
keyMapConfig.AddConfigPath("$HOME/.config/gtt") }
// Import theme if file exists // Import theme if file exists
if err := themeConfig.ReadInConfig(); err == nil { if err := themeConfig.ReadInConfig(); err == nil {