This repository has been archived by the owner on May 8, 2023. It is now read-only.
/
generate.go
260 lines (232 loc) · 6.34 KB
/
generate.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package edward
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/yext/edward/common"
"github.com/yext/edward/config"
"github.com/yext/edward/generators"
"github.com/yext/edward/services"
)
func (c *Client) Generate(names []string, force bool, group string, targets []string) error {
var cfg config.Config
configPath := c.Config
if configPath == "" {
configPath = "edward.json"
}
configPath = filepath.Join(c.WorkingDir, configPath)
if f, err := os.Stat(configPath); err == nil && f.Size() != 0 {
cfg, err = config.LoadConfig(configPath, common.EdwardVersion)
if err != nil {
return errors.WithMessage(err, configPath)
}
} else {
cfg = config.EmptyConfig(filepath.Dir(configPath))
}
targetedGenerators, err := generatorsMatchingTargets(targets)
if err != nil {
return errors.WithStack(err)
}
generators := &generators.GeneratorCollection{
Generators: targetedGenerators,
Path: c.WorkingDir,
Targets: names,
}
err = generators.Generate()
if err != nil {
return errors.WithStack(err)
}
foundServices := generators.Services()
foundGroups := generators.Groups()
foundImports := generators.Imports()
if len(foundServices) == 0 &&
len(foundGroups) == 0 &&
len(foundImports) == 0 {
c.UI.Errorf("No services, groups or imports found")
return nil
}
filteredServices, filteredGroups, filteredImports, err := c.filterGenerated(
&cfg,
foundServices,
foundGroups,
foundImports,
)
if err != nil {
return errors.WithStack(err)
}
if len(filteredServices) == 0 &&
len(filteredGroups) == 0 &&
len(filteredImports) == 0 {
c.UI.Errorf("No new services, groups or imports found")
return nil
}
// Check for duplicates
duplicates := findDuplicates(append(filteredServices, filteredGroups...))
if len(duplicates) > 0 {
return errors.New(fmt.Sprint("Multiple services or groups were found with the names: ", strings.Join(duplicates, ", ")))
}
// Prompt user to confirm the list of services that will be generated
if !force {
confirmed, err := c.confirmList(&cfg, filteredServices, filteredGroups, filteredImports)
if !confirmed {
return errors.WithStack(err)
}
}
foundServices, err = cfg.NormalizeServicePaths(c.WorkingDir, foundServices)
if err != nil {
return errors.WithStack(err)
}
err = cfg.AppendServices(foundServices)
if err != nil {
return errors.WithStack(err)
}
err = cfg.AppendGroups(foundGroups)
if err != nil {
return errors.WithStack(err)
}
// Put all discovered services into the specified group
if len(group) > 0 {
var newGroupConfig *services.ServiceGroupConfig
if existingGroup, ok := cfg.GroupMap[group]; ok {
newGroupConfig = existingGroup
err := cfg.RemoveGroup(group)
if err != nil {
return errors.WithStack(err)
}
} else {
newGroupConfig = &services.ServiceGroupConfig{
Name: group,
}
}
for _, s := range filteredServices {
newGroupConfig.Services = append(newGroupConfig.Services, cfg.ServiceMap[s])
}
for _, g := range filteredGroups {
newGroupConfig.Groups = append(newGroupConfig.Groups, cfg.GroupMap[g])
}
cfg.AppendGroups([]*services.ServiceGroupConfig{newGroupConfig})
}
cfg.Imports = append(cfg.Imports, foundImports...)
f, err := os.Create(configPath)
if err != nil {
return errors.WithStack(err)
}
defer f.Close()
w := bufio.NewWriter(f)
err = cfg.Save(w)
if err != nil {
return errors.WithStack(err)
}
err = w.Flush()
if err != nil {
return errors.WithStack(err)
}
c.UI.Infof("Wrote to: %v", configPath)
return nil
}
func findDuplicates(s []string) []string {
found := make(map[string]struct{})
duplicates := make(map[string]struct{})
for _, name := range s {
if _, ok := found[name]; ok {
duplicates[name] = struct{}{}
}
found[name] = struct{}{}
}
var dupList []string
for name := range duplicates {
dupList = append(dupList, name)
}
return dupList
}
func (c *Client) filterGenerated(cfg *config.Config,
foundServices []*services.ServiceConfig,
foundGroups []*services.ServiceGroupConfig,
foundImports []string) ([]string, []string, []string, error) {
var filteredServices []string
for _, service := range foundServices {
if _, ok := cfg.ServiceMap[service.Name]; !ok {
filteredServices = append(filteredServices, service.Name)
}
}
var filteredGroups []string
for _, group := range foundGroups {
if _, ok := cfg.GroupMap[group.Name]; !ok {
filteredGroups = append(filteredGroups, group.Name)
}
}
var filteredImports []string
for _, i := range foundImports {
var found bool
for _, existingImport := range cfg.Imports {
if existingImport == i {
found = true
}
}
if !found {
filteredImports = append(filteredImports, i)
}
}
return filteredServices, filteredGroups, filteredImports, nil
}
func (c *Client) confirmList(cfg *config.Config,
filteredServices []string,
filteredGroups []string,
filteredImports []string) (bool, error) {
c.UI.Infof("The following will be generated:")
if len(filteredServices) > 0 {
c.UI.Infof("Services:")
}
for _, service := range filteredServices {
c.UI.Infof("\t%v", service)
}
if len(filteredGroups) > 0 {
c.UI.Infof("Groups:")
}
for _, group := range filteredGroups {
c.UI.Infof("\t%v", group)
}
if len(filteredImports) > 0 {
c.UI.Infof("Imports:")
}
for _, i := range filteredImports {
c.UI.Infof("\t%v", i)
}
if !c.askForConfirmation("Do you wish to continue?") {
return false, nil
}
return true, nil
}
func generatorsMatchingTargets(targets []string) ([]generators.Generator, error) {
allGenerators := []generators.Generator{
&generators.EdwardGenerator{},
&generators.DockerGenerator{},
&generators.GoGenerator{},
&generators.IcbmGenerator{},
}
if len(targets) == 0 {
return allGenerators, nil
}
targetSet := make(map[string]struct{})
for _, target := range targets {
targetSet[target] = struct{}{}
}
var filteredGenerators = make([]generators.Generator, 0, len(allGenerators))
for _, gen := range allGenerators {
if _, exists := targetSet[gen.Name()]; exists {
filteredGenerators = append(filteredGenerators, gen)
delete(targetSet, gen.Name())
}
}
if len(targetSet) > 0 {
var missingTargets = make([]string, 0, len(targetSet))
for target := range targetSet {
missingTargets = append(missingTargets, target)
}
return nil, fmt.Errorf("targets not found: %v", strings.Join(missingTargets, ", "))
}
return filteredGenerators, nil
}