-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.go
365 lines (331 loc) · 8.52 KB
/
model.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
package spring
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"sort"
"strings"
"github.com/jenkins-x/jx/pkg/util"
"gopkg.in/AlecAivazis/survey.v1"
)
const (
OptionGroupId = "group"
OptionArtifactId = "artifact"
OptionLanguage = "language"
OptionJavaVersion = "java-version"
OptionBootVersion = "boot-version"
OptionPackaging = "packaging"
OptionDependency = "dep"
OptionDependencyKind = "kind"
startSpringURL = "http://start.spring.io"
)
var (
DefaultDependencyKinds = []string{"Core", "Web", "Template Engines", "SQL", "I/O", "Ops"}
)
type SpringValue struct {
Type string
Default string
}
type SpringOption struct {
ID string
Name string
Description string
VersionRange string
}
type SpringOptions struct {
Type string
Default string
Values []SpringOption
}
type SpringTreeGroup struct {
Name string
Values []SpringOption
}
type SpringTreeSelect struct {
Type string
Values []SpringTreeGroup
}
type SpringBootModel struct {
Packaging SpringOptions
Language SpringOptions
JavaVersion SpringOptions
BootVersion SpringOptions
GroupId SpringValue
ArtifactId SpringValue
Version SpringValue
Name SpringValue
Description SpringValue
PackageName SpringValue
Dependencies SpringTreeSelect
}
type SpringBootForm struct {
Packaging string
Language string
JavaVersion string
BootVersion string
GroupId string
ArtifactId string
Version string
Name string
PackageName string
Dependencies []string
DependencyKinds []string
}
func LoadSpringBoot(cacheDir string) (*SpringBootModel, error) {
loader := func() ([]byte, error) {
client := http.Client{}
req, err := http.NewRequest(http.MethodGet, startSpringURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
res, err := client.Do(req)
if err != nil {
return nil, err
}
return ioutil.ReadAll(res.Body)
}
cacheFileName := ""
if cacheDir != "" {
cacheFileName = filepath.Join(cacheDir, "start_spring_io.json")
}
body, err := util.LoadCacheData(cacheFileName, loader)
if err != nil {
return nil, err
}
model := SpringBootModel{}
err = json.Unmarshal(body, &model)
if err != nil {
return nil, err
}
return &model, nil
}
func (model *SpringBootModel) CreateSurvey(data *SpringBootForm, advanced bool, batchMode bool) error {
err := model.ValidateInput(OptionLanguage, &model.Language, data.Language)
if err != nil {
return err
}
err = model.ValidateInput(OptionBootVersion, &model.BootVersion, data.BootVersion)
if err != nil {
return err
}
err = model.ValidateInput(OptionJavaVersion, &model.JavaVersion, data.JavaVersion)
if err != nil {
return err
}
err = model.ValidateInput(OptionPackaging, &model.Packaging, data.Packaging)
if err != nil {
return err
}
err = model.ValidateTreeInput(OptionDependency, &model.Dependencies, data.Dependencies)
if err != nil {
return err
}
var qs = []*survey.Question{}
if batchMode {
return nil
}
if data.Language == "" {
qs = append(qs, CreateValueSelect("Language", "language", &model.Language, data))
}
if data.BootVersion == "" && advanced {
qs = append(qs, CreateValueSelect("Spring Boot version", "bootVersion", &model.BootVersion, data))
}
if data.JavaVersion == "" && advanced {
qs = append(qs, CreateValueSelect("Java version", "javaVersion", &model.JavaVersion, data))
}
if data.Packaging == "" && advanced {
qs = append(qs, CreateValueSelect("Packaging", "packaging", &model.Packaging, data))
}
if data.GroupId == "" {
qs = append(qs, CreateValueInput("Group", "groupId", &model.GroupId, data))
}
if data.ArtifactId == "" {
qs = append(qs, CreateValueInput("Artifact", "artifactId", &model.ArtifactId, data))
}
if emptyArray(data.Dependencies) {
qs = append(qs, CreateSpringTreeSelect("Dependencies", "dependencies", &model.Dependencies, data))
}
return survey.Ask(qs, data)
}
func (options *SpringOptions) StringArray() []string {
values := []string{}
for _, o := range options.Values {
id := o.ID
if id != "" {
values = append(values, id)
}
}
sort.Strings(values)
return values
}
func (options *SpringTreeSelect) StringArray() []string {
values := []string{}
for _, g := range options.Values {
for _, o := range g.Values {
id := o.ID
if id != "" {
values = append(values, id)
}
}
}
sort.Strings(values)
return values
}
func (model *SpringBootModel) ValidateInput(name string, options *SpringOptions, value string) error {
if value != "" && options != nil {
for _, v := range options.Values {
if v.ID == value {
return nil
}
}
return util.InvalidOption(name, value, options.StringArray())
}
return nil
}
func (model *SpringBootModel) ValidateTreeInput(name string, options *SpringTreeSelect, values []string) error {
if values != nil && len(values) > 0 && options != nil {
for _, value := range values {
if value != "" {
valid := false
for _, g := range options.Values {
for _, o := range g.Values {
if o.ID == value {
valid = true
break
}
}
}
if !valid {
return util.InvalidOption(name, value, options.StringArray())
}
}
}
}
return nil
}
func CreateValueSelect(message string, name string, options *SpringOptions, data *SpringBootForm) *survey.Question {
values := options.StringArray()
return &survey.Question{
Name: name,
Prompt: &survey.Select{
Message: message + ":",
Options: values,
Default: options.Default,
},
Validate: survey.Required,
}
}
func CreateValueInput(message string, name string, value *SpringValue, data *SpringBootForm) *survey.Question {
return &survey.Question{
Name: name,
Prompt: &survey.Input{
Message: message + ":",
Default: value.Default,
},
Validate: survey.Required,
}
}
func CreateSpringTreeSelect(message string, name string, tree *SpringTreeSelect, data *SpringBootForm) *survey.Question {
dependencyKinds := []string{}
if data.DependencyKinds != nil {
dependencyKinds = data.DependencyKinds
}
if len(dependencyKinds) == 0 {
dependencyKinds = DefaultDependencyKinds
}
values := []string{}
for _, t := range tree.Values {
name := t.Name
if util.StringArrayIndex(dependencyKinds, name) >= 0 {
for _, v := range t.Values {
id := v.ID
if id != "" {
values = append(values, id)
}
}
}
}
sort.Strings(values)
return &survey.Question{
Name: name,
Prompt: &survey.MultiSelect{
Message: message + ":",
Options: values,
},
Validate: survey.Required,
}
}
func (data *SpringBootForm) CreateProject(workDir string) (string, error) {
dirName := data.ArtifactId
if dirName == "" {
dirName = "project"
}
answer := filepath.Join(workDir, dirName)
client := http.Client{}
form := url.Values{}
data.AddFormValues(&form)
parameters := form.Encode()
if parameters != "" {
parameters = "?" + parameters
}
u := "http://start.spring.io/starter.zip" + parameters
//fmt.Printf("generating spring project from: %s\n", u)
req, err := http.NewRequest(http.MethodGet, u, strings.NewReader(""))
if err != nil {
return answer, err
}
res, err := client.Do(req)
if err != nil {
return answer, err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return answer, err
}
dir := filepath.Join(workDir, dirName)
zipFile := dir + ".zip"
err = ioutil.WriteFile(zipFile, body, util.DefaultWritePermissions)
if err != nil {
return answer, fmt.Errorf("Failed to download file %s due to %s", zipFile, err)
}
err = util.Unzip(zipFile, dir)
if err != nil {
return answer, fmt.Errorf("Failed to unzip new project file %s due to %s", zipFile, err)
}
err = os.Remove(zipFile)
if err != nil {
return answer, err
}
return answer, nil
}
func (data *SpringBootForm) AddFormValues(form *url.Values) {
AddFormValue(form, "packaging", data.Packaging)
AddFormValue(form, "language", data.Language)
AddFormValue(form, "javaVersion", data.JavaVersion)
AddFormValue(form, "bootVersion", data.BootVersion)
AddFormValue(form, "groupId", data.GroupId)
AddFormValue(form, "artifactId", data.ArtifactId)
AddFormValue(form, "version", data.Version)
AddFormValue(form, "name", data.Name)
AddFormValues(form, "dependencies", data.Dependencies)
}
func AddFormValues(form *url.Values, key string, values []string) {
for _, v := range values {
if v != "" {
form.Add(key, v)
}
}
}
func AddFormValue(form *url.Values, key string, v string) {
if v != "" {
form.Add(key, v)
}
}
func emptyArray(values []string) bool {
return values == nil || len(values) == 0
}