-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapi.go
98 lines (85 loc) · 2.77 KB
/
api.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
package config
import (
"bufio"
"bytes"
"fmt"
"os"
)
const (
structTagKey = "config"
structDelim = "__"
sliceDelim = " "
)
// Builder contains the current configuration state.
type Builder struct {
structDelim, sliceDelim string
configMap map[string]string
failedFields []string
}
// To accepts a struct pointer, and populates it with the current config state.
// Supported fields:
// * all int, uint, float variants
// * bool, struct, string
// * time.Duration
// * \*url.URL
// * slice of any of the above, except for []struct{}
// It returns an error if:
// * struct contains unsupported fields (pointers, maps, slice of structs, channels, arrays, funcs, interfaces, complex)
// * there were errors doing file i/o
// It panics if:
// * target is not a struct pointer
func (c *Builder) To(target interface{}) error {
return c.decode(target, "")
}
// Sub behaves the same as To, however it maps configuration to the struct starting at the given prefix.
func (c *Builder) Sub(target interface{}, prefix string) error {
return c.decode(target, prefix+c.structDelim)
}
// From returns a new Builder, populated with the values from file.
func From(file string) *Builder {
return newBuilder().From(file)
}
// FromOptional returns a new Builder, populated with the values from file if it exists.
func FromOptional(file string) *Builder {
return newBuilder().FromOptional(file)
}
// From merges new values from file into the current config state, returning the Builder.
func (c *Builder) From(file string) *Builder {
return c.appendFile(file, true)
}
// FromOptional merges new values from file (if it exists) into the current config state, returning the Builder.
func (c *Builder) FromOptional(file string) *Builder {
return c.appendFile(file, false)
}
func (c *Builder) appendFile(file string, includeErr bool) *Builder {
content, err := os.ReadFile(file)
if includeErr && err != nil {
c.failedFields = append(c.failedFields, fmt.Sprintf("file[%v]", file))
}
scanner := bufio.NewScanner(bytes.NewReader(content))
var ss []string
for scanner.Scan() {
ss = append(ss, scanner.Text())
}
if includeErr && scanner.Err() != nil {
c.failedFields = append(c.failedFields, fmt.Sprintf("file[%v]", file))
}
mergeMaps(c.configMap, stringsToMap(ss))
return c
}
// FromEnv returns a new Builder, populated with environment variables
func FromEnv() *Builder {
return newBuilder().FromEnv()
}
// FromEnv merges new values from the environment into the current config state, returning the Builder.
func (c *Builder) FromEnv() *Builder {
mergeMaps(c.configMap, stringsToMap(os.Environ()))
return c
}
func newBuilder() *Builder {
return &Builder{
configMap: make(map[string]string),
structDelim: structDelim,
sliceDelim: sliceDelim,
}
}