forked from nspcc-dev/neofs-node
-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
80 lines (62 loc) · 1.53 KB
/
config.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
package configtest
import (
"bufio"
"os"
"strings"
"github.com/TrueCloudLab/frostfs-node/cmd/frostfs-node/config"
)
func fromFile(path string) *config.Config {
var p config.Prm
os.Clearenv() // ENVs have priority over config files, so we do this in tests
return config.New(p,
config.WithConfigFile(path),
)
}
func fromEnvFile(path string) *config.Config {
var p config.Prm
loadEnv(path) // github.com/joho/godotenv can do that as well
return config.New(p)
}
func forEachFile(paths []string, f func(*config.Config)) {
for i := range paths {
f(fromFile(paths[i]))
}
}
// ForEachFileType passes configs read from next files:
// - `<pref>.yaml`;
// - `<pref>.json`.
func ForEachFileType(pref string, f func(*config.Config)) {
forEachFile([]string{
pref + ".yaml",
pref + ".json",
}, f)
}
// ForEnvFileType creates config from `<pref>.env` file.
func ForEnvFileType(pref string, f func(*config.Config)) {
f(fromEnvFile(pref + ".env"))
}
// EmptyConfig returns config without any values and sections.
func EmptyConfig() *config.Config {
var p config.Prm
return config.New(p)
}
// loadEnv reads .env file, parses `X=Y` records and sets OS ENVs.
func loadEnv(path string) {
f, err := os.Open(path)
if err != nil {
panic("can't open .env file")
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
k, v, found := strings.Cut(scanner.Text(), "=")
if !found {
continue
}
v = strings.Trim(v, `"`)
err = os.Setenv(k, v)
if err != nil {
panic("can't set environment variable")
}
}
}