go.undefinedlabs.com/scopeagent@v0.4.2/env/types.go (about) 1 package env 2 3 import ( 4 "fmt" 5 "os" 6 "strconv" 7 "strings" 8 ) 9 10 type ( 11 eVar struct { 12 Key string 13 Raw string 14 IsSet bool 15 } 16 17 BooleanEnvVar struct { 18 eVar 19 Value bool 20 } 21 22 IntEnvVar struct { 23 eVar 24 Value int 25 } 26 27 StringEnvVar struct { 28 eVar 29 Value string 30 } 31 32 SliceEnvVar struct { 33 eVar 34 Value []string 35 } 36 37 MapEnvVar struct { 38 eVar 39 Value map[string]interface{} 40 } 41 ) 42 43 func newEVar(keys ...string) eVar { 44 var e eVar 45 for _, key := range keys { 46 value, ok := os.LookupEnv(key) 47 e = eVar{ 48 Key: key, 49 Raw: value, 50 IsSet: ok, 51 } 52 if ok { 53 break 54 } 55 } 56 return e 57 } 58 59 func newBooleanEnvVar(defaultValue bool, keys ...string) BooleanEnvVar { 60 envVar := BooleanEnvVar{eVar: newEVar(keys...)} 61 if !envVar.IsSet { 62 envVar.Value = defaultValue 63 return envVar 64 } 65 value, err := strconv.ParseBool(envVar.Raw) 66 if err != nil { 67 panic(fmt.Sprintf("unable to parse %s - should be 'true' or 'false'", envVar.Key)) 68 } 69 envVar.Value = value 70 return envVar 71 } 72 73 func newIntEnvVar(defaultValue int, keys ...string) IntEnvVar { 74 envVar := IntEnvVar{eVar: newEVar(keys...)} 75 if !envVar.IsSet { 76 envVar.Value = defaultValue 77 return envVar 78 } 79 value, err := strconv.ParseInt(envVar.Raw, 0, 0) 80 if err != nil { 81 panic(fmt.Sprintf("unable to parse %s - does not seem to be an int", envVar.Key)) 82 } 83 envVar.Value = int(value) 84 return envVar 85 } 86 87 func newStringEnvVar(defaultValue string, keys ...string) StringEnvVar { 88 envVar := StringEnvVar{eVar: newEVar(keys...)} 89 if !envVar.IsSet { 90 envVar.Value = defaultValue 91 return envVar 92 } 93 envVar.Value = envVar.Raw 94 return envVar 95 } 96 97 func newSliceEnvVar(defaultValue []string, keys ...string) SliceEnvVar { 98 envVar := SliceEnvVar{eVar: newEVar(keys...)} 99 if !envVar.IsSet { 100 envVar.Value = defaultValue 101 return envVar 102 } 103 val := strings.Split(envVar.Raw, ",") 104 for i := range val { 105 val[i] = strings.TrimSpace(val[i]) 106 } 107 envVar.Value = val 108 return envVar 109 } 110 111 func newMapEnvVar(defaultValue map[string]interface{}, keys ...string) MapEnvVar { 112 envVar := MapEnvVar{eVar: newEVar(keys...)} 113 if !envVar.IsSet { 114 envVar.Value = defaultValue 115 return envVar 116 } 117 valItems := strings.Split(envVar.Raw, ",") 118 for i := range valItems { 119 valItems[i] = strings.TrimSpace(valItems[i]) 120 } 121 val := map[string]interface{}{} 122 for _, item := range valItems { 123 itemArr := strings.Split(item, "=") 124 if len(itemArr) == 2 { 125 val[itemArr[0]] = os.ExpandEnv(itemArr[1]) 126 } 127 } 128 envVar.Value = val 129 return envVar 130 } 131 132 // For use in if's 133 134 func (e *BooleanEnvVar) Tuple() (bool, bool) { 135 return e.Value, e.IsSet 136 } 137 func (e *IntEnvVar) Tuple() (int, bool) { 138 return e.Value, e.IsSet 139 } 140 func (e *StringEnvVar) Tuple() (string, bool) { 141 return e.Value, e.IsSet 142 } 143 func (e *SliceEnvVar) Tuple() ([]string, bool) { 144 return e.Value, e.IsSet 145 } 146 func (e *MapEnvVar) Tuple() (map[string]interface{}, bool) { 147 return e.Value, e.IsSet 148 }