-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdicts.go
45 lines (40 loc) · 990 Bytes
/
dicts.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
package main
import (
"encoding/json"
"io/ioutil"
"github.com/go-yaml/yaml"
"github.com/sirkon/message"
)
func yamlSource(path string) map[string]string {
res := map[string]string{}
data, err := ioutil.ReadFile(path)
if err != nil {
message.Criticalf("Cannot read `%s`: %s", path, err)
}
if err := yaml.Unmarshal(data, &res); err != nil {
message.Criticalf("Cannot parse `%s` as YAML file: %s", path, err)
}
return res
}
func jsonSource(path string) map[string]string {
res := map[string]string{}
data, err := ioutil.ReadFile(path)
if err != nil {
message.Criticalf("Cannot read `%s`: %s", path, err)
}
if err := json.Unmarshal(data, &res); err != nil {
message.Criticalf("Cannot parse `%s` as JSON file: %s", path, err)
}
return res
}
func getDict(c *runConfig) (res map[string]string) {
yamlDict := c.YAMLDict
jsonDict := c.JSONDict
if len(yamlDict) > 0 {
res = yamlSource(yamlDict)
}
if len(jsonDict) > 0 {
res = jsonSource(jsonDict)
}
return
}