parent
8c140ba3fa
commit
359d35fdd2
5 changed files with 25 additions and 21 deletions
16
cmd/root.go
16
cmd/root.go
|
|
@ -2,10 +2,8 @@ package cmd
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
homedir "github.com/mitchellh/go-homedir"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
|
@ -37,7 +35,7 @@ func init() {
|
|||
// Here you will define your flags and configuration settings.
|
||||
// Cobra supports persistent flags, which, if defined here,
|
||||
// will be global for your application.
|
||||
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.go-base.yaml)")
|
||||
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./dev.env")
|
||||
RootCmd.PersistentFlags().Bool("db_debug", false, "log sql to console")
|
||||
viper.BindPFlag("db_debug", RootCmd.PersistentFlags().Lookup("db_debug"))
|
||||
|
||||
|
|
@ -52,15 +50,9 @@ func initConfig() {
|
|||
// Use config file from the flag.
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
// Find home directory.
|
||||
home, err := homedir.Dir()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Search config in home directory with name ".go-base" (without extension).
|
||||
viper.AddConfigPath(home)
|
||||
viper.SetConfigName(".go-base")
|
||||
viper.AddConfigPath(".")
|
||||
viper.SetConfigName("dev")
|
||||
viper.SetConfigType("env")
|
||||
}
|
||||
|
||||
viper.AutomaticEnv() // read in environment variables that match
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue