diff --git a/cobra/cmd/add.go b/cobra/cmd/add.go index 81ef8171..988568ea 100644 --- a/cobra/cmd/add.go +++ b/cobra/cmd/add.go @@ -25,7 +25,6 @@ import ( func init() { addCmd.Flags().StringVarP(&parentName, "parent", "p", "RootCmd", "name of parent command for this command") - RootCmd.AddCommand(addCmd) } var parentName string diff --git a/cobra/cmd/init.go b/cobra/cmd/init.go index 84fc68b1..9a6f93d2 100644 --- a/cobra/cmd/init.go +++ b/cobra/cmd/init.go @@ -23,10 +23,6 @@ import ( "github.com/spf13/viper" ) -func init() { - RootCmd.AddCommand(initCmd) -} - var initCmd = &cobra.Command{ Use: "init [name]", Aliases: []string{"initialize", "initialise", "create"}, @@ -132,6 +128,7 @@ import ( "fmt" "os" + homedir "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" {{if .viper}} "github.com/spf13/viper"{{end}} ) @@ -158,7 +155,7 @@ to quickly create a Cobra application.` + "`" + `, func Execute() { if err := RootCmd.Execute(); err != nil { fmt.Println(err) - os.Exit(-1) + os.Exit(1) } } @@ -166,7 +163,7 @@ func init() { {{if .viper}} cobra.OnInitialize(initConfig){{end}} // Here you will define your flags and configuration settings. - // Cobra supports Persistent Flags, which, if defined here, + // Cobra supports persistent flags, which, if defined here, // will be global for your application.{{ if .viper }} RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ else }} // RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ end }} @@ -178,14 +175,23 @@ func init() { // initConfig reads in config file and ENV variables if set. func initConfig() { - if cfgFile != "" { // enable ability to specify config file via flag + if cfgFile != "" { + // Use config file from the flag. viper.SetConfigFile(cfgFile) } else { - viper.SetConfigName(".{{ .appName }}") // name of config file (without extension) - viper.AddConfigPath(os.Getenv("HOME")) // adding home directory as first search path + // Find home directory. + home, err := homedir.Dir() + if err != nil { + fmt.Println(home) + os.Exit(1) + } + + // Search config in home directory with name ".cobra" (without extension). + viper.AddConfigPath(home) + viper.SetConfigName(".cobra") } - viper.AutomaticEnv() // read in environment variables that match + viper.AutomaticEnv() // read in environment variables that match // If a config file is found, read it in. if err := viper.ReadInConfig(); err == nil { diff --git a/cobra/cmd/root.go b/cobra/cmd/root.go index dbfe7be1..967eecc9 100644 --- a/cobra/cmd/root.go +++ b/cobra/cmd/root.go @@ -17,6 +17,7 @@ import ( "fmt" "os" + homedir "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -34,12 +35,13 @@ to quickly create a Cobra application.`, func Execute() { if err := RootCmd.Execute(); err != nil { fmt.Println(err) - os.Exit(-1) + os.Exit(1) } } func init() { initViper() + RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)") RootCmd.PersistentFlags().StringVarP(&projectBase, "projectbase", "b", "", "base project directory, e.g. github.com/spf13/") RootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "Author name for copyright attribution") @@ -50,13 +52,26 @@ func init() { viper.BindPFlag("useViper", RootCmd.PersistentFlags().Lookup("viper")) viper.SetDefault("author", "NAME HERE ") viper.SetDefault("license", "apache") + + RootCmd.AddCommand(initCmd) + RootCmd.AddCommand(addCmd) + } func initViper() { - if cfgFile != "" { // enable ability to specify config file via flag + if cfgFile != "" { + // Use config file from the flag. viper.SetConfigFile(cfgFile) } else { - viper.AddConfigPath(os.Getenv("HOME")) + // Find home directory. + home, err := homedir.Dir() + if err != nil { + fmt.Println(home) + os.Exit(1) + } + + // Search config in home directory with name ".cobra" (without extension). + viper.AddConfigPath(home) viper.SetConfigName(".cobra") }