mirror of
https://github.com/eeeXun/GTT.git
synced 2025-05-18 08:40:35 -07:00
fix: check if config value is missing
If config value is missing, set it with default value
This commit is contained in:
parent
9c7056ed8b
commit
b27a7dc11b
32
config.go
32
config.go
@ -12,6 +12,16 @@ var (
|
|||||||
config = viper.New()
|
config = viper.New()
|
||||||
style = color.NewStyle()
|
style = color.NewStyle()
|
||||||
hideBelow bool
|
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
|
// Search XDG_CONFIG_HOME or $HOME/.config
|
||||||
@ -29,18 +39,26 @@ func configInit() {
|
|||||||
config.AddConfigPath("$HOME/.config/gtt")
|
config.AddConfigPath("$HOME/.config/gtt")
|
||||||
|
|
||||||
// Create config file if not exists
|
// Create config file if not exists
|
||||||
|
// Otherwise check if config value is missing
|
||||||
if err := config.ReadInConfig(); err != nil {
|
if err := config.ReadInConfig(); err != nil {
|
||||||
config.Set("transparent", false)
|
for key, value := range defaultConfig {
|
||||||
config.Set("theme", "Gruvbox")
|
config.Set(key, value)
|
||||||
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)
|
|
||||||
if _, err = os.Stat(defaultConfigPath); os.IsNotExist(err) {
|
if _, err = os.Stat(defaultConfigPath); os.IsNotExist(err) {
|
||||||
os.MkdirAll(defaultConfigPath, os.ModePerm)
|
os.MkdirAll(defaultConfigPath, os.ModePerm)
|
||||||
}
|
}
|
||||||
config.SafeWriteConfig()
|
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
|
// setup
|
||||||
|
Loading…
x
Reference in New Issue
Block a user