forked from kubeflow/arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.go
50 lines (42 loc) · 1000 Bytes
/
loader.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
package env
import (
"bufio"
"os"
"strings"
log "github.com/sirupsen/logrus"
)
// ReadEnvFile returns configs map
func ReadConfigFile(filename string) (configs map[string]string) {
configs = make(map[string]string)
file, err := os.Open(filename)
if err != nil {
return
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err = scanner.Err(); err != nil {
log.Debugf("load config file: %s due to error %v", filename, err)
return
}
for _, line := range lines {
if !canIgnore(line) {
var key, value string
splitString := strings.SplitN(line, "=", 2)
if len(splitString) != 2 {
continue
}
key = strings.Trim(splitString[0], " ")
value = strings.Trim(splitString[1], " ")
configs[key] = value
}
}
return
}
func canIgnore(line string) bool {
trimmedLine := strings.Trim(line, " \n\t")
return len(trimmedLine) == 0 || strings.HasPrefix(trimmedLine, "#")
}