-
Notifications
You must be signed in to change notification settings - Fork 9
/
config.go
73 lines (58 loc) · 1.49 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"github.com/cloud-gov/s3-broker/broker"
"gopkg.in/yaml.v2"
)
type Config struct {
LogLevel string `yaml:"log_level"`
Username string `yaml:"username"`
Password string `yaml:"password"`
Environment string `yaml:"environment"`
S3Config broker.Config `yaml:"s3_config"`
CFConfig *CFConfig `yaml:"cf_config"`
}
type CFConfig struct {
ApiAddress string `yaml:"api_url"`
ClientID string `yaml:"client_id"`
ClientSecret string `yaml:"client_secret"`
}
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 = yaml.Unmarshal(bytes, &config); err != nil {
return config, err
}
if err = config.Validate(); err != nil {
return config, fmt.Errorf("Validating config contents: %s", err)
}
return config, nil
}
func (c Config) Validate() error {
if c.LogLevel == "" {
return errors.New("Must provide a non-empty LogLevel")
}
if c.Username == "" {
return errors.New("Must provide a non-empty Username")
}
if c.Password == "" {
return errors.New("Must provide a non-empty Password")
}
if err := c.S3Config.Validate(); err != nil {
return fmt.Errorf("Validating S3 configuration: %s", err)
}
return nil
}