-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
71 lines (58 loc) · 1.44 KB
/
config.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
package config
import (
"bufio"
"log"
"os"
"path/filepath"
"strings"
)
// our loaded configurations
var configs = make(map[string]string)
// load configuration for one or more files,
// providing file names is optional, if not provided it will
// look for at file called [executable_name].ini
func Load(requiredCondfigs []string, fileNames ...string) {
//fmt.Println(executable)
if len(fileNames) == 0 {
executable := filepath.Base(os.Args[0])
fileNames = []string{executable + ".ini"}
}
for _, fileName := range fileNames {
parse(fileName, requiredCondfigs)
}
}
func parse(fileName string, requiredCondfigs []string) {
file, err := os.Open(fileName) // from "current" dir?
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
text := strings.Trim(scanner.Text(), " ")
if strings.HasPrefix(text, ";") || strings.HasPrefix(text, "[") {
continue
}
qIndex := strings.Index(text, "=")
if qIndex > -1 {
key := strings.Trim(string(text[:qIndex]), " ")
value := strings.Trim(string(text[qIndex:]), "= ")
if key != "" && value != "" {
configs[key] = value
}
}
}
if err := scanner.Err(); err != nil {
log.Print(err)
}
// check for *required* configs!
for _, conf := range requiredCondfigs {
_, exists := configs[conf]
if !exists {
log.Fatal("Missing **required** configuration: " + conf)
}
}
}
func Get(key string) string {
return configs[key]
}