-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathconfig.go
73 lines (60 loc) · 1.58 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 (
"fmt"
"net/http"
"strings"
yaml "gopkg.in/yaml.v2"
)
type config struct {
Marathon configMarathon `yaml:"marathon"`
Image configImage `yaml:"image"`
Environments map[string]configEnvironment `yaml:"environments"`
}
type configMarathon struct {
Host string `yaml:"host"`
Headers http.Header
}
type configImage struct {
Repository string `yaml:"repository"`
Name string `yaml:"name"`
TagTemplate string `yaml:"tagTemplate"`
}
type configEnvironment struct {
Marathon struct {
File string `yaml:"file"`
} `yaml:"marathon"`
Images map[string]configImage
}
func configLoad(fileData []byte, flags flags) (config, error) {
// Parse file YAML
var c config
err := yaml.Unmarshal(fileData, &c)
if err != nil {
return config{}, err
}
// Check environment exists
if _, ok := c.Environments[flags.env]; !ok {
return config{}, fmt.Errorf("Environment %s not found in config", flags.env)
}
// Override marathon host if provided
if flags.marathonHost != "" {
c.Marathon.Host = flags.marathonHost
}
// Parse marathon headers if provided
if flags.marathonCurlOpts != "" {
c.Marathon.Headers = http.Header{}
curlOpts := strings.Split(flags.marathonCurlOpts, "-H")
for _, curlOpt := range curlOpts {
curlOpt = strings.TrimSpace(strings.Replace(curlOpt, "\"", "", 2))
if curlOpt == "" {
continue
}
curlOptSplit := strings.Split(curlOpt, ": ")
if len(curlOptSplit) == 2 {
c.Marathon.Headers.Add(curlOptSplit[0], curlOptSplit[1])
}
}
}
// Return config struct
return c, nil
}