-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
55 lines (45 loc) · 1.26 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
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"github.com/mitchellh/go-homedir"
)
type brizoctlConfig struct {
Endpoint string `json:"endpoint"`
Token string `json:"token"`
}
// ConfigPath contains file path to brizo config file
var ConfigPath = ""
// Config contains the parsed config struct
var Config = &brizoctlConfig{}
// parseConfig will ensure the config file exists, and contains required properties
func parseConfig() error {
// expand home directory
actualPath, err := homedir.Expand(ConfigPath)
if err != nil {
return errors.New("Unable to parse config path: " + ConfigPath)
}
// validate file exists
if _, err = os.Stat(actualPath); os.IsNotExist(err) {
return errors.New("Config file does not exist at " + ConfigPath)
}
// read file to struct
data, err := ioutil.ReadFile(actualPath)
if err != nil {
return errors.New("Unable to read config file: " + ConfigPath)
}
c := new(brizoctlConfig)
err = json.Unmarshal(data, c)
if err != nil {
return errors.New("Unable to read JSON in config file " + ConfigPath)
}
// validate required properties
if c.Endpoint == "" || c.Token == "" {
return errors.New("Config file does not contain required properties (endpoint, token).")
}
// set config
Config = c
return nil
}