This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
177 lines (153 loc) · 3.55 KB
/
env.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
package env
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
type (
LookupFunc func(string) (string, bool)
EnvSet struct {
flags map[string]flag
lookup LookupFunc
}
)
func MakeEnvSet(funcs ...LookupFunc) EnvSet {
lookupFuncs := make([]LookupFunc, 0, len(funcs))
for _, fn := range funcs {
if fn == nil {
continue
}
lookupFuncs = append(lookupFuncs, fn)
}
lookup := os.LookupEnv
if len(lookupFuncs) > 0 {
lookup = joinLookupFuncs(lookupFuncs...)
}
return EnvSet{
flags: make(map[string]flag),
lookup: lookup,
}
}
func (env *EnvSet) SetLookupFunc(fns ...LookupFunc) {
env.lookup = joinLookupFuncs(fns...)
}
func (env EnvSet) Parse() error {
errs := make([]error, 0, len(Environment.flags))
for name, flag := range env.flags {
envvar, ok := env.lookup(name)
if !ok && flag.opts.required {
errs = append(errs, fmt.Errorf("%q is required but not found", name))
continue
}
if !ok {
flag.value.Set(flag.opts.fallback)
continue
}
if err := flag.value.Parse(envvar); err != nil {
errs = append(errs, fmt.Errorf("failed to parse %s: %v", name, err))
continue
}
}
return errors.Join(errs...)
}
// MustParse is like Parse but panics if an error occurs
func (env EnvSet) MustParse() {
if err := env.Parse(); err != nil {
panic(err)
}
}
func FlagVar[T any](envset EnvSet, p *T, name string, opts ...Options[T]) {
envset.flags[name] = flag{
value: genericValue[T]{p},
opts: multiOpts[T](opts).toFlagOptions(),
}
}
type flag struct {
value value
opts flagOptions
}
var Environment = EnvSet{
flags: make(map[string]flag),
lookup: os.LookupEnv,
}
func Var[T any](p *T, name string, opts ...Options[T]) {
FlagVar(Environment, p, name, opts...)
}
func Parse() error {
return Environment.Parse()
}
func MustParse() {
if err := Parse(); err != nil {
panic(err)
}
}
// CommandLineArgs returns a lookup function that will search the provided args for flags.
// Since we often want our EnvironmentVariable name declarations to be reusable for command line args
// the lookup is case-insensitive and all underscores are changes to dashes.
// For example, a variable mapped to DATABASE_URL can be found using the --database-url flag when working with CommandLineArgs.
func CommandLineArgs(args ...string) LookupFunc {
if len(args) == 0 {
args = os.Args[1:]
}
var (
m = map[string][]string{}
flag = ""
)
for _, arg := range args {
switch {
case strings.HasPrefix(arg, "-"):
if flag != "" && len(m[flag]) == 0 {
m[flag] = []string{"true"}
}
flag = strings.ToLower(strings.TrimLeft(arg, "-"))
if key, value, ok := strings.Cut(flag, "="); ok {
m[key] = append(m[key], value)
flag = ""
}
case flag == "":
// skip positional args
default:
m[flag] = append(m[flag], arg)
flag = ""
}
}
return func(name string) (string, bool) {
name = strings.ReplaceAll(strings.ToLower(name), "_", "-")
value, ok := m[name]
return strings.Join(value, ","), ok
}
}
type FSLookupOpts struct {
Base string
}
func FileSystem(opts FSLookupOpts) LookupFunc {
if opts.Base == "" {
opts.Base = "."
}
return func(path string) (string, bool) {
if !filepath.IsAbs(path) {
path = filepath.Join(opts.Base, path)
}
data, err := os.ReadFile(path)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
panic(err)
}
return "", false
}
return string(data), true
}
}
func joinLookupFuncs(fns ...LookupFunc) LookupFunc {
return func(key string) (value string, ok bool) {
for _, fn := range fns {
value, ok = fn(key)
if ok {
return
}
}
return
}
}