-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvcfg_examples_test.go
269 lines (231 loc) · 6.83 KB
/
envcfg_examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package envcfg_test
import (
"fmt"
"os"
"time"
"github.com/sethpollack/envcfg"
)
func ExampleParse() {
os.Setenv("NAME", "name")
os.Setenv("PORT", "8080")
os.Setenv("RATE", "1.23")
os.Setenv("IS_ENABLED", "true")
os.Setenv("TIMEOUT", "60s")
os.Setenv("REDIS_HOST", "localhost")
os.Setenv("REDIS_PORT", "6379")
os.Setenv("TAGS", "tag1,tag2,tag3")
os.Setenv("PORTS_0", "8080")
os.Setenv("PORTS_1", "9090")
os.Setenv("SERVERS_0_HOST", "localhost1")
os.Setenv("SERVERS_0_PORT", "8080")
os.Setenv("SERVERS_1_HOST", "localhost2")
os.Setenv("SERVERS_1_PORT", "9090")
os.Setenv("LABELS", "key1:value1,key2:value2")
os.Setenv("SETTINGS_KEY1", "1")
os.Setenv("SETTINGS_KEY2", "2")
os.Setenv("DATABASES_PRIMARY_HOST", "localhost1")
os.Setenv("DATABASES_PRIMARY_PORT", "6379")
os.Setenv("DATABASES_SECONDARY_HOST", "localhost2")
os.Setenv("DATABASES_SECONDARY_PORT", "6380")
defer os.Clearenv()
type ServerConfig struct {
Host string
Port int
}
type Config struct {
Name string
Port int
Rate float64
IsEnabled bool
Timeout time.Duration
Redis ServerConfig
Tags []string
Ports []int
Servers []ServerConfig
Labels map[string]string
Settings map[string]int
Databases map[string]ServerConfig
}
cfg := Config{}
if err := envcfg.Parse(&cfg); err != nil {
panic(err)
}
fmt.Printf(`Config:
Name: %s
Port: %d
Rate: %.2f
IsEnabled: %t
Timeout: %s
Redis:
Host: %s
Port: %d
Tags: %v
Ports: %v
Servers: [
{Host: %s, Port: %d},
{Host: %s, Port: %d}
]
Labels: %v
Settings: %v
Databases: {
primary: {Host: %s, Port: %d},
secondary: {Host: %s, Port: %d}
}
`,
cfg.Name, cfg.Port, cfg.Rate, cfg.IsEnabled, cfg.Timeout,
cfg.Redis.Host, cfg.Redis.Port,
cfg.Tags, cfg.Ports,
cfg.Servers[0].Host, cfg.Servers[0].Port,
cfg.Servers[1].Host, cfg.Servers[1].Port,
cfg.Labels, cfg.Settings,
cfg.Databases["primary"].Host, cfg.Databases["primary"].Port,
cfg.Databases["secondary"].Host, cfg.Databases["secondary"].Port,
)
// Output:
// Config:
// Name: name
// Port: 8080
// Rate: 1.23
// IsEnabled: true
// Timeout: 1m0s
// Redis:
// Host: localhost
// Port: 6379
// Tags: [tag1 tag2 tag3]
// Ports: [8080 9090]
// Servers: [
// {Host: localhost1, Port: 8080},
// {Host: localhost2, Port: 9090}
// ]
// Labels: map[key1:value1 key2:value2]
// Settings: map[key1:1 key2:2]
// Databases: {
// primary: {Host: localhost1, Port: 6379},
// secondary: {Host: localhost2, Port: 6380}
// }
}
func ExampleParse_fieldMatching() {
os.Setenv("FIELDNAME", "field name")
os.Setenv("SNAKE_FIELD_NAME", "snake field name")
os.Setenv("TOML_OVERRIDE", "toml override")
os.Setenv("ENV_FIELD_NAME", "env field name")
defer os.Clearenv()
type Config struct {
FieldName string
SnakeFieldName string
TomlFieldName string `toml:"toml_override"`
EnvFieldName string `env:"ENV_FIELD_NAME"`
}
cfg := Config{}
if err := envcfg.Parse(&cfg); err != nil {
panic(err)
}
fmt.Printf("%+v\n", cfg)
// Output: {FieldName:field name SnakeFieldName:snake field name TomlFieldName:toml override EnvFieldName:env field name}
}
func ExampleParse_tags() {
tmpfile, err := os.CreateTemp("", "test.txt")
if err != nil {
panic(err)
}
defer os.Remove(tmpfile.Name())
_, err = tmpfile.WriteString("${FILE_CONTENTS}")
if err != nil {
panic(err)
}
os.Setenv("EXPAND", "${EXPAND_OTHER}")
os.Setenv("EXPAND_OTHER", "expand other")
os.Setenv("FILE_CONTENTS", "file contents")
os.Setenv("FILE", tmpfile.Name())
os.Setenv("EXPAND_FILE", tmpfile.Name())
os.Setenv("IGNORE", "ignore")
os.Setenv("INIT_VALUES_FIELD", "init values")
os.Setenv("INIT_NEVER_FIELD", "init never")
defer os.Clearenv()
type Nested struct {
Field string
}
type Config struct {
Default string `default:"default value"`
ExpandDefault string `expand:"true" default:"${EXPAND_OTHER}"`
Expand string `expand:"true"`
File string `file:"true"`
ExpandFile string `expand:"true" file:"true"`
Ignore string `ignore:"true"`
InitValues *Nested `init:"values"`
InitAlways *Nested `init:"always"`
InitNever *Nested `init:"never"`
}
cfg := Config{}
if err := envcfg.Parse(&cfg); err != nil {
panic(err)
}
fmt.Printf("Default: %v, ExpandDefault: %v, Expand: %v, File: %v, ExpandFile: %v, Ignore: %v, InitValues: %v, InitAlways: %v, InitNever: %v",
cfg.Default, cfg.ExpandDefault, cfg.Expand, cfg.File, cfg.ExpandFile, cfg.Ignore, cfg.InitValues, cfg.InitAlways, cfg.InitNever,
)
// Output: Default: default value, ExpandDefault: expand other, Expand: expand other, File: ${FILE_CONTENTS}, ExpandFile: file contents, Ignore: , InitValues: &{init values}, InitAlways: &{}, InitNever: <nil>
}
func ExampleParse_options() {
tmpfile, err := os.CreateTemp("", "test.txt")
if err != nil {
panic(err)
}
defer os.Remove(tmpfile.Name())
_, err = tmpfile.WriteString("${FILE_CONTENTS}")
if err != nil {
panic(err)
}
os.Setenv("EXPAND", "${EXPAND_OTHER}")
os.Setenv("EXPAND_OTHER", "expand other")
os.Setenv("FILE_CONTENTS", "file contents")
os.Setenv("FILE", tmpfile.Name())
os.Setenv("EXPAND_FILE", tmpfile.Name())
os.Setenv("IGNORE", "ignore")
os.Setenv("INIT_VALUES_FIELD", "init values")
os.Setenv("INIT_NEVER_FIELD", "init never")
defer os.Clearenv()
type Nested struct {
Field string
}
type Config struct {
Default string `env:",default=default value"`
ExpandDefault string `env:",expand,default=${EXPAND_OTHER}"`
Expand string `env:",expand"`
File string `env:",file"`
ExpandFile string `env:",expand,file"`
Ignore string `env:"-"`
InitValues *Nested `env:",init=values"`
InitAlways *Nested `env:",init=always"`
InitNever *Nested `env:",init=never"`
}
cfg := Config{}
err = envcfg.Parse(&cfg)
if err != nil {
panic(err)
}
fmt.Printf("Default: %v, ExpandDefault: %v, Expand: %v, File: %v, ExpandFile: %v, Ignore: %v, InitValues: %v, InitAlways: %v, InitNever: %v",
cfg.Default, cfg.ExpandDefault, cfg.Expand, cfg.File, cfg.ExpandFile, cfg.Ignore, cfg.InitValues, cfg.InitAlways, cfg.InitNever,
)
// Output: Default: default value, ExpandDefault: expand other, Expand: expand other, File: ${FILE_CONTENTS}, ExpandFile: file contents, Ignore: , InitValues: &{init values}, InitAlways: &{}, InitNever: <nil>
}
func ExampleParse_validationNotempty() {
os.Setenv("NOT_EMPTY", "")
defer os.Clearenv()
type Config struct {
NotEmpty string `notempty:"true"`
}
cfg := Config{}
err := envcfg.Parse(&cfg)
fmt.Printf("%+v\n", err)
// Output: environment variable is empty: NOT_EMPTY
}
func ExampleParse_validationRequired() {
defer os.Clearenv()
type Config struct {
Required string `required:"true"`
}
cfg := Config{}
err := envcfg.Parse(&cfg)
fmt.Printf("%+v\n", err)
// Output: required field not found: Required
}