-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
90 lines (73 loc) · 2.43 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
package anser
import (
"github.com/deciduosity/anser/model"
"github.com/cdr/grip"
"github.com/pkg/errors"
)
// NewApplication constructs and sets up an application instance from
// a configuration structure, presumably loaded from a configuration
// file.
//
// You can construct an application instance using default
// initializers, if you do not want to define the migrations using the
// configuration structures.
func NewApplication(env Environment, conf *model.Configuration) (*Application, error) {
if conf == nil {
return nil, errors.New("cannot specify a nil configuration")
}
app := &Application{
Options: conf.Options,
}
catcher := grip.NewBasicCatcher()
for _, g := range conf.SimpleMigrations {
if !g.Options.IsValid() {
catcher.Errorf("simple migration generator '%s' is not valid", g.Options.JobID)
continue
}
if g.Update == nil || len(g.Update) == 0 {
catcher.Errorf("simple migration generator '%s' does not contain an update", g.Options.JobID)
continue
}
grip.Infof("registered simple migration '%s'", g.Options.JobID)
app.Generators = append(app.Generators, NewSimpleMigrationGenerator(env, g.Options, g.Update))
}
for _, g := range conf.ManualMigrations {
if !g.Options.IsValid() {
catcher.Errorf("manual migration generator '%s' is not valid", g.Options.JobID)
continue
}
seen := 0
if _, ok := env.GetManualMigrationOperation(g.Name); ok {
seen++
}
if seen != 1 {
catcher.Errorf("manual migration operation '%s' is not defined ", g.Name)
continue
}
grip.Infof("registered manual migration '%s' (%s)", g.Options.JobID, g.Name)
app.Generators = append(app.Generators, NewManualMigrationGenerator(env, g.Options, g.Name))
}
for _, g := range conf.StreamMigrations {
if !g.Options.IsValid() {
catcher.Add(errors.Errorf("stream migration generator '%s' is not valid", g.Options.JobID))
continue
}
seen := 0
if _, ok := env.GetDocumentProcessor(g.Name); !ok {
seen++
}
if seen != 1 {
catcher.Errorf("stream migration operation '%s' is not defined", g.Name)
continue
}
grip.Infof("registered stream migration '%s' (%s)", g.Options.JobID, g.Name)
app.Generators = append(app.Generators, NewStreamMigrationGenerator(env, g.Options, g.Name))
}
if catcher.HasErrors() {
return nil, catcher.Resolve()
}
if err := app.Setup(env); err != nil {
return nil, errors.Wrap(err, "problem loading migration utility from config file")
}
return app, nil
}