-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
132 lines (103 loc) · 2.73 KB
/
config.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
// Package config load configuration values into given struct.
//
// The struct must be passed by reference.
//
// Fields must be exported. Unexported fields will be ignored. They can have the `env` tag which defines the key
// of the value. If no tag provided, the key will be the uppercase full path of the field (all the fields names
// starting the root until current field, joined by underscore).
//
// The `json` tag will be used for loading from JSON.
package config
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
)
const dotEnvFile = ".env"
// Loader provides methods to load configuration values into a struct
type Loader struct {
i interface{}
}
// Load creates a Loader with given struct
func Load(i interface{}) *Loader {
return &Loader{i: i}
}
// Env loads config into struct from environment variables
func (l *Loader) Env() error {
if err := checkNilStruct(l.i); err != nil {
return err
}
return parseIntoStruct(l.i, os.Getenv)
}
// EnvFile loads config into struct from environment variables in one or multiple files (dotenv).
// If no file is passed, the default is ".env".
func (l *Loader) EnvFile(files ...string) error {
if err := checkNilStruct(l.i); err != nil {
return err
}
if len(files) == 0 {
files = append(files, dotEnvFile)
}
vars := make(map[string]string)
for i := 0; i < len(files); i++ {
f, err := os.Open(files[i])
if err != nil {
return fmt.Errorf("config: %s", err)
}
err = parseVars(f, vars)
if err != nil {
if e := f.Close(); e != nil {
return fmt.Errorf("config: %s", e)
}
return fmt.Errorf("config: %s", err)
}
if err = f.Close(); err != nil {
return fmt.Errorf("config: %s", err)
}
}
interpolateVars(vars)
f := func(s string) string {
return vars[s]
}
return parseIntoStruct(l.i, f)
}
// Bytes loads config into struct from byte array
func (l *Loader) Bytes(input []byte) error {
if err := checkNilStruct(l.i); err != nil {
return err
}
return fromBytes(l.i, input)
}
// String loads config into struct from a string
func (l *Loader) String(input string) error {
if err := checkNilStruct(l.i); err != nil {
return err
}
return fromBytes(l.i, []byte(input))
}
// JSON loads config into struct from json
func (l *Loader) JSON(input json.RawMessage) error {
if err := checkNilStruct(l.i); err != nil {
return err
}
return json.Unmarshal(input, l.i)
}
func checkNilStruct(i interface{}) error {
if i == nil {
return errors.New("config: nil struct passed")
}
return nil
}
func fromBytes(i interface{}, input []byte) error {
vars := make(map[string]string)
if err := parseVars(bytes.NewReader(input), vars); err != nil {
return err
}
interpolateVars(vars)
f := func(s string) string {
return vars[s]
}
return parseIntoStruct(i, f)
}