-
Notifications
You must be signed in to change notification settings - Fork 40
/
conf.go
138 lines (120 loc) · 2.61 KB
/
conf.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
package conf
import (
"fmt"
"os"
"strings"
"time"
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/v2"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
var konf = koanf.New(".")
var logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
var defaultConfigPaths = []string{
"config.local.toml",
"config.toml",
"~/tork/config.toml",
"/etc/tork/config.toml",
}
func LoadConfig() error {
var paths []string
userConfig := os.Getenv("TORK_CONFIG")
if userConfig != "" {
paths = []string{userConfig}
} else {
paths = defaultConfigPaths
}
// load configs from file paths
var loaded bool
for _, f := range paths {
err := konf.Load(file.Provider(f), toml.Parser())
if errors.Is(err, os.ErrNotExist) {
continue
}
if err != nil {
return errors.Wrapf(err, "error loading config from %s", f)
}
logger.Info().Msgf("Config loaded from %s", f)
loaded = true
break
}
if !loaded && userConfig != "" {
return errors.Errorf(fmt.Sprintf("could not find config file in: %s", userConfig))
}
// load configs from env vars
if err := konf.Load(env.Provider("TORK_", ".", func(s string) string {
return strings.Replace(strings.ToLower(
strings.TrimPrefix(s, "TORK_")), "_", ".", -1)
}), nil); err != nil {
return errors.Wrapf(err, "error loading config from env")
}
return nil
}
func IntMap(key string) map[string]int {
return konf.IntMap(key)
}
func Unmarshal(key string, o any) error {
return konf.Unmarshal(key, o)
}
func BoolMap(key string) map[string]bool {
return konf.BoolMap(key)
}
func StringMap(key string) map[string]string {
return konf.StringMap(key)
}
func Strings(key string) []string {
strs := konf.Strings(key)
if len(strs) > 0 {
return strs
}
str := konf.String(key)
if str == "" {
return []string{}
}
return strings.Split(str, ",")
}
func DurationDefault(key string, dv time.Duration) time.Duration {
v := konf.Get(key)
if v == nil {
return dv
}
return konf.Duration(key)
}
func StringsDefault(key string, dv []string) []string {
v := konf.Get(key)
if v == nil {
return dv
}
return Strings(key)
}
func IntDefault(key string, dv int) int {
v := konf.Get(key)
if v == nil {
return dv
}
return konf.Int(key)
}
func String(key string) string {
return konf.String(key)
}
func StringDefault(key, dv string) string {
v := String(key)
if v != "" {
return v
}
return dv
}
func Bool(key string) bool {
return konf.Bool(key)
}
func BoolDefault(key string, dv bool) bool {
v := konf.Get(key)
if v == nil {
return dv
}
return Bool(key)
}