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)
20 if val.Kind() == reflect.Ptr {
21 typ = val.Elem().Type()
26 for i := 0; i < typ.NumField(); i++ {
27 // pull out the struct tags:
28 // flag - the name of the command line flag
29 // deprecated - (optional) the name of the deprecated command line flag
30 // cfg - (optional, defaults to underscored flag) the name of the config file option
32 fieldV := reflect.Indirect(val).Field(i)
34 if field.Type.Kind() == reflect.Struct && field.Anonymous {
35 cfg.LoadEnvForStruct(fieldV.Interface())
39 flagName := field.Tag.Get("flag")
40 envName := field.Tag.Get("env")
41 cfgName := field.Tag.Get("cfg")
42 if cfgName == "" && flagName != "" {
43 cfgName = strings.ReplaceAll(flagName, "-", "_")
45 if envName == "" || cfgName == "" {
46 // resolvable fields must have the `env` and `cfg` struct tag
49 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