github.com/deso-protocol/core@v1.2.9/cmd/root.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 8 "github.com/spf13/cobra" 9 10 homedir "github.com/mitchellh/go-homedir" 11 "github.com/spf13/viper" 12 ) 13 14 var cfgFile string 15 16 // rootCmd represents the base command when called without any subcommands 17 var rootCmd = &cobra.Command{ 18 Use: "core", 19 Short: "DeSo node", 20 Long: `...`, 21 } 22 23 func Execute() { 24 cobra.CheckErr(rootCmd.Execute()) 25 } 26 27 func init() { 28 cobra.OnInitialize(initConfig) 29 30 rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.deso/core.yaml)") 31 } 32 33 func initConfig() { 34 if cfgFile != "" { 35 viper.SetConfigFile(cfgFile) 36 } else { 37 home, err := homedir.Expand("~/.deso") 38 cobra.CheckErr(err) 39 40 viper.AddConfigPath(home) 41 viper.SetConfigName("core") 42 } 43 44 // Environment variable support 45 viper.AutomaticEnv() 46 viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) 47 48 if err := viper.ReadInConfig(); err == nil { 49 fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) 50 } 51 }