mirror of
https://github.com/spf13/cobra
synced 2024-11-17 19:27:09 +00:00
Restore the use of cobra.OnInitialize in cobra executable
Without cobra.OnInitialize, cobra does not see the --config command-line option and always tries to load $HOME/.cobra.yaml instead.
This commit is contained in:
parent
d4c40bbacf
commit
cb9cf94690
1 changed files with 2 additions and 2 deletions
|
@ -40,7 +40,7 @@ func Execute() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
initViper()
|
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.yaml)")
|
||||||
rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "author name for copyright attribution")
|
rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "author name for copyright attribution")
|
||||||
|
@ -55,7 +55,7 @@ func init() {
|
||||||
rootCmd.AddCommand(initCmd)
|
rootCmd.AddCommand(initCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func initViper() {
|
func initConfig() {
|
||||||
if cfgFile != "" {
|
if cfgFile != "" {
|
||||||
// Use config file from the flag.
|
// Use config file from the flag.
|
||||||
viper.SetConfigFile(cfgFile)
|
viper.SetConfigFile(cfgFile)
|
||||||
|
|
Loading…
Reference in a new issue