forked from docopt/docopt.go
/
config_file.go
76 lines (68 loc) · 1.94 KB
/
config_file.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
package main
import (
"encoding/json"
"fmt"
"github.com/aviddiviner/docopt-go"
"strings"
)
func loadJSONConfig() map[string]interface{} {
var result map[string]interface{}
jsonData := []byte(`{"--force": true, "--timeout": "10", "--baud": "9600"}`)
json.Unmarshal(jsonData, &result)
return result
}
func loadIniConfig() map[string]interface{} {
iniData := `
[default-arguments]
--force
--baud=19200
<host>=localhost`
// trivial ini parser
// default value for an item is bool: true (for --force)
// otherwise the value is a string
iniParsed := make(map[string]map[string]interface{})
var section string
for _, line := range strings.Split(iniData, "\n") {
if strings.HasPrefix(line, "[") {
section = line
iniParsed[section] = make(map[string]interface{})
} else if section != "" {
kv := strings.SplitN(line, "=", 2)
if len(kv) == 1 {
iniParsed[section][kv[0]] = true
} else if len(kv) == 2 {
iniParsed[section][kv[0]] = kv[1]
}
}
}
return iniParsed["[default-arguments]"]
}
// merge combines two maps.
// truthiness takes priority over falsiness
// mapA takes priority over mapB
func merge(mapA, mapB map[string]interface{}) map[string]interface{} {
result := make(map[string]interface{})
for k, v := range mapA {
result[k] = v
}
for k, v := range mapB {
if _, ok := result[k]; !ok || result[k] == nil || result[k] == false {
result[k] = v
}
}
return result
}
func main() {
usage := `Usage:
config_file tcp [<host>] [--force] [--timeout=<seconds>]
config_file serial <port> [--baud=<rate>] [--timeout=<seconds>]
config_file -h | --help | --version`
jsonConfig := loadJSONConfig()
iniConfig := loadIniConfig()
arguments, _ := docopt.ParseArgs(usage, nil, "0.1.1rc")
// Arguments take priority over INI, INI takes priority over JSON
result := merge(arguments, merge(iniConfig, jsonConfig))
fmt.Println("JSON config: ", jsonConfig)
fmt.Println("INI config: ", iniConfig)
fmt.Println("Result: ", result)
}