-
Notifications
You must be signed in to change notification settings - Fork 6
/
gofig_reg.go
132 lines (114 loc) · 2.8 KB
/
gofig_reg.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 gofig
import (
"bytes"
"strings"
"unicode"
"github.com/akutz/gofig/types"
"github.com/akutz/goof"
log "github.com/sirupsen/logrus"
)
type configReg struct {
name string
yaml string
keys []types.ConfigRegistrationKey
}
type configRegKey struct {
keyType types.ConfigKeyTypes
defVal interface{}
short string
desc string
keyName string
flagName string
envVarName string
}
// NewRegistration creates a new registration with the given name.
func NewRegistration(name string) types.ConfigRegistration {
return newRegistration(name)
}
func newRegistration(name string) *configReg {
return &configReg{name: name, keys: []types.ConfigRegistrationKey{}}
}
func (r *configReg) Name() string {
return r.name
}
func (r *configReg) Keys() <-chan types.ConfigRegistrationKey {
c := make(chan types.ConfigRegistrationKey)
go func() {
for _, k := range r.keys {
c <- k
}
close(c)
}()
return c
}
func (r *configReg) YAML() string { return r.yaml }
func (r *configReg) SetYAML(y string) { r.yaml = y }
func (r *configReg) Key(
keyType types.ConfigKeyTypes,
short string,
defVal interface{},
description string,
keys ...interface{}) {
lk := len(keys)
if lk == 0 {
panic(goof.New("keys is empty"))
}
rk := &configRegKey{
keyType: keyType,
short: short,
desc: description,
defVal: defVal,
keyName: toString(keys[0]),
}
if keyType == types.SecureString {
secureKey(rk)
}
if lk < 2 {
kp := strings.Split(rk.keyName, ".")
for x, s := range kp {
if x == 0 {
var buff []byte
b := bytes.NewBuffer(buff)
for y, r := range s {
if y == 0 {
b.WriteRune(unicode.ToLower(r))
} else {
b.WriteRune(r)
}
}
kp[x] = b.String()
} else {
kp[x] = strings.Title(s)
}
}
rk.flagName = strings.Join(kp, "")
} else {
rk.flagName = toString(keys[1])
}
if lk < 3 {
kp := strings.Split(rk.keyName, ".")
for x, s := range kp {
kp[x] = strings.ToUpper(s)
}
rk.envVarName = strings.Join(kp, "_")
} else {
rk.envVarName = toString(keys[2])
}
r.keys = append(r.keys, rk)
}
func (k *configRegKey) KeyType() types.ConfigKeyTypes { return k.keyType }
func (k *configRegKey) DefaultValue() interface{} { return k.defVal }
func (k *configRegKey) Short() string { return k.short }
func (k *configRegKey) Description() string { return k.desc }
func (k *configRegKey) KeyName() string { return k.keyName }
func (k *configRegKey) FlagName() string { return k.flagName }
func (k *configRegKey) EnvVarName() string { return k.envVarName }
func secureKey(k *configRegKey) {
secureKeysRWL.Lock()
defer secureKeysRWL.Unlock()
kn := strings.ToLower(k.keyName)
if LogSecureKey {
log.WithField("keyName", kn).Debug("securing key")
}
secureKeys[kn] = k
}