-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
117 lines (106 loc) · 3.07 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
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
package cfggo
import (
"net/http"
"os"
"strings"
)
// Option is a function that configures a Structure
type Option func(*Structure) error
// withNoop returns an Option that does nothing
func withNoop() Option {
return func(c *Structure) error {
return nil
}
}
// 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, and logs and error if unable to find file
func WithFileConfig(filename string) Option {
return withFileConfig(filename, "WithFileConfig", false)
}
// withFileConfig sets the config source/dest to a filename, and ignores if unable to find file
func WithDefaultFileConfig(filename string) Option {
return withFileConfig(filename, "WithDefaultFileConfig", true)
}
func withFileConfig(filename string, funcName string, defaultConfig bool) Option {
if _, err := os.Stat(filename); err != nil {
if os.IsNotExist(err) {
if !defaultConfig {
Logger.Warn("filename (" + filename + ") does not exist")
}
return withNoop()
}
Logger.Warn("error loading filename (" + filename + "): " + err.Error())
return withNoop()
}
return func(c *Structure) error {
if c.configHandler != nil {
if defaultConfig {
return nil
}
if !c.configHandler.IsDefault() {
return ErrorWrapper(nil, 400, "configHandler is already set, ignoring "+funcName)
}
}
handler := &handlerFile{filename: filename, defaultConfig: defaultConfig}
c.configHandler = handler
return nil
}
}
// WithFileConfigParamName sets the config source/dest to a filename defined in the command line arguments
func WithFileConfigParamName(argName string) Option {
var filename string
for i := 1; i < len(os.Args); i++ {
arg := os.Args[i]
// Handle --config=filename.json format
if strings.HasPrefix(arg, "--"+argName+"=") {
filename = strings.TrimPrefix(arg, "--"+argName+"=")
break
}
// Handle --config filename.json format
if arg == "--"+argName && i+1 < len(os.Args) {
filename = os.Args[i+1]
break
}
}
if filename == "" {
// Logger.Warn("no filename found for argument %s", argName)
return withNoop()
}
return WithFileConfig(filename)
}
// 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
}
}