-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
96 lines (80 loc) · 1.81 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"errors"
"io/ioutil"
"os"
"github.com/ghodss/yaml"
)
const configFile = "/workspaces/deployment-controller/deployment-controller.yaml"
type KeyValue struct {
Key string `yaml:"Key"`
Value string `yaml:"Value"`
}
// cookie data from config file
type Cookie struct {
Expiration string `yaml:"Expiration"`
CanaryPercent float32 `yaml:"CanaryPercent"`
IfSuccessful KeyValue `yaml:"IfSuccessful"`
IfFail KeyValue `yaml:"IfFail"`
}
type View struct {
ShowSuccess bool `yaml:"ShowSuccess"`
ShowFail bool `yaml:"ShowFail"`
}
type Logging struct {
Disable bool `yaml:"Disable"`
}
type App struct {
Name string `yaml:"Name"`
Disable bool `yaml:"Disable"`
CookieInfo Cookie `yaml:"CookieInfo"`
View View `yaml:"View"`
Logging Logging `yaml:"Logging"`
}
type Config struct {
Apps []App `yaml:"Apps"`
Port int `yaml:"Port"`
}
func ReadConfig() (Config, error) {
// set path, use /workspaces/.. if unspecified
configPath := os.Getenv("APP_CONFIG_PATH")
if configPath == "" {
configPath = "/workspaces/deployment-controller/deployment-controller.yaml"
}
config := Config{}
configYaml, err := ioutil.ReadFile(configPath)
if err != nil {
return config, err
}
err = yaml.Unmarshal(configYaml, &config)
if err != nil {
return config, err
}
return config, nil
}
func UpdateConfig(app App) error {
config, err := ReadConfig()
if err != nil {
return err
}
appFound := false
for i, configapp := range config.Apps {
if app.Name == configapp.Name {
configapp = app
config.Apps[i] = configapp
appFound = true
}
}
if !appFound {
return errors.New("could not find app")
}
data, err := yaml.Marshal(config)
if err != nil {
return err
}
err = ioutil.WriteFile(configFile, data, 0)
if err != nil {
return err
}
return nil
}