-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.go
129 lines (104 loc) · 2.26 KB
/
builder.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
package builder
import (
"fmt"
"text/template"
"github.com/sirupsen/logrus"
"github.com/gogap/config"
)
type Builder struct {
options *Options
projects map[string]*Project
projectsKeys []string
}
type Option func(*Options)
type Options struct {
Config config.Configuration
UpdateRepo bool
Template *template.Template
}
func ConfigFile(file string) Option {
return func(o *Options) {
o.Config = config.NewConfig(config.ConfigFile(file))
}
}
func WithConfig(conf config.Configuration) Option {
return func(o *Options) {
o.Config = conf
}
}
func ConfigString(configStr string) Option {
return func(o *Options) {
o.Config = config.NewConfig(config.ConfigString(configStr))
}
}
func Template(tmpl *template.Template) Option {
return func(o *Options) {
o.Template = tmpl
}
}
func UpdateRepo(update bool) Option {
return func(o *Options) {
o.UpdateRepo = update
}
}
func NewBuilder(opts ...Option) (builder *Builder, err error) {
builderOpts := &Options{}
for _, o := range opts {
o(builderOpts)
}
var projs = make(map[string]*Project)
var projKeys []string
bu := &Builder{options: builderOpts}
for _, projName := range builderOpts.Config.Keys() {
var proj *Project
proj, err = NewProject(projName, bu)
if err != nil {
return
}
if _, exist := projs[projName]; exist {
if exist {
err = fmt.Errorf("project: %s already exist", projName)
return
}
}
projs[projName] = proj
projKeys = append(projKeys, projName)
}
bu.projectsKeys = projKeys
bu.projects = projs
builder = bu
return
}
func (p *Builder) ListProject() []string {
var porj []string
for _, c := range p.projectsKeys {
porj = append(porj, c)
}
return porj
}
func (p *Builder) Build(data map[string]interface{}, porj ...string) (err error) {
for _, c := range porj {
logrus.WithField("project", c).Infoln("building")
err = p.projects[c].Build(data, false, nil)
if err != nil {
return
}
}
return
}
func (p *Builder) Run(data map[string]interface{}, porj string, args []string) (err error) {
err = p.projects[porj].Build(data, true, args)
if err != nil {
return
}
return
}
func (p *Builder) Pull(porj ...string) (err error) {
for _, c := range porj {
err = p.projects[c].Pull()
if err != nil {
return
}
}
return
}