/
wizard.go
298 lines (256 loc) · 6.83 KB
/
wizard.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
package wizard
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"text/tabwriter"
"github.com/howeyc/gopass"
"github.com/spf13/cobra"
"github.com/ovh/cds/sdk"
)
// Cmd dashboard
var Cmd = &cobra.Command{
Use: "wizard",
Short: "Interactive application configuration wizard",
Long: "",
Aliases: []string{"wiz"},
Run: func(cmd *cobra.Command, args []string) {
runWizard()
},
}
func runWizard() {
fmt.Printf("Application name: ")
appName := readline()
project := choseProject(appName)
buildTmpl := choseBuildTemplate()
buildTmpl = choseBuildTemplateParameters(buildTmpl)
applyTemplate(project.Key, appName, buildTmpl)
}
func applyTemplate(projectKey string, name string, build sdk.Template) {
app, err := sdk.ApplyApplicationTemplate(projectKey, name, build)
if err != nil {
sdk.Exit("Error: Cannot apply template (%s)\n", err)
}
fmt.Printf("\n")
fmt.Printf("Application %s/%s created\n", projectKey, app.Name)
var pip sdk.Pipeline
for i := range app.Pipelines {
if app.Pipelines[i].Pipeline.Type == sdk.BuildPipeline {
pip = app.Pipelines[i].Pipeline
break
}
}
if pip.ID > 0 {
fmt.Printf("\n")
fmt.Printf("Do you want to run %s/%s/%s now ? [Y/n]: ", projectKey, app.Name, pip.Name)
run := readline()
if run == "Y" || run == "y" || run == "yes" || run == "" {
fmt.Printf("Running %s/%s/%s:\n", projectKey, app.Name, pip.Name)
ch, err := sdk.RunPipeline(projectKey, app.Name, pip.Name, "NoEnv", true, sdk.RunRequest{}, false)
if err != nil {
sdk.Exit("Error: Cannot start build (%s)\n", err)
}
w := tabwriter.NewWriter(os.Stdout, 27, 1, 2, ' ', 0)
titles := []string{"DATE", "ACTION", "LOG"}
fmt.Fprintln(w, strings.Join(titles, "\t"))
for l := range ch {
fmt.Fprintf(w, "%s\t%s\t%s",
[]byte(l.Timestamp.String())[:19],
l.Step,
l.Value,
)
w.Flush()
// Exit 1 if pipeline fail
if l.ID == 0 && strings.Contains(l.Value, "status: Fail") {
sdk.Exit("")
}
}
}
}
}
func choseDeploymentTemplate() sdk.Template {
var t sdk.Template
fmt.Printf("\nDo you need a deployment pipeline ? [Y/n]: ")
a := readline()
if a == "n" || a == "no" {
return t
}
for {
tmpls, err := sdk.GetDeploymentTemplates()
if err != nil {
sdk.Exit("Error: cannot retrieve deployment templates (%s)\n", err)
}
fmt.Printf("List of existing deployment templates:\n")
for i, t := range tmpls {
fmt.Printf("-[%d] %-15s (%s)\n", i+1, t.Name, t.Description)
}
fmt.Printf("which deployment template do you want to apply [1-%d]: ", len(tmpls))
tmplIDS := readline()
tmplID, err := strconv.Atoi(tmplIDS)
if err != nil {
fmt.Printf("Error: %s is not a valid number\n", tmplIDS)
continue
}
if tmplID > len(tmpls) || tmplID <= 0 {
fmt.Printf("Error: %s is not a valid number", tmplIDS)
continue
}
return tmpls[tmplID-1]
}
}
func choseDeploymentTemplateParameters(tmpl sdk.Template) sdk.Template {
if tmpl.ID == 0 || len(tmpl.Params) == 0 {
return tmpl
}
fmt.Printf("\nTemplate %s parameters:\n", tmpl.Name)
for i, p := range tmpl.Params {
if p.Type == sdk.SecretVariable {
fmt.Printf("- %-15s (%s) [no echo]: ", p.Name, p.Description)
pass, err := gopass.GetPasswd()
if err != nil {
fmt.Printf("\nError: cannot read password (%s)\n", err)
}
tmpl.Params[i].Value = string(pass)
} else {
fmt.Printf("- %-15s (%s) [%s]: ", p.Name, p.Description, p.Type)
tmpl.Params[i].Value = readline()
}
}
return tmpl
}
func getParamValue(p sdk.TemplateParam) string {
var val string
for {
if p.Type == sdk.SecretVariable {
fmt.Printf("- %-15s (%s) [no echo]: ", p.Name, p.Description)
pass, err := gopass.GetPasswd()
if err != nil {
fmt.Printf("\nError: cannot read password (%s)\n", err)
}
val = string(pass)
} else {
fmt.Printf("- %-15s (%s) [%s]: ", p.Name, p.Description, p.Type)
val = readline()
}
switch p.Type {
case sdk.BooleanVariable:
if strings.ToLower(val) == "y" ||
strings.ToLower(val) == "yes" ||
strings.ToLower(val) == "n" ||
strings.ToLower(val) == "no" {
return val
}
_, err := strconv.ParseBool(val)
if err != nil {
fmt.Printf("Value '%s' is not a boolean\n", val)
continue
}
return val
}
return val
}
}
func choseBuildTemplateParameters(tmpl sdk.Template) sdk.Template {
if len(tmpl.Params) == 0 {
return tmpl
}
fmt.Printf("\nTemplate %s parameters:\n", tmpl.Name)
for i, p := range tmpl.Params {
tmpl.Params[i].Value = getParamValue(p)
}
return tmpl
}
func choseBuildTemplate() sdk.Template {
for {
tmpls, err := sdk.GetBuildTemplates()
if err != nil {
sdk.Exit("Error: cannot retrieve build templates (%s)\n", err)
}
fmt.Printf("\nList of existing build templates:\n")
for i, t := range tmpls {
fmt.Printf("-[%d] %-15s (%s)\n", i+1, t.Name, t.Description)
}
fmt.Printf("which build template do you want to apply [1-%d]: ", len(tmpls))
tmplIDS := readline()
tmplID, err := strconv.Atoi(tmplIDS)
if err != nil {
fmt.Printf("Error: %s is not a valid number\n", tmplIDS)
continue
}
if tmplID > len(tmpls) || tmplID <= 0 {
fmt.Printf("Error: %s is not a valid number\n", tmplIDS)
continue
}
return tmpls[tmplID-1]
}
}
func createProject() sdk.Project {
for {
fmt.Printf("\n")
fmt.Printf("Project name: ")
name := readline()
fmt.Printf("Project key: ")
key := readline()
fmt.Printf("Project admin group: ")
group := readline()
err := sdk.AddProject(name, key, group)
if err != nil {
fmt.Printf("Error: Cannot create project (%s)\n", err)
continue
}
p, err := sdk.GetProject(key)
if err != nil {
sdk.Exit("Error: Cannot retrieve project (%s)\n", err)
}
return p
}
}
func choseProject(appName string) sdk.Project {
for {
fmt.Printf("\nProjects you have access to:\n")
projects, err := sdk.ListProject()
if err != nil {
sdk.Exit("Error: cannot retrieve project list (%s)\n", err)
}
for i, p := range projects {
fmt.Printf("-[%d] %s\n", i+1, p.Key)
}
var lowerBound int
if len(projects) > 0 {
lowerBound = 1
}
fmt.Printf("Creating application %s in [Enter: create][%d-%d]: ", appName, lowerBound, len(projects))
projectIDS := readline()
// create a new project ?
if projectIDS == "" {
return createProject()
}
projectID, err := strconv.Atoi(projectIDS)
if err != nil {
fmt.Printf("Error: %s is not a valid number\n", projectIDS)
continue
}
if projectID > len(projects) || projectID <= 0 {
fmt.Printf("Error: %s is not a valid number", projectIDS)
continue
}
return projects[projectID-1]
}
}
func readline() string {
var all string
var line []byte
var err error
hasMoreInLine := true
bio := bufio.NewReader(os.Stdin)
for hasMoreInLine {
line, hasMoreInLine, err = bio.ReadLine()
if err != nil {
sdk.Exit("Error: cannot read from stdin (%s)\n", err)
}
all += string(line)
}
return strings.Replace(all, "\n", "", -1)
}