-
Notifications
You must be signed in to change notification settings - Fork 0
/
confi.go
104 lines (83 loc) · 2.49 KB
/
confi.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
package confi
import (
"context"
"os"
"strconv"
"strings"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
func Get[T any](ctx context.Context, appName string) (*T, *Schema, error) {
replacer := strings.NewReplacer(`-`, `_`, `.`, `_`)
provider := &DefaultSourceProvider{
EnvPrefix: replacer.Replace(appName) + "_",
Env: os.Environ(),
Args: os.Args[1:],
Stdin: os.Stdin,
}
return FromProvider[T](ctx, provider)
}
func FromProvider[T any](ctx context.Context, provider SourceProvider) (*T, *Schema, error) {
sources, err := provider.GetSources(ctx)
if err != nil {
return nil, nil, errors.Wrap(err, "get sources")
}
var config T
schema, err := GenerateSchema(config)
if err != nil {
return nil, nil, errors.Wrapf(err, "generate schema")
}
for _, source := range sources {
values, err := source.GetValues(ctx)
if err != nil {
return nil, nil, errors.Wrapf(err, "get values from %s", source)
}
data, err := yaml.Marshal(SpecifyType(values))
if err != nil {
return nil, nil, errors.Wrapf(err, "marshal values from %s to yaml", source)
}
if err := yaml.Unmarshal(data, &config); err != nil {
return nil, nil, errors.Wrapf(err, "unmarshal values from %s from yaml", source)
}
}
if err := schema.ApplyDefaults(&config); err != nil {
return nil, nil, errors.Wrap(err, "apply defaults")
}
return &config, schema, nil
}
func testFloat(source string, target float64) bool {
tokens := strings.Split(source, ".")
var prec int
if len(tokens) > 1 {
prec = len(tokens[1])
}
return strconv.FormatFloat(target, 'f', prec, 64) == source
}
func SpecifyType(source any) any {
switch source := source.(type) {
case string:
if target, err := strconv.ParseBool(source); err == nil && strconv.FormatBool(target) == source {
return true
} else if target, err := strconv.ParseUint(source, 10, 64); err == nil && strconv.FormatUint(target, 10) == source {
return target
} else if target, err := strconv.ParseInt(source, 10, 64); err == nil && strconv.FormatInt(target, 10) == source {
return target
} else if target, err := strconv.ParseFloat(source, 64); err == nil && testFloat(source, target) {
return target
}
return source
case []any:
target := make([]any, len(source))
for i, value := range source {
target[i] = SpecifyType(value)
}
return target
case map[string]any:
target := make(map[any]any, len(source))
for key, value := range source {
target[SpecifyType(key)] = SpecifyType(value)
}
return target
}
return source
}