-
Notifications
You must be signed in to change notification settings - Fork 12
/
config.go
57 lines (46 loc) · 1.3 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
package config
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
)
// Config for all
type Config struct {
BrokerConfig BrokerConfig `json:"broker_config"`
BackDatabase BackDatabase `json:"back_database"`
CloudCredentials CloudCredentials `json:"cloud_credentials"`
Catalog Catalog `json:"catalog"`
}
// LoadConfig from file
func LoadConfig(configFile string) (config Config, err error) {
if configFile == "" {
return config, errors.New("Must provide a config file")
}
file, err := os.Open(configFile)
if err != nil {
return config, err
}
defer file.Close()
bytes, err := ioutil.ReadAll(file)
if err != nil {
return config, err
}
if err = json.Unmarshal(bytes, &config); err != nil {
return config, err
}
if err = config.BrokerConfig.Validate(); err != nil {
return config, fmt.Errorf("Validating BrokerConfig contents: %s", err)
}
if err = config.BackDatabase.Validate(); err != nil {
return config, fmt.Errorf("Validating BackDatabase contents: %s", err)
}
if err = config.CloudCredentials.Validate(); err != nil {
return config, fmt.Errorf("Validating CloudCredentials contents: %s", err)
}
if err = config.Catalog.Validate(); err != nil {
return config, fmt.Errorf("Validating Catalog contents: %s", err)
}
return config, nil
}