/
env.go
136 lines (115 loc) · 3.33 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
package env
import (
"fmt"
"os"
"regexp"
"sort"
"strings"
"sync"
"github.com/sirupsen/logrus"
)
// SYSL_PLANTUML
// URL of PlantUML server. Sysl depends upon
// [PlantUML](http://plantuml.com/) for diagram generation.
// SYSL_SSH_KEYS
// SSH private key file path and passphrase for git/github credentials + domains to use them on
// e.g. SYSL_SSH_KEYS=github.com:keypatha:1234,gitlab.com:keypathb:5678
// SYSL_TOKENS
// Tokens to use for git/github credentials + domains to use them on
// e.g. SYSL_TOKENS=github.com:1234,gitlab.com:567
//
// The following development-only vars will only be reported by sysl env if
// their values differ from their defaults...
//
// SYSL_DEV_RENEST_FLATTENED_TYPES
// off: Don't renest flattened types.
// retain: Renest flattened types, but retain the original.
// move: Renest flattened types, removing the original.
type Var string
//nolint:revive,stylecheck
const (
SYSL_MODULES Var = "SYSL_MODULES"
SYSL_PLANTUML Var = "SYSL_PLANTUML"
SYSL_SSH_KEYS Var = "SYSL_SSH_KEYS"
SYSL_TOKENS Var = "SYSL_TOKENS" //nolint:gosec
// Development-only vars
SYSL_DEV_RENEST_FLATTENED_TYPES Var = "SYSL_DEV_RENEST_FLATTENED_TYPES"
SYSL_DEV_UPDATE_GOLDEN_TESTS Var = "SYSL_DEV_UPDATE_GOLDEN_TESTS"
)
var entries = map[Var]*entry{
SYSL_MODULES: newEntry("on", "off"),
SYSL_PLANTUML: newEntry("https://plantuml.com/plantuml"),
SYSL_SSH_KEYS: newEntry(""),
SYSL_TOKENS: newEntry(""),
SYSL_DEV_RENEST_FLATTENED_TYPES: newEntry("off", "retain", "move"),
SYSL_DEV_UPDATE_GOLDEN_TESTS: newEntry("off", "on"),
}
type entry struct {
defaultValue string
validValues []string
validationRE *regexp.Regexp
once sync.Once
value *string
}
func newEntry(defaultValue string, otherValidValues ...string) *entry {
var validValues []string
var validValuesRE *regexp.Regexp
if len(otherValidValues) > 0 {
validValues = append(make([]string, 0, 1+len(otherValidValues)), defaultValue)
validValues = append(validValues, otherValidValues...)
validValuesRE = regexp.MustCompile(
fmt.Sprintf("^(%s)$", regexp.MustCompile(strings.Join(validValues, "|"))))
}
return &entry{
defaultValue: defaultValue,
validValues: validValues,
validationRE: validValuesRE,
}
}
var Vars = func() VarSlice {
ret := make(VarSlice, 0, len(entries))
for e := range entries {
ret = append(ret, e)
}
sort.Sort(ret)
return ret
}()
func (e Var) Default() string {
return entries[e].defaultValue
}
func (e Var) Name() string {
return string(e)
}
func (e Var) On() bool {
return e.Value() == "on"
}
func (e Var) Value() string {
entry := entries[e]
entry.once.Do(func() {
var value string
if value = os.Getenv(string(e)); value == "" {
value = entry.defaultValue
}
if entry.validationRE != nil && !entry.validationRE.MatchString(value) {
expectation := ""
if len(entry.validValues) > 0 {
expectation = fmt.Sprintf(" (expecting one of: %s)", strings.Join(entry.validValues, ", "))
}
logrus.Errorf("invalid %s=%q%s; assuming the default: %s",
e, value, expectation, entry.defaultValue)
value = entry.defaultValue
}
entry.value = &value
})
return *entry.value
}
type VarSlice []Var
func (v VarSlice) Len() int {
return len(v)
}
func (v VarSlice) Less(i, j int) bool {
return v[i] < v[j]
}
func (v VarSlice) Swap(i, j int) {
v[i], v[j] = v[j], v[i]
}