-
Notifications
You must be signed in to change notification settings - Fork 1
/
configfilesource.go
66 lines (56 loc) · 1.35 KB
/
configfilesource.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
package conf
import (
"bufio"
"os"
"strings"
)
// confSource is a source 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.
type confSource struct {
m map[string]string
}
func newConfSource(filename string) (*confSource, error) {
m := make(map[string]string)
cf, err := os.Open(filename)
if err != nil {
return nil, err
}
defer cf.Close()
s := bufio.NewScanner(cf)
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.Index(value, " #"); i >= 0 {
value = strings.TrimSpace(value[:i])
}
m[name] = value
}
return &confSource{
m: m,
}, nil
}
// Get returns the stringfied value stored at the specified key in the plain
// config file
func (p *confSource) Get(key []string) (string, bool) {
k := getEnvName(key)
value, ok := p.m[k]
return value, ok
}