mirror of
https://github.com/spf13/cobra
synced 2025-05-05 21:07:24 +00:00
Changed the default config location from to the system-directory for config
This commit is contained in:
parent
8c6fa02d22
commit
4e773f9787
1 changed files with 27 additions and 17 deletions
44
cobra/cmd/init.go
Normal file → Executable file
44
cobra/cmd/init.go
Normal file → Executable file
|
@ -142,17 +142,27 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
{{if .viper}}
|
|
||||||
homedir "github.com/mitchellh/go-homedir"{{end}}
|
"github.com/Fjolnir-Dvorak/environ"{{if .viper}}
|
||||||
"github.com/spf13/cobra"{{if .viper}}
|
"github.com/spf13/cobra"{{if .viper}}
|
||||||
"github.com/spf13/viper"{{end}}
|
"github.com/spf13/viper"
|
||||||
|
"path/filepath"{{end}}
|
||||||
){{if .viper}}
|
){{if .viper}}
|
||||||
|
|
||||||
var cfgFile string{{end}}
|
const (
|
||||||
|
VendorName = ""
|
||||||
|
ApplicationName = "{{.appName}}"
|
||||||
|
DefaultConfType = "yaml"
|
||||||
|
)
|
||||||
|
{{end}}
|
||||||
|
var ({{if .viper}}
|
||||||
|
cfgFile string{{end}}
|
||||||
|
Environ environ.Environ
|
||||||
|
)
|
||||||
|
|
||||||
// RootCmd represents the base command when called without any subcommands
|
// RootCmd represents the base command when called without any subcommands
|
||||||
var RootCmd = &cobra.Command{
|
var RootCmd = &cobra.Command{
|
||||||
Use: "{{.appName}}",
|
Use: ApplicationName,
|
||||||
Short: "A brief description of your application",
|
Short: "A brief description of your application",
|
||||||
Long: ` + "`" + `A longer description that spans multiple lines and likely contains
|
Long: ` + "`" + `A longer description that spans multiple lines and likely contains
|
||||||
examples and usage of using your application. For example:
|
examples and usage of using your application. For example:
|
||||||
|
@ -174,14 +184,20 @@ func Execute() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() { {{if .viper}}
|
func init() {
|
||||||
|
Environ = environ.New(VendorName, ApplicationName)
|
||||||
|
{{if .viper}}
|
||||||
cobra.OnInitialize(initConfig)
|
cobra.OnInitialize(initConfig)
|
||||||
{{end}}
|
{{end}}
|
||||||
// Here you will define your flags and configuration settings.
|
// 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 }}
|
// will be global for your application.{{ if .viper }}
|
||||||
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ else }}
|
configFile := filepath.Join(Environ.VarConfigLocal(), ApplicationName + "." + DefaultConfType)
|
||||||
// RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ end }}
|
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "",
|
||||||
|
"config file (default is " + configFile + ")"){{ else }}
|
||||||
|
// configFile := filepath.Join(Environ.VarConfigLocal(), ApplicationName + "." + DefaultConfType)
|
||||||
|
// RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "",
|
||||||
|
// "config file (default is " + configFile + ")"){{ end }}
|
||||||
|
|
||||||
// Cobra also supports local flags, which will only run
|
// Cobra also supports local flags, which will only run
|
||||||
// when this action is called directly.
|
// when this action is called directly.
|
||||||
|
@ -194,16 +210,10 @@ func initConfig() {
|
||||||
// Use config file from the flag.
|
// Use config file from the flag.
|
||||||
viper.SetConfigFile(cfgFile)
|
viper.SetConfigFile(cfgFile)
|
||||||
} else {
|
} else {
|
||||||
// Find home directory.
|
|
||||||
home, err := homedir.Dir()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Search config in home directory with name ".{{ .appName }}" (without extension).
|
// Search config in Environ.ConfigLocal() directory with name "ApplicationName" (without extension).
|
||||||
viper.AddConfigPath(home)
|
viper.AddConfigPath(Environ.ConfigLocal())
|
||||||
viper.SetConfigName(".{{ .appName }}")
|
viper.SetConfigName(ApplicationName)
|
||||||
}
|
}
|
||||||
|
|
||||||
viper.AutomaticEnv() // read in environment variables that match
|
viper.AutomaticEnv() // read in environment variables that match
|
||||||
|
|
Loading…
Add table
Reference in a new issue