Private
Public Access
1
0

Database config parameter

This commit is contained in:
2023-07-08 22:15:03 -07:00
parent 6583a36514
commit 1963aae0b7
6 changed files with 32 additions and 484 deletions

View File

@@ -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())
}
}