forked from cristalhq/aconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
176 lines (152 loc) · 4.15 KB
/
example_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
package aconfig_test
import (
"fmt"
"log"
"os"
"github.com/cristalhq/aconfig"
)
type MyConfig struct {
HTTPPort int `default:"1111" usage:"just a number"`
Auth struct {
User string `default:"def-user" usage:"your user"`
Pass string `default:"def-pass" usage:"make it strong"`
}
}
func Example_simpleUsage() {
var cfg MyConfig
loader := aconfig.LoaderFor(&cfg, aconfig.Config{
SkipDefaults: true,
SkipFiles: true,
SkipEnv: true,
SkipFlags: true,
Files: []string{"/var/opt/myapp/config.json"},
EnvPrefix: "APP",
FlagPrefix: "app",
})
if err := loader.Load(); err != nil {
log.Panic(err)
}
fmt.Printf("HTTPPort: %v\n", cfg.HTTPPort)
fmt.Printf("Auth.User: %q\n", cfg.Auth.User)
fmt.Printf("Auth.Pass: %q\n", cfg.Auth.Pass)
// Output:
//
// HTTPPort: 0
// Auth.User: ""
// Auth.Pass: ""
}
func Example_walkFields() {
var cfg MyConfig
loader := aconfig.LoaderFor(&cfg, aconfig.Config{
SkipFiles: true,
SkipEnv: true,
SkipFlags: true,
})
loader.WalkFields(func(f aconfig.Field) bool {
fmt.Printf("%v: %q %q %q %q\n", f.Name(), f.Tag("env"), f.Tag("flag"), f.Tag("default"), f.Tag("usage"))
return true
})
// Output:
// HTTPPort: "HTTP_PORT" "http_port" "1111" "just a number"
// Auth.User: "USER" "user" "def-user" "your user"
// Auth.Pass: "PASS" "pass" "def-pass" "make it strong"
}
// Just load defaults from struct definition.
func Example_defaults() {
var cfg MyConfig
loader := aconfig.LoaderFor(&cfg, aconfig.Config{
SkipFiles: true,
SkipEnv: true,
SkipFlags: true,
})
if err := loader.Load(); err != nil {
log.Panic(err)
}
fmt.Printf("HTTPPort: %v\n", cfg.HTTPPort)
fmt.Printf("Auth.User: %v\n", cfg.Auth.User)
fmt.Printf("Auth.Pass: %v\n", cfg.Auth.Pass)
// Output:
//
// HTTPPort: 1111
// Auth.User: def-user
// Auth.Pass: def-pass
}
// Load defaults from struct defunition and overwrite with a file.
func Example_file() {
var cfg MyConfig
loader := aconfig.LoaderFor(&cfg, aconfig.Config{
SkipEnv: true,
SkipFlags: true,
Files: []string{"testdata/example_config.json"},
})
if err := loader.Load(); err != nil {
log.Panic(err)
}
fmt.Printf("HTTPPort: %v\n", cfg.HTTPPort)
fmt.Printf("Auth.User: %v\n", cfg.Auth.User)
fmt.Printf("Auth.Pass: %v\n", cfg.Auth.Pass)
// Output:
//
// HTTPPort: 2222
// Auth.User: json-user
// Auth.Pass: json-pass
}
// Load defaults from struct definition and overwrite with a file.
// And then overwrite with environment variables.
func Example_env() {
os.Setenv("EXAMPLE_HTTP_PORT", "3333")
os.Setenv("EXAMPLE_AUTH_USER", "env-user")
os.Setenv("EXAMPLE_AUTH_PASS", "env-pass")
defer os.Clearenv()
var cfg MyConfig
loader := aconfig.LoaderFor(&cfg, aconfig.Config{
SkipFlags: true,
EnvPrefix: "EXAMPLE",
Files: []string{"testdata/example_config.json"},
})
if err := loader.Load(); err != nil {
log.Panic(err)
}
fmt.Printf("HTTPPort: %v\n", cfg.HTTPPort)
fmt.Printf("Auth.User: %v\n", cfg.Auth.User)
fmt.Printf("Auth.Pass: %v\n", cfg.Auth.Pass)
// Output:
//
// HTTPPort: 3333
// Auth.User: env-user
// Auth.Pass: env-pass
}
// Load defaults from struct definition and overwrite with a file.
// And then overwrite with environment variables.
// Finally read command line flags.
func Example_flag() {
var cfg MyConfig
loader := aconfig.LoaderFor(&cfg, aconfig.Config{
FlagPrefix: "ex",
Files: []string{"testdata/example_config.json"},
})
flags := loader.Flags() // <- IMPORTANT: use this to define your non-config flags
flags.String("my.other.port", "1234", "debug port")
// IMPORTANT: next statement is made only to hack flag params
// to make test example work
// feel free to remove it completely during copy-paste :)
os.Args = append([]string{}, os.Args[0],
"-ex.http_port=4444",
"-ex.auth.user=flag-user",
"-ex.auth.pass=flag-pass",
)
if err := flags.Parse(os.Args[1:]); err != nil {
log.Panic(err)
}
if err := loader.Load(); err != nil {
log.Panic(err)
}
fmt.Printf("HTTPPort: %v\n", cfg.HTTPPort)
fmt.Printf("Auth.User: %v\n", cfg.Auth.User)
fmt.Printf("Auth.Pass: %v\n", cfg.Auth.Pass)
// Output:
//
// HTTPPort: 4444
// Auth.User: flag-user
// Auth.Pass: flag-pass
}