-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.go
69 lines (63 loc) · 1.12 KB
/
init.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
package conf
import (
"bufio"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
)
var configMap map[string]string
/**
*
*/
func init() {
configMap = make(map[string]string)
load()
}
func Get(key string) (value string) {
value, _ = configMap[key]
return
}
func GetOrDefault(key, defaultValue string) string {
value, ok := configMap[key]
if ok {
return value
} else {
return defaultValue
}
}
/**
*
*/
func load() {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
fmt.Errorf("Error get current dir: %s", err.Error())
}
f := fmt.Sprintf("%s/.env.conf", dir)
file, err := os.Open(f)
if err != nil {
file, err = os.Open("/tmp/.env.conf")
if err != nil {
log.Fatalf("Error load .env.conf: %s\n", err.Error())
}
}
reader := bufio.NewReader(file)
var key, value string
for {
line, err := reader.ReadString('\n')
if err != nil {
if err != io.EOF {
log.Fatalf("Error read config file: %s", err.Error())
}
if line == "" {
return
}
}
ss := strings.Split(line, "=")
key, value = strings.Trim(ss[0], " "), strings.Trim(ss[1], " \n")
configMap[key] = value
}
}