Database config parameter
This commit is contained in:
36
cmd/root.go
36
cmd/root.go
@@ -11,8 +11,6 @@ import (
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"go.ntppool.org/common/version"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var cfgFile string
|
||||
@@ -64,41 +62,9 @@ func Execute() {
|
||||
}
|
||||
|
||||
func (cli *CLI) init(cmd *cobra.Command) {
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
// Cobra supports persistent flags, which, if defined here,
|
||||
// will be global for your application.
|
||||
|
||||
cmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.data-api.yaml)")
|
||||
cmd.PersistentFlags().StringVar(&cfgFile, "database-config", "database.yaml", "config file (default is $HOME/.data-api.yaml)")
|
||||
|
||||
// Cobra also supports local flags, which will only run
|
||||
// when this action is called directly.
|
||||
cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
|
||||
// initConfig reads in config file and ENV variables if set.
|
||||
func initConfig() {
|
||||
if cfgFile != "" {
|
||||
// Use config file from the flag.
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
// Find home directory.
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Search config in home directory with name ".data-api" (without extension).
|
||||
viper.AddConfigPath(home)
|
||||
viper.SetConfigName(".data-api")
|
||||
}
|
||||
|
||||
viper.AutomaticEnv() // read in environment variables that match
|
||||
|
||||
// If a config file is found, read it in.
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
fmt.Println("Using config file:", viper.ConfigFileUsed())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user