forked from peterbourgon/ff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
190 lines (162 loc) · 4.91 KB
/
parse.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package ff
import (
"bufio"
"flag"
"io"
"os"
"strings"
"github.com/pkg/errors"
)
// Parse the flags in the flag set from the provided (presumably commandline)
// args. Additional options may be provided to parse from a config file and/or
// environment variables in that priority order.
func Parse(fs *flag.FlagSet, args []string, options ...Option) error {
var c Context
for _, option := range options {
option(&c)
}
if err := fs.Parse(args); err != nil {
return errors.Wrap(err, "error parsing commandline args")
}
provided := map[string]bool{}
fs.Visit(func(f *flag.Flag) {
provided[f.Name] = true
})
if c.configFile == "" && c.configFileFlagName != "" {
fs.VisitAll(func(f *flag.Flag) {
if f.Name == c.configFileFlagName {
c.configFile = f.Value.String()
}
})
}
if c.configFile != "" && c.configFileParser != nil {
f, err := os.Open(c.configFile)
if err != nil {
return err
}
defer f.Close()
c.configFileParser(f, func(name, value string) error {
if fs.Lookup(name) == nil {
return errors.Errorf("config file flag %q not defined in flag set", name)
}
if provided[name] {
return nil // commandline args take precedence
}
if err := fs.Set(name, value); err != nil {
return errors.Wrapf(err, "error setting flag %q from config file", name)
}
return nil
})
}
fs.Visit(func(f *flag.Flag) {
provided[f.Name] = true
})
if c.envVarPrefix != "" {
var errs []string
fs.VisitAll(func(f *flag.Flag) {
if provided[f.Name] {
return // commandline args and config file take precedence
}
var key string
{
key = strings.ToUpper(f.Name)
key = envVarReplacer.Replace(key)
key = strings.ToUpper(c.envVarPrefix) + "_" + key
}
if value := os.Getenv(key); value != "" {
for _, individual := range strings.Split(value, ",") {
if err := fs.Set(f.Name, strings.TrimSpace(individual)); err != nil {
errs = append(errs, errors.Wrapf(err, "error setting flag %q from env var %q", f.Name, key).Error())
}
}
}
})
if len(errs) > 0 {
return errors.Errorf("error parsing env vars: %s", strings.Join(errs, "; "))
}
}
return nil
}
// Context contains private fields used during parsing.
type Context struct {
configFile string
configFileFlagName string
configFileParser ConfigFileParser
envVarPrefix string
}
// Option controls some aspect of parse behavior.
type Option func(*Context)
// WithConfigFile tells parse to read the provided filename as a config file.
// Requires WithConfigFileParser, and overrides WithConfigFileFlag.
func WithConfigFile(filename string) Option {
return func(c *Context) {
c.configFile = filename
}
}
// WithConfigFileFlag tells parse to treat the flag with the given name as a
// config file. Requires WithConfigFileParser, and is overridden by WithConfigFile.
func WithConfigFileFlag(flagname string) Option {
return func(c *Context) {
c.configFileFlagName = flagname
}
}
// WithConfigFileParser tells parse how to interpret the config file provided via
// WithConfigFile or WithConfigFileFlag.
func WithConfigFileParser(p ConfigFileParser) Option {
return func(c *Context) {
c.configFileParser = p
}
}
// WithEnvVarPrefix tells parse to look in the environment for variables with
// the given prefix. Flag names are converted to environment variables by
// capitalizing them, and replacing separator characters like periods or hyphens
// with underscores. Additionally, if the environment variable's value contains
// commas, each comma-delimited token is treated as a separate instance of the
// associated flag name.
func WithEnvVarPrefix(prefix string) Option {
return func(c *Context) {
c.envVarPrefix = prefix
}
}
// ConfigFileParser interprets the config file represented by the reader
// and calls the set function for each parsed flag pair.
type ConfigFileParser func(r io.Reader, set func(name, value string) error) error
// PlainParser is a parser for config files in an extremely simple format. Each
// line is tokenized as a single key/value pair. The first whitespace-delimited
// token in the line is interpreted as the flag name, and all remaining tokens
// are interpreted as the value. Any leading hyphens on the flag name are
// ignored.
func PlainParser(r io.Reader, set func(name, value string) error) error {
s := bufio.NewScanner(r)
for s.Scan() {
line := strings.TrimSpace(s.Text())
if line == "" {
continue // skip empties
}
if line[0] == '#' {
continue // skip comments
}
var (
name string
value string
index = strings.IndexRune(line, ' ')
)
if index < 0 {
name, value = line, "true" // boolean option
} else {
name, value = line[:index], strings.TrimSpace(line[index:])
}
if i := strings.IndexRune(value, '#'); i >= 0 {
value = strings.TrimSpace(value[:i])
}
if err := set(name, value); err != nil {
return err
}
}
return nil
}
var envVarReplacer = strings.NewReplacer(
"-", "_",
".", "_",
"/", "_",
)