|
| 1 | +# envconfig |
| 2 | + |
| 3 | +Borrowed heavily from [Kelsey Hightower's envconfig](https://github.com/kelseyhightower/envconfig) |
| 4 | + |
| 5 | +```Go |
| 6 | +import "git.exlhub.io/exlinc/golang-utils/envconfig" |
| 7 | +``` |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +Set some environment variables: |
| 12 | + |
| 13 | +```Bash |
| 14 | +export MYAPP_DEBUG=false |
| 15 | +export MYAPP_PORT=8080 |
| 16 | +export MYAPP_USER=Kelsey |
| 17 | +export MYAPP_RATE="0.5" |
| 18 | +export MYAPP_TIMEOUT="3m" |
| 19 | +export MYAPP_USERS="rob,ken,robert" |
| 20 | +export MYAPP_COLORCODES="red:1,green:2,blue:3" |
| 21 | +``` |
| 22 | + |
| 23 | +Write some code: |
| 24 | + |
| 25 | +```Go |
| 26 | +package main |
| 27 | + |
| 28 | +import ( |
| 29 | + "fmt" |
| 30 | + "log" |
| 31 | + "time" |
| 32 | + |
| 33 | + "git.exlhub.io/exlinc/golang-utils/envconfig" |
| 34 | +) |
| 35 | + |
| 36 | +type Specification struct { |
| 37 | + Debug bool |
| 38 | + Port int |
| 39 | + User string |
| 40 | + Users []string |
| 41 | + Rate float32 |
| 42 | + Timeout time.Duration |
| 43 | + ColorCodes map[string]int |
| 44 | +} |
| 45 | + |
| 46 | +func main() { |
| 47 | + var s Specification |
| 48 | + err := envconfig.Process("myapp", &s) |
| 49 | + if err != nil { |
| 50 | + log.Fatal(err.Error()) |
| 51 | + } |
| 52 | + format := "Debug: %v\nPort: %d\nUser: %s\nRate: %f\nTimeout: %s\n" |
| 53 | + _, err = fmt.Printf(format, s.Debug, s.Port, s.User, s.Rate, s.Timeout) |
| 54 | + if err != nil { |
| 55 | + log.Fatal(err.Error()) |
| 56 | + } |
| 57 | + |
| 58 | + fmt.Println("Users:") |
| 59 | + for _, u := range s.Users { |
| 60 | + fmt.Printf(" %s\n", u) |
| 61 | + } |
| 62 | + |
| 63 | + fmt.Println("Color codes:") |
| 64 | + for k, v := range s.ColorCodes { |
| 65 | + fmt.Printf(" %s: %d\n", k, v) |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +Results: |
| 71 | + |
| 72 | +```Bash |
| 73 | +Debug: false |
| 74 | +Port: 8080 |
| 75 | +User: Kelsey |
| 76 | +Rate: 0.500000 |
| 77 | +Timeout: 3m0s |
| 78 | +Users: |
| 79 | + rob |
| 80 | + ken |
| 81 | + robert |
| 82 | +Color codes: |
| 83 | + red: 1 |
| 84 | + green: 2 |
| 85 | + blue: 3 |
| 86 | +``` |
| 87 | + |
| 88 | +## Struct Tag Support |
| 89 | + |
| 90 | +Envconfig supports the use of struct tags to specify alternate, default, and required |
| 91 | +environment variables. |
| 92 | + |
| 93 | +For example, consider the following struct: |
| 94 | + |
| 95 | +```Go |
| 96 | +type Specification struct { |
| 97 | + ManualOverride1 string `envconfig:"manual_override_1"` |
| 98 | + DefaultVar string `default:"foobar"` |
| 99 | + RequiredVar string `required:"true"` |
| 100 | + IgnoredVar string `ignored:"true"` |
| 101 | + AutoSplitVar string `split_words:"true"` |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +Envconfig has automatic support for CamelCased struct elements when the |
| 106 | +`split_words:"true"` tag is supplied. Without this tag, `AutoSplitVar` above |
| 107 | +would look for an environment variable called `MYAPP_AUTOSPLITVAR`. With the |
| 108 | +setting applied it will look for `MYAPP_AUTO_SPLIT_VAR`. Note that numbers |
| 109 | +will get globbed into the previous word. If the setting does not do the |
| 110 | +right thing, you may use a manual override. |
| 111 | + |
| 112 | +Envconfig will process value for `ManualOverride1` by populating it with the |
| 113 | +value for `MYAPP_MANUAL_OVERRIDE_1`. Without this struct tag, it would have |
| 114 | +instead looked up `MYAPP_MANUALOVERRIDE1`. With the `split_words:"true"` tag |
| 115 | +it would have looked up `MYAPP_MANUAL_OVERRIDE1`. |
| 116 | + |
| 117 | +```Bash |
| 118 | +export MYAPP_MANUAL_OVERRIDE_1="this will be the value" |
| 119 | + |
| 120 | +# export MYAPP_MANUALOVERRIDE1="and this will not" |
| 121 | +``` |
| 122 | + |
| 123 | +If envconfig can't find an environment variable value for `MYAPP_DEFAULTVAR`, |
| 124 | +it will populate it with "foobar" as a default value. |
| 125 | + |
| 126 | +If envconfig can't find an environment variable value for `MYAPP_REQUIREDVAR`, |
| 127 | +it will return an error when asked to process the struct. |
| 128 | + |
| 129 | +If envconfig can't find an environment variable in the form `PREFIX_MYVAR`, and there |
| 130 | +is a struct tag defined, it will try to populate your variable with an environment |
| 131 | +variable that directly matches the envconfig tag in your struct definition: |
| 132 | + |
| 133 | +```shell |
| 134 | +export SERVICE_HOST=127.0.0.1 |
| 135 | +export MYAPP_DEBUG=true |
| 136 | +``` |
| 137 | +```Go |
| 138 | +type Specification struct { |
| 139 | + ServiceHost string `envconfig:"SERVICE_HOST"` |
| 140 | + Debug bool |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +Envconfig won't process a field with the "ignored" tag set to "true", even if a corresponding |
| 145 | +environment variable is set. |
| 146 | + |
| 147 | +## Supported Struct Field Types |
| 148 | + |
| 149 | +envconfig supports supports these struct field types: |
| 150 | + |
| 151 | + * string |
| 152 | + * int8, int16, int32, int64 |
| 153 | + * bool |
| 154 | + * float32, float64 |
| 155 | + * slices of any supported type |
| 156 | + * maps (keys and values of any supported type) |
| 157 | + * [encoding.TextUnmarshaler](https://golang.org/pkg/encoding/#TextUnmarshaler) |
| 158 | + |
| 159 | +Embedded structs using these fields are also supported. |
| 160 | + |
| 161 | +## Custom Decoders |
| 162 | + |
| 163 | +Any field whose type (or pointer-to-type) implements `envconfig.Decoder` can |
| 164 | +control its own deserialization: |
| 165 | + |
| 166 | +```Bash |
| 167 | +export DNS_SERVER=8.8.8.8 |
| 168 | +``` |
| 169 | + |
| 170 | +```Go |
| 171 | +type IPDecoder net.IP |
| 172 | + |
| 173 | +func (ipd *IPDecoder) Decode(value string) error { |
| 174 | + *ipd = IPDecoder(net.ParseIP(value)) |
| 175 | + return nil |
| 176 | +} |
| 177 | + |
| 178 | +type DNSConfig struct { |
| 179 | + Address IPDecoder `envconfig:"DNS_SERVER"` |
| 180 | +} |
| 181 | +``` |
| 182 | + |
| 183 | +Also, envconfig will use a `Set(string) error` method like from the |
| 184 | +[flag.Value](https://godoc.org/flag#Value) interface if implemented. |
0 commit comments