1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
func init() {
viper.SetEnvPrefix("DISPATCH")
viper.AutomaticEnv()
/*
When AutomaticEnv called, Viper will check for an environment variable any
time a viper.Get request is made. It will apply the following rules. It
will check for a environment variable with a name matching the key
uppercased and prefixed with the EnvPrefix if set.
*/
flags := mainCmd.Flags()
flags.Bool("Debug", false, "Turn on debugging.")
viper.BindPFlag("Debug", flags.Lookup("Debug"))
flags.String("addr", "localhost:5002", "Address of the service.")
viper.BindPFlag("addr", flags.Lookup("addr"))
flags.String("smtp-addr", "localhost:25", "Address of the SMTP server.")
viper.BindPFlag("smtp-addr", flags.Lookup("smtp-addr"))
flags.String("smtp-user", "", "User for the SMTP server.")
viper.BindPFlag("smtp-user", flags.Lookup("smtp-user"))
flags.String("smtp-password", "", "Password for the SMTP server.")
viper.BindPFlag("smtp-password", flags.Lookup("smtp-password"))
flags.String("email-from", "noreply@abc.com", "The from email address.")
viper.BindPFlag("email-from", flags.Lookup("email-from"))
// Viper supports reading from yaml, toml and/or json files. Viper can
// search multiple paths. Paths will be searched in the order they are
// provided. Searches stopped once Config File found.
viper.SetConfigName("CommandLineCV") // name of config file (without extension)
viper.AddConfigPath("/tmp")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
println("No config file found. Using built-in defaults.")
}
}
|