mirror of
https://github.com/spf13/cobra
synced 2024-11-24 22:57:12 +00:00
parent
1dd5ff2e11
commit
d404c53e88
2 changed files with 10 additions and 6 deletions
|
@ -205,14 +205,16 @@ func init() {
|
||||||
{{ if .viper }}
|
{{ if .viper }}
|
||||||
// initConfig reads in config file and ENV variables if set.
|
// initConfig reads in config file and ENV variables if set.
|
||||||
func initConfig() {
|
func initConfig() {
|
||||||
if cfgFile != "" { // enable ability to specify config file via flag
|
|
||||||
viper.SetConfigFile(cfgFile)
|
|
||||||
}
|
|
||||||
|
|
||||||
viper.SetConfigName(".{{ .appName }}") // name of config file (without extension)
|
viper.SetConfigName(".{{ .appName }}") // name of config file (without extension)
|
||||||
viper.AddConfigPath("$HOME") // adding home directory as first search path
|
viper.AddConfigPath("$HOME") // adding home directory as first search path
|
||||||
viper.AutomaticEnv() // read in environment variables that match
|
viper.AutomaticEnv() // read in environment variables that match
|
||||||
|
|
||||||
|
// Checking for the cfgFile after setting default paths.
|
||||||
|
if cfgFile != "" { // enable ability to specify config file via flag
|
||||||
|
viper.SetConfigFile(cfgFile)
|
||||||
|
}
|
||||||
|
|
||||||
// If a config file is found, read it in.
|
// If a config file is found, read it in.
|
||||||
if err := viper.ReadInConfig(); err == nil {
|
if err := viper.ReadInConfig(); err == nil {
|
||||||
fmt.Println("Using config file:", viper.ConfigFileUsed())
|
fmt.Println("Using config file:", viper.ConfigFileUsed())
|
||||||
|
|
|
@ -57,14 +57,16 @@ func init() {
|
||||||
|
|
||||||
// Read in config file and ENV variables if set.
|
// Read in config file and ENV variables if set.
|
||||||
func initConfig() {
|
func initConfig() {
|
||||||
if cfgFile != "" { // enable ability to specify config file via flag
|
|
||||||
viper.SetConfigFile(cfgFile)
|
|
||||||
}
|
|
||||||
|
|
||||||
viper.SetConfigName(".cobra") // name of config file (without extension)
|
viper.SetConfigName(".cobra") // name of config file (without extension)
|
||||||
viper.AddConfigPath("$HOME") // adding home directory as first search path
|
viper.AddConfigPath("$HOME") // adding home directory as first search path
|
||||||
viper.AutomaticEnv() // read in environment variables that match
|
viper.AutomaticEnv() // read in environment variables that match
|
||||||
|
|
||||||
|
// Checking for the cfgFile after setting default paths.
|
||||||
|
if cfgFile != "" { // enable ability to specify config file via flag
|
||||||
|
viper.SetConfigFile(cfgFile)
|
||||||
|
}
|
||||||
|
|
||||||
// If a config file is found, read it in.
|
// If a config file is found, read it in.
|
||||||
if err := viper.ReadInConfig(); err == nil {
|
if err := viper.ReadInConfig(); err == nil {
|
||||||
fmt.Println("Using config file:", viper.ConfigFileUsed())
|
fmt.Println("Using config file:", viper.ConfigFileUsed())
|
||||||
|
|
Loading…
Reference in a new issue