diff options
Diffstat (limited to 'cmd/root.go')
-rw-r--r-- | cmd/root.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..1048bf2 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,46 @@ +package cmd + +import ( + "log" + + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +// LogLevel Flag +var LogLevel = "info" +var LogFormat = "json" +var cfgFile string +var rootCmd = &cobra.Command{ + Use: "demoon", + Short: "demoon", +} + +func init() { + cobra.OnInitialize(initConfig) + rootCmd.PersistentFlags().StringVar(&cfgFile, + "config", "", "config file (default is ./config.yml)") + viper.SetConfigName("config") + viper.AddConfigPath(".") // First try to load the config from the current directory + viper.AddConfigPath("$HOME") // Then try to load it from the HOME directory + viper.AddConfigPath("/etc/demoon/") // As a last resort try to load it from the /etc/ +} + +func initConfig() { + if cfgFile != "" { + // Use config file from the flag. + viper.SetConfigFile(cfgFile) + } + viper.SetEnvPrefix("CFG") + viper.AutomaticEnv() + if err := viper.ReadInConfig(); err != nil { + log.Fatal("Can't read configuration file", "error", err) + } +} + +// Execute the commands +func Execute() { + if err := rootCmd.Execute(); err != nil { + log.Fatal("failed to execute", "error", err) + } +} |