-
Notifications
You must be signed in to change notification settings - Fork 111
/
config.go
127 lines (107 loc) · 2.95 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package generator
import (
"time"
"github.com/BurntSushi/toml"
"github.com/pkg/errors"
)
var now = time.Now()
func ProfileFromFile(path string) (*LoadProfileCfg, error) {
var profileCfg LoadProfileCfg
_, err := toml.DecodeFile(path, &profileCfg)
if err != nil {
return nil, errors.Wrapf(err, "Invalid load profile %q", path)
}
return &profileCfg, nil
}
type LoadProfileCfg struct {
Templates Templates `toml:"templates"`
GeneratorCfg GeneratorCfg `toml:"generator"`
}
func (l *LoadProfileCfg) BuildRunner() (*LoadGenRunner, error) {
groups, err := l.BuildSupervisorGroups()
if err != nil {
return nil, err
}
return &LoadGenRunner{SupervisorGroups: groups}, nil
}
func (l *LoadProfileCfg) BuildSupervisorGroups() (SupervisorGroupCollection, error) {
groups := []*SupervisorGroup{}
for _, supCfg := range l.GeneratorCfg.Supervisors {
group := SupervisorGroup{Name: supCfg.Name, Count: supCfg.Count}
supT := l.Templates.GetTemplate(supCfg.TemplateName)
for _, svcT := range supT.ServiceTemplates {
var m MessagePrototype
m.SetRelease()
err := m.ApplySvcTemplate(svcT)
if err != nil {
return nil, err
}
m.ApplySupCfg(supCfg)
group.MessagePrototypes = append(group.MessagePrototypes, &m)
}
groups = append(groups, &group)
}
return SupervisorGroupCollection(groups), nil
}
// [templates]
// [[templates.supervisor]]
// # SupervisorTemplate one
// [[templates.supervisor]]
// # SupervisorTemplate two
type Templates struct {
SupervisorTemplates []SupervisorTemplate `toml:"supervisor"`
}
func (ts Templates) GetTemplate(name string) *SupervisorTemplate {
for _, t := range ts.SupervisorTemplates {
if t.Name == name {
return &t
}
}
return nil
}
// name = "pos_terminal"
// [[ service ]]
// package = "noms/pos-service-1"
// [[ service ]]
// package = "noms/pos-service-2"
type SupervisorTemplate struct {
Name string `toml:"name"`
ServiceTemplates []ServiceTemplate `toml:"service"`
}
// [[ service ]]
// package = "noms/pos-service-2"
type ServiceTemplate struct {
Package string `toml:"package"`
Application string `toml:"application"`
}
// [generator]
// [[generator.supervisor]]
// # SupervisorCfg one
// [[generator.supervisor]]
// # SupervisorCfg two
type GeneratorCfg struct {
Supervisors []SupervisorCfg `toml:"supervisor"`
}
// [[generator.supervisor]]
// name = "pos_terminals:production1"
// template = "pos_terminal"
// count = 1000
// channel = "production1"
// [site_range]
// format = "restaurant-prod1-%s"
// count = 1000
type SupervisorCfg struct {
Name string `toml:"name"`
TemplateName string `toml:"template"`
Count int32 `toml:"count"`
Channel string `toml:"channel"`
Environment string `toml:"environment"`
Site string `toml:"site"`
}
// [site_range]
// format = "restaurant-prod1-%s"
// count = 1000
type SiteRange struct {
FormatStr string `toml:"format"`
Count int32 `toml:"count"`
}