-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
57 lines (44 loc) · 1.12 KB
/
configuration.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 main
import (
"fmt"
"io/ioutil"
"gopkg.in/yaml.v2"
)
type Configuration struct {
Host string `yaml:"host"`
Application string `yaml:"application"`
ApiToken string `yaml:"api_token"`
Stages map[string][]string `yaml:"stages"`
}
func (c *Configuration) Validate() error {
if c.Host == "" {
return fmt.Errorf("Empty `host` field in configuration file.")
}
if c.Application == "" {
return fmt.Errorf("Empty `application` field in configuration file.")
}
if c.ApiToken == "" {
return fmt.Errorf("Empty `api_token` field in configuration file.")
}
if len(c.Stages) == 0 {
return fmt.Errorf("Empty `stages` field in configuration file.")
}
for k, v := range c.Stages {
if len(v) == 0 {
return fmt.Errorf("Empty `stages.%s` field in configuration file", k)
}
}
return nil
}
func readConfiguration(path string) (*Configuration, error) {
var config Configuration
configFile, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(configFile, &config)
if err != nil {
return nil, err
}
return &config, nil
}