9// EnvOptions holds program options loaded from the process environment
10type EnvOptions map[string]interface{}
12// LoadEnvForStruct loads environment variables for each field in an options
13// struct passed into it.
15// Fields in the options struct must have an `env` and `cfg` tag to be read
16// from the environment
17func (cfg EnvOptions) LoadEnvForStruct(options interface{}) {
18 val := reflect.ValueOf(options).Elem()
20 for i := 0; i < typ.NumField(); i++ {
21 // pull out the struct tags:
22 // flag - the name of the command line flag
23 // deprecated - (optional) the name of the deprecated command line flag
24 // cfg - (optional, defaults to underscored flag) the name of the config file option
26 flagName := field.Tag.Get("flag")
27 envName := field.Tag.Get("env")
28 cfgName := field.Tag.Get("cfg")
29 if cfgName == "" && flagName != "" {
30 cfgName = strings.Replace(flagName, "-", "_", -1)
32 if envName == "" || cfgName == "" {
33 // resolvable fields must have the `env` and `cfg` struct tag
36 v := os.Getenv(envName)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list