git.iko.soy/typeable/oauth2-proxy
on v1.0 help sign in
typeable/oauth2-proxy/env_options.go
33 lines 887 B history raw
1package main
3import (
4 "os"
5 "reflect"
6 "strings"
7)
9func LoadOptionsFromEnv(options interface{}, cfg map[string]interface{}) {
10 val := reflect.ValueOf(options).Elem()
11 typ := val.Type()
12 for i := 0; i < typ.NumField(); i++ {
13 // pull out the struct tags:
14 // flag - the name of the command line flag
15 // deprecated - (optional) the name of the deprecated command line flag
16 // cfg - (optional, defaults to underscored flag) the name of the config file option
17 field := typ.Field(i)
18 flagName := field.Tag.Get("flag")
19 envName := field.Tag.Get("env")
20 cfgName := field.Tag.Get("cfg")
21 if cfgName == "" && flagName != "" {
22 cfgName = strings.Replace(flagName, "-", "_", -1)
23 }
24 if envName == "" || cfgName == "" {
25 // resolvable fields must have the `env` and `cfg` struct tag
26 continue
27 }
28 v := os.Getenv(envName)
29 if v != "" {
30 cfg[cfgName] = v
31 }
32 }