-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.go
More file actions
151 lines (128 loc) · 2.99 KB
/
source.go
File metadata and controls
151 lines (128 loc) · 2.99 KB
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
package config
import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"gopkg.in/yaml.v3"
)
type Source interface {
Load() (*Content, error)
}
var _ Source = (*file)(nil)
// file is a source that loads configuration from a file.
type file struct {
path string
}
func NewFile(path string) *file {
return &file{path: path}
}
func (f *file) Load() (*Content, error) {
p := strings.Split(f.path, ".")
if len(p) <= 1 {
return nil, errors.New("invalid file path")
}
bytes, err := os.ReadFile(f.path)
if err != nil {
return nil, fmt.Errorf("error reading config file: %v", err)
}
ext := p[len(p)-1]
switch ext {
case "yaml", "yml":
return parseYAML(bytes)
case "json":
return parseJSON(bytes)
default:
return nil, errors.New("unsupported file extension")
}
}
func parseYAML(bytes []byte) (*Content, error) {
c := NewContent()
err := yaml.Unmarshal(bytes, &c.content)
if err != nil {
return nil, fmt.Errorf("error parsing config file: %v", err)
}
return c, nil
}
func parseJSON(bytes []byte) (*Content, error) {
c := NewContent()
err := json.Unmarshal(bytes, &c.content)
if err != nil {
return nil, fmt.Errorf("error parsing config file: %v", err)
}
return c, nil
}
var _ Source = (*environment)(nil)
// environment is a source that loads environment variables.
type environment struct {
prefixes []string
replacer *strings.Replacer
normalizeFn func(string) string
}
func NewEnviron(prefixes ...string) *environment {
return &environment{
prefixes: prefixes,
replacer: strings.NewReplacer("_", "."),
normalizeFn: strings.ToLower,
}
}
// WithKeyReplacer maps environment keys to configuration paths.
func (e *environment) WithKeyReplacer(oldnew ...string) *environment {
e.replacer = strings.NewReplacer(oldnew...)
return e
}
// WithKeyNormalizer normalizes environment keys after replacement.
func (e *environment) WithKeyNormalizer(fn func(string) string) *environment {
if fn != nil {
e.normalizeFn = fn
}
return e
}
// Load implements Source.
func (e *environment) Load() (*Content, error) {
c := NewContent()
// handle environment variables.
for _, env := range os.Environ() {
// split the key and value.
kv := strings.SplitN(env, "=", 2)
if len(kv) != 2 {
continue
}
key, value := kv[0], kv[1]
shouldMap := false
if len(e.prefixes) > 0 {
p, ok := matchPrefix(e.prefixes, key)
if !ok || len(p) == len(key) {
continue
}
// trim prefix
key = strings.TrimPrefix(key, p)
key = strings.TrimPrefix(key, "_")
shouldMap = true
}
if len(key) != 0 {
if shouldMap {
if e.replacer != nil {
key = e.replacer.Replace(key)
}
if e.normalizeFn != nil {
key = e.normalizeFn(key)
}
}
err := c.Set(key, value)
if err != nil {
return nil, fmt.Errorf("handle environ variables %s failed: %w", key, err)
}
}
}
return c, nil
}
func matchPrefix(prefixes []string, s string) (string, bool) {
for _, p := range prefixes {
if strings.HasPrefix(s, p) {
return p, true
}
}
return "", false
}