-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.go
43 lines (35 loc) · 866 Bytes
/
load.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
package config
import (
"encoding/json"
"os"
)
// LoadDefault reads and unmarshals config file using precedence order from Init()
func LoadDefault() (map[string][]string, error) {
bytes, err := os.ReadFile(ConfigFile)
if err != nil {
return nil, err
}
return Load(bytes)
}
// Load reads and unmarshals given config content
func Load(bytes []byte) (map[string][]string, error) {
var items []string
if err := json.Unmarshal(bytes, &items); err == nil {
return map[string][]string{
"default": items,
}, nil
}
var data map[string][]string
if err := json.Unmarshal(bytes, &data); err != nil {
return nil, err
}
return data, nil
}
// MustLoad is like Load but panics in case of error
func MustLoad(bytes []byte) map[string][]string {
data, err := Load(bytes)
if err != nil {
panic("can't load config: " + err.Error())
}
return data
}