forked from simplesurance/baur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.go
391 lines (301 loc) · 8.77 KB
/
loader.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package baur
import (
"fmt"
"path/filepath"
"strings"
"github.com/simplesurance/baur/v1/cfg"
"github.com/simplesurance/baur/v1/internal/fs"
)
type Logger interface {
Debugf(format string, v ...interface{})
}
// Loader discovers and instantiates apps and tasks.
type Loader struct {
logger Logger
includeDB *cfg.IncludeDB
repositoryRoot string
appConfigPaths []string
gitCommitIDFunc func() (string, error)
}
// NewLoader instantiates a Loader.
// When an app config is loaded the DefaultResolvers are applied on the content
// before they are merged with their includes. The gitCommitIDFunc is used as
// config resolved to resolve $GITCOMMIT variables.
func NewLoader(repoCfg *cfg.Repository, gitCommitIDFunc func() (string, error), logger Logger) (*Loader, error) {
repositoryRootDir := filepath.Dir(repoCfg.FilePath())
appConfigPaths, err := findAppConfigs(fs.AbsPaths(repositoryRootDir, repoCfg.Discover.Dirs), repoCfg.Discover.SearchDepth)
if err != nil {
return nil, fmt.Errorf("discovering application config files failed: %w", err)
}
logger.Debugf("loader: found the following application configs:\n%s", strings.Join(appConfigPaths, "\n"))
return &Loader{
logger: logger,
repositoryRoot: repositoryRootDir,
includeDB: cfg.NewIncludeDB(logger.Debugf),
appConfigPaths: appConfigPaths,
gitCommitIDFunc: gitCommitIDFunc,
}, nil
}
// LoadTasks loads the tasks of apps that match the passed specifier.
// Specifier format is <APP-SPEC>[.<TASK-SPEC>].
// <APP-SPEC> is:
// - <APP-NAME> or
// - '*'
// <TASK-SPEC> is:
// - Task Name or
// - '*'
// If no specifier is passed all tasks of all apps are returned.
// If multiple specifiers match the same task, it's only returned 1x in the returned slice.
func (a *Loader) LoadTasks(specifier ...string) ([]*Task, error) {
var result []*Task
specs, err := parseSpecs(specifier)
if err != nil {
return nil, err
}
specs.all = specs.all || len(specifier) == 0
apps, err := a.apps(specs)
if err != nil {
return nil, err
}
if specs.all {
return a.allTasks(apps), nil
}
result = a.allTasks(apps)
tasks, err := a.tasks(specs.taskSpecs)
if err != nil {
return nil, err
}
result = append(result, tasks...)
return dedupTasks(result), nil
}
// LoadApps loads the apps that match the passed specifiers.
// Valid specifiers are:
// - application directory path
// - <APP-NAME>
// - '*'
// If no specifier is passed all apps are returned.
// If multiple specifiers match the same app, it's only returned 1x in the returned slice.
func (a *Loader) LoadApps(specifier ...string) ([]*App, error) {
specs, err := parseSpecs(specifier)
if err != nil {
return nil, err
}
if len(specs.taskSpecs) > 0 {
return nil, fmt.Errorf("invalid app specifiers: %s", specs.taskSpecs)
}
specs.all = specs.all || len(specifier) == 0
return a.apps(specs)
}
// AppNames discovers and loads the apps with the given names.
func (a *Loader) AppNames(names ...string) ([]*App, error) {
namesMap := make(map[string]struct{}, len(names))
result := make([]*App, 0, len(names))
a.logger.Debugf("loader: loading app %q", names)
for _, name := range names {
namesMap[name] = struct{}{}
}
for _, path := range a.appConfigPaths {
if len(namesMap) == 0 {
return result, nil
}
path, err := filepath.Abs(path)
if err != nil {
return nil, err
}
appCfg, err := cfg.AppFromFile(path)
if err != nil {
return nil, fmt.Errorf("%s: %w", path, err)
}
if _, exist := namesMap[appCfg.Name]; !exist {
continue
}
app, err := a.fromCfg(appCfg)
if err != nil {
return nil, fmt.Errorf("%s: %w", path, err)
}
result = append(result, app)
delete(namesMap, appCfg.Name)
}
notFoundApps := make([]string, 0, len(namesMap))
for name := range namesMap {
notFoundApps = append(notFoundApps, name)
}
if len(notFoundApps) != 0 {
return nil, fmt.Errorf("could not find the following apps: %s", strings.Join(notFoundApps, ", "))
}
return result, nil
}
func (a *Loader) allApps() ([]*App, error) {
a.logger.Debugf("loader: loading all apps")
result := make([]*App, 0, len(a.appConfigPaths))
for _, path := range a.appConfigPaths {
app, err := a.AppPath(path)
if err != nil {
return nil, fmt.Errorf("%s: %w", path, err)
}
result = append(result, app)
}
return result, nil
}
func (a *Loader) allTasks(apps []*App) []*Task {
var result []*Task
for _, app := range apps {
result = append(result, app.Tasks()...)
}
return result
}
// AppDirs load apps from the given directories.
func (a *Loader) AppDirs(dirs ...string) ([]*App, error) {
result := make([]*App, 0, len(dirs))
for _, dir := range dirs {
cfgPath := filepath.Join(dir, AppCfgFile)
app, err := a.AppPath(cfgPath)
if err != nil {
return nil, fmt.Errorf("%s: %w", cfgPath, err)
}
result = append(result, app)
}
return result, nil
}
// AppPath loads the app from the config file.
func (a *Loader) AppPath(appConfigPath string) (*App, error) {
a.logger.Debugf("loader: loading app from %q", appConfigPath)
appConfigPath, err := filepath.Abs(appConfigPath)
if err != nil {
return nil, err
}
appCfg, err := cfg.AppFromFile(appConfigPath)
if err != nil {
return nil, err
}
return a.fromCfg(appCfg)
}
// tasks load all tasks for the given taskSpecs.
// wildcards are only supported for appNames.
func (a *Loader) tasks(taskSpecs []*taskSpec) ([]*Task, error) {
result := make([]*Task, 0, len(taskSpecs))
taskSpecMap := make(map[string][]string, len(taskSpecs))
appNames := make([]string, 0, len(taskSpecs))
for _, t := range taskSpecs {
val, exist := taskSpecMap[t.appName]
if exist {
taskSpecMap[t.appName] = append(val, t.taskName)
continue
}
appNames = append(appNames, t.appName)
taskSpecMap[t.appName] = []string{t.taskName}
}
var apps []*App
var err error
if _, exist := taskSpecMap["*"]; exist {
apps, err = a.allApps()
} else {
apps, err = a.AppNames(appNames...)
}
if err != nil {
return nil, err
}
for _, app := range apps {
taskSpecs := taskSpecMap[app.Name]
taskSpecs = append(taskSpecs, taskSpecMap["*"]...)
if len(taskSpecs) == 0 {
panic(fmt.Sprintf("app %q was loaded which was not part of taskSpecs: %v", app.Name, taskSpecs))
}
for _, spec := range taskSpecs {
for _, task := range app.Tasks() {
if task.Name == spec {
result = append(result, task)
}
}
}
}
return result, nil
}
func (a *Loader) apps(specs *specs) ([]*App, error) {
if specs.all {
return a.allApps()
}
result := make([]*App, 0, len(specs.appDirs)+len(specs.appNames))
for _, path := range specs.appDirs {
apps, err := a.AppDirs(path)
if err != nil {
return nil, fmt.Errorf("%s: %w", path, err)
}
result = append(result, apps...)
}
apps, err := a.AppNames(specs.appNames...)
if err != nil {
return nil, err
}
result = append(result, apps...)
return dedupApps(result), nil
}
func (a *Loader) fromCfg(appCfg *cfg.App) (*App, error) {
includeResolvers := IncludeCfgVarResolvers(a.repositoryRoot, appCfg.Name)
err := appCfg.Merge(a.includeDB, includeResolvers)
if err != nil {
return nil, fmt.Errorf("merging includes failed: %w", err)
}
resolvers := DefaultAppCfgResolvers(a.repositoryRoot, appCfg.Name, a.gitCommitIDFunc)
err = appCfg.Resolve(resolvers)
if err != nil {
return nil, fmt.Errorf("resolving variables in config failed: %w", err)
}
err = appCfg.Validate()
if err != nil {
return nil, fmt.Errorf("validation failed: %w", err)
}
app, err := NewApp(appCfg, a.repositoryRoot)
if err != nil {
return nil, err
}
return app, nil
}
// IsAppDirectory returns true if the directory contains an app config file.
func isAppDirectory(dir string) bool {
cfgPath := filepath.Join(dir, AppCfgFile)
isFile, _ := fs.IsFile(cfgPath)
return isFile
}
func findAppConfigs(searchDirs []string, searchDepth int) ([]string, error) {
var result []string
for _, searchDir := range searchDirs {
if err := fs.DirsExist(searchDir); err != nil {
return nil, fmt.Errorf("application search directory: %w", err)
}
cfgPaths, err := fs.FindFilesInSubDir(searchDir, AppCfgFile, searchDepth)
if err != nil {
return nil, err
}
result = append(result, cfgPaths...)
}
return result, nil
}
func dedupApps(apps []*App) []*App {
dedupMap := make(map[string]*App, len(apps))
for _, app := range apps {
if _, exist := dedupMap[app.Path]; exist {
continue
}
dedupMap[app.Path] = app
}
result := make([]*App, 0, len(dedupMap))
for _, app := range dedupMap {
result = append(result, app)
}
return result
}
func dedupTasks(tasks []*Task) []*Task {
dedupMap := make(map[string]*Task, len(tasks))
for _, task := range tasks {
if _, exist := dedupMap[task.ID()]; exist {
continue
}
dedupMap[task.ID()] = task
}
result := make([]*Task, 0, len(dedupMap))
for _, task := range dedupMap {
result = append(result, task)
}
return result
}