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