forked from harness/gitness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
envconfig.go
117 lines (99 loc) · 2.42 KB
/
envconfig.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
package envconfig
import (
"bufio"
"errors"
"os"
"strconv"
"strings"
)
type Env map[string]string
// Get returns the value of the environment variable named by the key.
func (env Env) Get(key string) string {
return env[key]
}
// String returns the string value of the environment variable named by the
// key. If the variable is not present, the default value is returned.
func (env Env) String(key, value string) string {
got, ok := env[key]
if ok {
value = got
}
return value
}
// Bool returns the boolean value of the environment variable named by the key.
// If the variable is not present, the default value is returned.
func (env Env) Bool(name string, value bool) bool {
got, ok := env[name]
if ok {
value, _ = strconv.ParseBool(got)
}
return value
}
// Int returns the integer value of the environment variable named by the key.
// If the variable is not present, the default value is returned.
func (env Env) Int(name string, value int) int {
got, ok := env[name]
if ok {
value, _ = strconv.Atoi(got)
}
return value
}
// Load reads the environment file and reads variables in "key=value" format.
// Then it read the system environment variables. It returns the combined
// results in a key value map.
func Load(filepath string) Env {
var envs = map[string]string{}
// load the environment file
f, err := os.Open(filepath)
if err == nil {
defer f.Close()
r := bufio.NewReader(f)
for {
line, _, err := r.ReadLine()
if err != nil {
break
}
key, val, err := parseln(string(line))
if err != nil {
continue
}
os.Setenv(key, val)
}
}
// load the environment variables
for _, env := range os.Environ() {
key, val, err := parseln(env)
if err != nil {
continue
}
envs[key] = val
}
return Env(envs)
}
// helper function to parse a "key=value" environment variable string.
func parseln(line string) (key string, val string, err error) {
line = removeComments(line)
if len(line) == 0 {
return
}
splits := strings.SplitN(line, "=", 2)
if len(splits) < 2 {
err = errors.New("missing delimiter '='")
return
}
key = strings.Trim(splits[0], " ")
val = strings.Trim(splits[1], ` "'`)
return
}
// helper function to trim comments and whitespace from a string.
func removeComments(s string) (_ string) {
if len(s) == 0 || string(s[0]) == "#" {
return
} else {
index := strings.Index(s, " #")
if index > -1 {
s = strings.TrimSpace(s[0:index])
}
}
return s
}