-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfigstruct_test.go
212 lines (167 loc) · 6.65 KB
/
configstruct_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
package configstruct
import (
"flag"
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
type testConfig struct {
Hostname string `env:"CONFIGSTRUCT_HOSTNAME" cli:"hostname" usage:"hostname value"`
Port int `env:"CONFIGSTRUCT_PORT" cli:"port" cliAlt:"p" usage:"listen port"`
Debug bool `env:"CONFIGSTRUCT_DEBUG" cli:"debug" usage:"debug mode"`
FloatValue float64 `env:"CONFIGSTRUCT_FLOAT" cli:"floatValue" usage:"float value"`
}
func TestParse(t *testing.T) {
t.Run("valid cli fields", func(t *testing.T) {
cliArgs := []string{"command", "-hostname=localhost", "-port=8080", "-debug=true", "-floatValue=100.5"}
flagSet := flag.NewFlagSet(cliArgs[0], flag.ExitOnError)
conf := testConfig{}
err := ParseWithFlagSet(flagSet, cliArgs, &conf)
assert.NoError(t, err)
assert.Equal(t, 8080, conf.Port)
assert.Equal(t, "localhost", conf.Hostname)
assert.True(t, conf.Debug)
assert.Equal(t, 100.5, conf.FloatValue)
})
t.Run("alternative cli field", func(t *testing.T) {
cliArgs := []string{"command", "-hostname=localhost", "-p=8080", "-debug=true", "-floatValue=100.5"}
flagSet := flag.NewFlagSet(cliArgs[0], flag.ExitOnError)
conf := testConfig{}
err := ParseWithFlagSet(flagSet, cliArgs, &conf)
assert.NoError(t, err)
assert.Equal(t, 8080, conf.Port)
assert.Equal(t, "localhost", conf.Hostname)
assert.True(t, conf.Debug)
assert.Equal(t, 100.5, conf.FloatValue)
})
t.Run("set using env fields", func(t *testing.T) {
cliArgs := []string{"command", "-hostname=localhost", "-port=8080", "-debug=true", "-floatValue=100.5"}
flagSet := flag.NewFlagSet(cliArgs[0], flag.ExitOnError)
os.Clearenv()
os.Setenv("CONFIGSTRUCT_HOSTNAME", "myhost")
os.Setenv("CONFIGSTRUCT_PORT", "9000")
os.Setenv("CONFIGSTRUCT_DEBUG", "true")
os.Setenv("CONFIGSTRUCT_FLOAT", "2.5")
conf := testConfig{}
err := ParseWithFlagSet(flagSet, cliArgs, &conf, WithPrecedenceEnv())
assert.NoError(t, err)
assert.Equal(t, 9000, conf.Port)
assert.Equal(t, "myhost", conf.Hostname)
assert.True(t, conf.Debug)
assert.Equal(t, 2.5, conf.FloatValue)
})
t.Run("overwrite cli flags with env fields", func(t *testing.T) {
cliArgs := []string{"command", "-hostname=server", "-port=8000", "-debug=true", "-floatValue=100.5"}
flagSet := flag.NewFlagSet(cliArgs[0], flag.ExitOnError)
os.Clearenv()
os.Setenv("CONFIGSTRUCT_HOSTNAME", "myhost")
os.Setenv("CONFIGSTRUCT_PORT", "9000")
conf := testConfig{}
err := ParseWithFlagSet(flagSet, cliArgs, &conf, WithPrecedenceEnv())
assert.NoError(t, err)
assert.Equal(t, 9000, conf.Port)
assert.Equal(t, "myhost", conf.Hostname)
assert.True(t, conf.Debug)
})
t.Run("cli with defaults", func(t *testing.T) {
os.Args = []string{"command", "-hostname", "myhost"}
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
os.Clearenv()
conf := testConfig{
Hostname: "localhost",
Port: 8000,
Debug: true,
FloatValue: 300.1,
}
err := Parse(&conf)
assert.NoError(t, err)
assert.Equal(t, 8000, conf.Port)
assert.Equal(t, "myhost", conf.Hostname)
assert.True(t, conf.Debug)
assert.Equal(t, 300.1, conf.FloatValue)
})
t.Run("not implemented types", func(t *testing.T) {
cliArgs := []string{"command", "-hostname=localhost", "-port=8080", "-debug=true", "-floatValue=100.5"}
flagSet := flag.NewFlagSet(cliArgs[0], flag.ExitOnError)
conf := struct {
Hostname string `env:"CONFIGSTRUCT_HOSTNAME" cli:"hostname" usage:"hostname value"`
Port int64 `env:"CONFIGSTRUCT_PORT" cli:"port" usage:"listen port"`
}{}
err := ParseWithFlagSet(flagSet, cliArgs, &conf)
assert.Error(t, err)
})
t.Run("one required argument", func(t *testing.T) {
cliArgs := []string{"command", "-hostname=localhost", "-port=8080", "start"}
flagSet := flag.NewFlagSet(cliArgs[0], flag.ExitOnError)
conf := struct {
Hostname string `env:"CONFIGSTRUCT_HOSTNAME" cli:"hostname" usage:"hostname value"`
Port int `env:"CONFIGSTRUCT_PORT" cli:"port" usage:"listen port"`
Command string `arg:"1" name:"command" required:"true"`
}{}
err := ParseWithFlagSet(flagSet, cliArgs, &conf)
assert.NoError(t, err)
assert.Equal(t, "start", conf.Command)
})
t.Run("two arguments", func(t *testing.T) {
cliArgs := []string{"command", "-hostname=localhost", "-port=8080", "start", "myfile"}
flagSet := flag.NewFlagSet(cliArgs[0], flag.ExitOnError)
conf := struct {
Hostname string `env:"CONFIGSTRUCT_HOSTNAME" cli:"hostname" usage:"hostname value"`
Port int `env:"CONFIGSTRUCT_PORT" cli:"port" usage:"listen port"`
Command string `arg:"1" name:"command" required:"true"`
Filename string `arg:"2" name:"filename" required:"true"`
}{}
err := ParseWithFlagSet(flagSet, cliArgs, &conf)
assert.NoError(t, err)
assert.Equal(t, "start", conf.Command)
assert.Equal(t, "myfile", conf.Filename)
})
t.Run("arguments with defaults", func(t *testing.T) {
cliArgs := []string{"command", "-hostname=localhost", "-port=8080", "start"}
flagSet := flag.NewFlagSet(cliArgs[0], flag.ExitOnError)
conf := struct {
Hostname string `env:"CONFIGSTRUCT_HOSTNAME" cli:"hostname" usage:"hostname value"`
Port int `env:"CONFIGSTRUCT_PORT" cli:"port" usage:"listen port"`
Command string `arg:"1" name:"command" required:"true"`
Filename string `arg:"2" name:"filename"`
}{
Filename: "myfile",
}
err := ParseWithFlagSet(flagSet, cliArgs, &conf)
assert.NoError(t, err)
assert.Equal(t, "start", conf.Command)
assert.Equal(t, "myfile", conf.Filename)
})
t.Run("required argument missing", func(t *testing.T) {
cliArgs := []string{"command", "-hostname=localhost", "-port=8080"}
flagSet := flag.NewFlagSet(cliArgs[0], flag.ExitOnError)
conf := struct {
Hostname string `env:"CONFIGSTRUCT_HOSTNAME" cli:"hostname" usage:"hostname value"`
Port int `env:"CONFIGSTRUCT_PORT" cli:"port" usage:"listen port"`
Command string `arg:"1" name:"command" required:"true"`
}{}
err := ParseWithFlagSet(flagSet, cliArgs, &conf)
assert.Error(t, err)
})
}
// Example for using `configstruct` with default values.
func ExampleParse() {
// define a struct with tags
type Config struct {
Hostname string `env:"CONFIGSTRUCT_HOSTNAME" cli:"hostname" usage:"hostname value"`
Port int `env:"CONFIGSTRUCT_PORT" cli:"port" usage:"listen port"`
Debug bool `env:"CONFIGSTRUCT_DEBUG" cli:"debug" usage:"debug mode"`
}
// create a variable of the struct type and define defaults if needed
conf := testConfig{
Hostname: "localhost",
Port: 8000,
Debug: true,
}
// now parse values from first cli flags and then env into this var
err := Parse(&conf)
if err != nil {
fmt.Printf("can't parse config %s", err)
}
}