Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <spf@spf13.com>
Expand Down
25 changes: 18 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -57,23 +57,34 @@ func init() {
}

func initConfig() {
viper.AutomaticEnv()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason this was moved above config file loading? I'm not familiar with what this does, but if possible, I'd like to keep this PR scoped to just the config file changes

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call to viper.AutomaticEnv() was originally done before the call to viper.ReadInConfig() and since I moved the latter to within the if/else logic, I moved the viper.AutomaticEnv() higher up to avoid duplicating it. But I can keep it closer to viper.ReadInConfig() by coding it twice.


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())
}
}
}
}