From a95c45e8c9203bcd3ba024b08d7abcbd1cc61061 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Mon, 5 Nov 2018 17:53:15 -0500 Subject: [PATCH] add multiple config paths (fixes #60) --- README.md | 7 ++++++- cmd/root.go | 23 +++++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7a1b6b2..09be352 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/cmd/root.go b/cmd/root.go index 0b2fe03..a53eebf 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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())