-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
146 lines (138 loc) · 4.34 KB
/
Copy pathexample_test.go
File metadata and controls
146 lines (138 loc) · 4.34 KB
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
package config_test
import (
"flag"
"fmt"
"os"
"strings"
"github.com/Bofry/config"
"gopkg.in/yaml.v2"
)
func Example() {
// set env
{
os.Clearenv()
os.Setenv("ENVIRONMENT", "production")
os.Setenv("REDIS_HOST", "127.0.0.3:6379")
os.Setenv("REDIS_PASSWORD", "1234")
os.Setenv("K8S_REDIS_HOST", "demo-kubernetes:6379")
os.Setenv("K8S_REDIS_PASSWORD", "p@ssw0rd")
os.Setenv("K8S_REDIS_DB", "6")
}
// set command line arguments
{
os.Args = []string{"example", "-redis-db", "32"}
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
}
// generate .env
{
os.WriteFile(".env", []byte(
strings.Join([]string{
"REDIS_HOST=127.0.0.1:6379",
"REDIS_DB=29",
"TAG=demo,test",
}, "\n")), 0644)
}
// generate .VERSION
{
os.WriteFile(".VERSION", []byte(
strings.Join([]string{
"v1.0.2",
}, "\n")), 0644)
}
// generate config.yaml
{
os.WriteFile("config.yaml", []byte(
strings.Join([]string{
"redisDB: 3",
"redisPoolSize: 10",
"workspace: demo_test",
}, "\n")), 0644)
}
// generate config.staging.yaml
{
os.WriteFile("config.staging.yaml", []byte(
strings.Join([]string{
"redisDB: 9",
"redisPoolSize: 10",
"workspace: demo_stag",
}, "\n")), 0644)
}
// generate config.production.yaml
{
os.WriteFile("config.production.yaml", []byte(
strings.Join([]string{
"redisDB: 12",
"redisPoolSize: 50",
"workspace: demo_prod",
}, "\n")), 0644)
}
conf := struct {
RedisHost string `env:"REDIS_HOST" yaml:"redisHost" arg:"redis-host;the Redis server address and port"`
RedisPassword string `env:"REDIS_PASSWORD" yaml:"redisPassword" arg:"redis-passowrd;the Redis password"`
RedisDB int `env:"REDIS_DB" yaml:"redisDB" arg:"redis-db;the Redis database number"`
RedisPoolSize int `env:"-" yaml:"redisPoolSize"`
Workspace string `env:"-" yaml:"workspace" arg:"workspace;the data workspace"`
Tags []string `env:"TAG"`
Version string `resource:".VERSION"`
}{}
config.NewConfigurationService(&conf).
LoadDotEnv().
LoadEnvironmentVariables("").
LoadEnvironmentVariables("K8S").
LoadYamlFile("config.yaml").
LoadYamlFile("config.${ENVIRONMENT}.yaml").
LoadCommandArguments().
LoadResource("")
fmt.Printf("RedisHost = %q\n", conf.RedisHost)
fmt.Printf("RedisPassword = %q\n", conf.RedisPassword)
fmt.Printf("RedisDB = %d\n", conf.RedisDB)
fmt.Printf("RedisPoolSize = %d\n", conf.RedisPoolSize)
fmt.Printf("Workspace = %q\n", conf.Workspace)
fmt.Printf("Tags = %q\n", conf.Tags)
fmt.Printf("Version = %q\n", conf.Version)
// Output:
// RedisHost = "demo-kubernetes:6379"
// RedisPassword = "p@ssw0rd"
// RedisDB = 32
// RedisPoolSize = 50
// Workspace = "demo_prod"
// Tags = ["demo" "test"]
// Version = "v1.0.2"
}
func ExampleConfigurationService_LoadFile() {
// prepare config.yaml
{
os.WriteFile("config.yaml", []byte(
strings.Join([]string{
"redisDB: 3",
"redisPoolSize: 10",
"workspace: demo_test",
}, "\n")), 0644)
}
conf := struct {
RedisHost string `env:"REDIS_HOST" yaml:"redisHost" arg:"redis-host;the Redis server address and port"`
RedisPassword string `env:"REDIS_PASSWORD" yaml:"redisPassword" arg:"redis-passowrd;the Redis password"`
RedisDB int `env:"REDIS_DB" yaml:"redisDB" arg:"redis-db;the Redis database number"`
RedisPoolSize int `env:"-" yaml:"redisPoolSize"`
Workspace string `env:"-" yaml:"workspace" arg:"workspace;the data workspace"`
Tags []string `env:"TAG"`
Version string `resource:".VERSION"`
}{}
config.NewConfigurationService(&conf).
LoadFile("config.yaml", yaml.Unmarshal)
fmt.Printf("RedisHost = %q\n", conf.RedisHost)
fmt.Printf("RedisPassword = %q\n", conf.RedisPassword)
fmt.Printf("RedisDB = %d\n", conf.RedisDB)
fmt.Printf("RedisPoolSize = %d\n", conf.RedisPoolSize)
fmt.Printf("Workspace = %q\n", conf.Workspace)
fmt.Printf("Tags = %q\n", conf.Tags)
fmt.Printf("Version = %q\n", conf.Version)
// Output:
// RedisHost = ""
// RedisPassword = ""
// RedisDB = 3
// RedisPoolSize = 10
// Workspace = "demo_test"
// Tags = []
// Version = ""
}