add multiple config paths (fixes #60)

This commit is contained in:
Alex Goodman 2018-11-05 17:53:15 -05:00
parent 951d2c7b71
commit a95c45e8c9
No known key found for this signature in database
GPG Key ID: 743640FAA11698A1
2 changed files with 25 additions and 5 deletions

View File

@ -115,7 +115,7 @@ Key Binding | Description
## Configuration
No configuration is necessary, however, you can create a `~/.dive.yaml` file and override values:
No configuration is necessary, however, you can create a config file and override values:
```yaml
log:
enabled: true
@ -143,3 +143,8 @@ keybinding:
page-up: pgup
page-down: pgdn
```
dive will search for configs in the following locations:
- `~/.dive.yaml`
- `$XDG_CONFIG_HOME/dive.yaml`
- `~/.config/dive.yaml`

View File

@ -39,7 +39,7 @@ func init() {
cobra.OnInitialize(initConfig)
cobra.OnInitialize(initLogging)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.dive.yaml)")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.dive.yaml, ~/.config/dive.yaml, or $XDG_CONFIG_HOME/dive.yaml)")
rootCmd.PersistentFlags().BoolP("version", "v", false, "display version number")
}
@ -57,9 +57,24 @@ func initConfig() {
utils.Exit(1)
}
// Search config in home directory with name ".dive" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".dive")
// allow for:
// ~/.dive.yaml
// $XDG_CONFIG_HOME/dive.yaml
// ~/.config/dive.yaml
hiddenHomeCfg := home + string(os.PathSeparator) + ".dive.yaml"
if _, err := os.Stat(hiddenHomeCfg); os.IsNotExist(err) {
viper.SetConfigName("dive")
xdgHome := os.Getenv("XDG_CONFIG_HOME")
if len(xdgHome) > 0 {
viper.AddConfigPath(xdgHome)
}
viper.AddConfigPath(home + string(os.PathSeparator) + ".config")
} else {
viper.SetConfigFile(hiddenHomeCfg)
}
}
viper.SetDefault("log.level", log.InfoLevel.String())