-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
64 lines (58 loc) · 1.6 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
package cfggo
import (
"net/http"
"os"
)
// Option is a function that configures a Structure
type Option func(*Structure) error
// WithName sets the name of the configuration
func WithName(name string) Option {
return func(c *Structure) error {
c.name = name
return nil
}
}
// WithFileConfig sets the config source/dest to a filename
func WithFileConfig(filename string) Option {
if _, err := os.Stat(filename); os.IsNotExist(err) {
Logger.Warn("filename %s does not exist", filename)
}
return func(c *Structure) error {
if c.configHandler != nil {
return ErrorWrapper(nil, 400, "configHandler is already set, ignoring WithFileConfig")
}
handler := &handlerFile{filename: filename}
c.configHandler = handler
return nil
}
}
// WithHTTPConfig sets the config source/dest to a filename
func WithHTTPConfig(httpLoader *http.Request, httpSaver *http.Request) Option {
if httpLoader == nil && httpSaver == nil {
return func(c *Structure) error {
return ErrorWrapper(nil, 400, "httpLoader and httpSaver cannot both be nil")
}
}
return func(c *Structure) error {
if c.configHandler != nil {
return ErrorWrapper(nil, 400, "configHandler is already set, ignoring WithHTTPConfig")
}
handler := &handlerHTTP{}
if httpLoader != nil {
handler.source = *httpLoader
}
if httpSaver != nil {
handler.dest = *httpSaver
}
c.configHandler = handler
return nil
}
}
// WithSkipEnvironment skips loading from environment variables
func WithSkipEnvironment() Option {
return func(c *Structure) error {
// Logger.Debug("Skipping environment variables")
c.skipEnv = true
return nil
}
}