From abd44aa0f095964c573716a5da6e2c3bee79ee53 Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Tue, 22 Feb 2022 20:53:46 -0500 Subject: [PATCH] Rename configuration file to .cobra-cli.yaml With the renaming of the generator to cobra-cli, this commit also renames the configuration file as ".cobra-cli.yaml". For backwards- compatibility, if "$HOME/.cobra-cli.yaml" can't be read, we fallback to "$HOME/.cobra.yaml". Co-authored-by: Jordan Liggitt Signed-off-by: Marc Khouzam --- README.md | 6 ++++-- cmd/root.go | 25 ++++++++++++++++++------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f7aa064..84719d5 100644 --- a/README.md +++ b/README.md @@ -128,9 +128,11 @@ Have fun! The Cobra generator will be easier to use if you provide a simple configuration file which will help you eliminate providing a bunch of repeated information in -flags over and over. +flags over and over. If not specified through the `--config` flag, the configuration +file should be placed at `$HOME/.cobra-cli.yaml` (for backwards-compatibility, +`$HOME/.cobra.yaml` will also be accepted). -An example ~/.cobra.yaml file: +An example ~/.cobra-cli.yaml file: ```yaml author: Steve Francia diff --git a/cmd/root.go b/cmd/root.go index b44c2e7..6c16105 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -43,7 +43,7 @@ func Execute() error { func init() { cobra.OnInitialize(initConfig) - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)") + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra-cli.yaml)") rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "author name for copyright attribution") rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "name of license for the project") rootCmd.PersistentFlags().Bool("viper", false, "use Viper for configuration") @@ -57,23 +57,34 @@ func init() { } func initConfig() { + viper.AutomaticEnv() + if cfgFile != "" { // Use config file from the flag. viper.SetConfigFile(cfgFile) + + if err := viper.ReadInConfig(); err == nil { + fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) + } } else { // Find home directory. home, err := os.UserHomeDir() cobra.CheckErr(err) - // Search config in home directory with name ".cobra" (without extension). + // Search config in home directory with name ".cobra-cli" or ".cobra" (without extension). viper.AddConfigPath(home) viper.SetConfigType("yaml") - viper.SetConfigName(".cobra") - } + viper.SetConfigName(".cobra-cli") - viper.AutomaticEnv() + if err := viper.ReadInConfig(); err == nil { + fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) + } else if _, ok := err.(viper.ConfigFileNotFoundError); ok { + // For backwards compatibility, fallback to .cobra.yaml (without extension) + viper.SetConfigName(".cobra") - if err := viper.ReadInConfig(); err == nil { - fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) + if err := viper.ReadInConfig(); err == nil { + fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) + } + } } }