-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
75 lines (62 loc) · 1.38 KB
/
options.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
package envconfig
import "strings"
const (
DefaultFileSuffix = "_FILE"
)
type (
options struct {
prefix string
isLoadFromFile bool
defaultFileSuffix string
trimSpaces bool
}
Option func(o *options)
)
func defaultOptions() *options {
return &options{
prefix: "",
isLoadFromFile: true,
defaultFileSuffix: DefaultFileSuffix,
trimSpaces: true,
}
}
func (o *options) apply(opts ...Option) *options {
for _, opt := range opts {
opt(o)
}
return o
}
func (o *options) copy() *options {
return &options{
prefix: o.prefix,
isLoadFromFile: o.isLoadFromFile,
defaultFileSuffix: o.defaultFileSuffix,
trimSpaces: o.trimSpaces,
}
}
func WithPrefix(prefix string) Option {
return func(o *options) {
o.prefix = strings.ToUpper(prefix)
}
}
// WithoutDefaultLoadingFromFiles disables loading values from files pointed by *_FILE vars.
func WithoutDefaultLoadingFromFiles() Option {
return func(o *options) {
o.isLoadFromFile = false
}
}
func WithDefaultFileSuffix(suffix string) Option {
suffix = strings.TrimSpace(suffix)
if suffix == "" {
suffix = DefaultFileSuffix
}
return func(o *options) {
o.defaultFileSuffix = suffix
}
}
// WithoutTrimSpaces disables spaces trimming around value (see strings.TrimSpace).
func WithoutTrimSpaces() Option {
return func(o *options) {
o.trimSpaces = false
}
}