-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathenvironment.go
62 lines (55 loc) · 1.64 KB
/
environment.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
// (c) 2022 Rick Arnold. Licensed under the BSD license (see LICENSE).
package props
import (
"os"
"strings"
)
// Environment reads properties from the OS environment.
type Environment struct {
// Normalize indicates that key values should be converted to POSIX-style
// environment variable names.
//
// If true, key values passed to Get and GetDefault will be converted to:
// - Uppercase
// - Alphanumeric (as per ASCII)
// - All non-alphanumeric characters replaced with underscore '_'
//
// For example, 'foo.bar.baz' would become 'FOO_BAR_BAZ' and
// '$my-test#val_1' would become '_MY_TEST_VAL_1'.
Normalize bool
}
// Ensure that Environment implements PropertyGetter
var _ PropertyGetter = &Environment{}
// Get retrieves the value of a property from the environment. If the env var
// does not exist, an empty string will be returned. The bool return value
// indicates whether the property was found.
func (e *Environment) Get(key string) (string, bool) {
if e.Normalize {
envKey := strings.Map(normalizeEnv, key)
return os.LookupEnv(envKey)
} else {
return os.LookupEnv(key)
}
}
// GetDefault retrieves the value of a property from the environment. If the
// env var does not exist, then the default value will be returned.
func (e *Environment) GetDefault(key, defVal string) string {
v, ok := e.Get(key)
if !ok {
return defVal
}
return v
}
// normalizeEnv converts a rune into a suitable replacement for an environment
// variable name.
func normalizeEnv(r rune) rune {
if r >= 'a' && r <= 'z' {
return r - 32
} else if r >= 'A' && r <= 'Z' {
return r
} else if r >= '0' && r <= '9' {
return r
} else {
return '_'
}
}