-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.go
243 lines (205 loc) · 4.82 KB
/
project.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
package project
import (
"fmt"
"github.com/eden-framework/context"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"os/exec"
"path"
"reflect"
"runtime"
"strconv"
"strings"
)
var (
DefaultConfigFile = "project.yml"
DockerRegistry string
)
type Project struct {
Name string `env:"name" yaml:"name"`
Group string `env:"group" yaml:"group,omitempty"`
Owner string `env:"owner" yaml:"owner,omitempty"`
Version Version `env:"version" yaml:"version"`
Desc string `env:"description" yaml:"description"`
ProgramLanguage string `env:"program_language" yaml:"program_language"`
Workflow Workflow `yaml:"workflow,omitempty"`
Scripts map[string]Script `yaml:"scripts,omitempty"`
Feature string `yaml:"feature,omitempty"`
Selector string `env:"selector" yaml:"-"`
}
func (p *Project) UnmarshalFromFile(filePath, fileName string) error {
if fileName == "" {
fileName = DefaultConfigFile
}
bytes, err := ioutil.ReadFile(path.Join(filePath, fileName))
if err != nil {
return err
}
errForUnmarshal := yaml.Unmarshal(bytes, p)
if errForUnmarshal != nil {
return errForUnmarshal
}
return nil
}
func (p Project) WithVersion(s string) Project {
v, err := FromVersionString(s)
if err != nil {
panic(err)
}
p.Version = *v
return p
}
func (p Project) WithGroup(group string) Project {
p.Group = group
return p
}
func (p Project) WithOwner(owner string) Project {
p.Owner = owner
return p
}
func (p Project) WithDesc(desc string) Project {
p.Desc = desc
return p
}
func (p Project) WithName(name string) Project {
p.Name = name
return p
}
func (p Project) WithLanguage(pl string) Project {
p.ProgramLanguage = pl
return p
}
func (p Project) WithWorkflow(workflow string) Project {
p.Workflow.Extends = workflow
return p
}
func (p Project) WithFeature(f string) Project {
p.Feature = f
return p
}
func (p Project) WithScripts(key string, scripts ...string) Project {
if p.Scripts == nil {
p.Scripts = map[string]Script{}
}
p.Scripts[key] = append(Script{}, scripts...)
return p
}
func WrapEnv(s string) string {
return strings.ToUpper("PROJECT_" + s)
}
func SetEnv(k string, v string) {
os.Setenv(k, v)
fmt.Printf("export %s=%s\n", k, v)
}
func (p *Project) SetEnviron() {
if os.Getenv(EnvKeyDockerRegistryKey) == "" {
SetEnv(EnvKeyDockerRegistryKey, DockerRegistry)
}
tpe := reflect.TypeOf(p).Elem()
rv := reflect.Indirect(reflect.ValueOf(p))
for i := 0; i < tpe.NumField(); i++ {
field := tpe.Field(i)
env := field.Tag.Get("env")
if len(env) > 0 {
value := rv.FieldByName(field.Name)
if stringer, ok := value.Interface().(fmt.Stringer); ok {
v := stringer.String()
if len(v) > 0 {
SetEnv(WrapEnv(env), v)
}
} else {
SetEnv(WrapEnv(env), value.String())
}
}
}
}
func (p *Project) Command(args ...string) *exec.Cmd {
p.SetEnviron()
sh := "sh"
if runtime.GOOS == "windows" {
sh = "bash"
}
envVars := context.EnvVars{}
envVars.LoadFromEnviron()
return exec.Command(sh, "-c", envVars.Parse(strings.Join(args, " ")))
}
func (p *Project) Run(commands ...*exec.Cmd) {
for _, cmd := range commands {
if cmd != nil {
context.StdRun(cmd)
}
}
}
func (p *Project) RunScript(key string, inDocker bool) error {
if _, ok := p.Scripts[key]; !ok {
return fmt.Errorf("script %s not defined", key)
}
s := p.Scripts[key]
if inDocker {
builder := RegisteredBuilders.GetBuilderBy(p.ProgramLanguage)
if builder == nil {
return fmt.Errorf("no builder for %s", p.ProgramLanguage)
}
p.Run(
p.Command(fmt.Sprintf(
"docker run --rm -v ${PWD}:%s -w %s %s sh -c %s",
builder.WorkingDir,
builder.WorkingDir,
FullImage(builder.Image),
strconv.Quote(s.String()),
)),
)
return nil
}
p.Run(p.Command(s.String()))
return nil
}
func (p *Project) Execute(args ...string) {
p.Run(p.Command(args...))
}
func (p *Project) WriteToFile(filePath, fileName string) {
if fileName == "" {
fileName = DefaultConfigFile
}
bytes, err := yaml.Marshal(p)
if err != nil {
panic(err)
}
ioutil.WriteFile(path.Join(filePath, fileName), bytes, os.ModePerm)
}
func (p *Project) String() string {
bytes, err := yaml.Marshal(p)
if err != nil {
panic(err)
}
return string(bytes)
}
type Script []string
func (s Script) IsZero() bool {
return len(s) == 0
}
func (s Script) String() string {
return strings.Join(s, " && ")
}
func (s Script) MarshalYAML() (interface{}, error) {
if len(s) > 1 {
return s, nil
}
return s[0], nil
}
func (s *Script) UnmarshalYAML(unmarshal func(interface{}) error) error {
var str string
err := unmarshal(&str)
if err == nil {
*s = []string{str}
} else {
var values []string
err := unmarshal(&values)
if err != nil {
return err
}
*s = values
}
return nil
}