From 317a42cec3369c7421a4f24e6c8071746354375c Mon Sep 17 00:00:00 2001 From: Sarah Schulte Date: Mon, 1 Jul 2024 13:29:26 -0700 Subject: [PATCH] add xdg style config path --- cmd/root.go | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 1c7b134..3fb1e3f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -37,6 +37,17 @@ func init() { rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.bootdev.yaml)") } +func readViperConfig(paths []string) error { + for _, path := range paths { + _, err := os.Stat(path) + if err == nil { + viper.SetConfigFile(path) + break + } + } + return viper.ReadInConfig() +} + // initConfig reads in config file and ENV variables if set. func initConfig() { viper.SetDefault("base_url", "https://boot.dev") @@ -54,14 +65,13 @@ func initConfig() { home, err := os.UserHomeDir() cobra.CheckErr(err) - // Search config in home directory with name ".bootdev" (without extension). - viper.AddConfigPath(home) - viper.SetConfigType("yaml") - viper.SetConfigName(".bootdev") - if err := viper.ReadInConfig(); err != nil { - home, err := os.UserHomeDir() - cobra.CheckErr(err) - viper.SafeWriteConfigAs(path.Join(home, ".bootdev.yaml")) + // viper's built in config path thing sucks, let's do it ourselves + defaultPath := path.Join(home, ".bootdev.yaml") + configPaths := []string{} + configPaths = append(configPaths, path.Join(home, ".config", "bootdev", "config.yaml")) + configPaths = append(configPaths, defaultPath) + if err := readViperConfig(configPaths); err != nil { + viper.SafeWriteConfigAs(defaultPath) viper.ReadInConfig() cobra.CheckErr(err) }