-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal_routing.go
372 lines (338 loc) · 10.1 KB
/
terminal_routing.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
366
367
368
369
370
371
372
package main
import (
"errors"
"log"
"strconv"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/fedevilensky/go-scaffold/internal/inputmodels"
"github.com/fedevilensky/go-scaffold/internal/progressloader"
"github.com/fedevilensky/go-scaffold/internal/project"
)
const (
projName = "projName"
web = "web"
db = "dbLib"
removeDep = "removeDep"
addDep = "addDep"
vendor = "vendor"
build = "build"
)
func projectName(proj *project.Configuration) tea.Model {
next := func() tea.Model { return selectWebLibrary(proj) }
return projectNameWithNext(proj, next)
}
// next func() tea.Model is done in orden to defer build until the last posible moment
func projectNameWithNext(proj *project.Configuration, next func() tea.Model) tea.Model {
nextModel := func() (tea.Model, tea.Cmd) {
nextModel := next()
cmd := nextModel.Init()
return nextModel, cmd
}
opts := inputmodels.TextInputOptions{
Header: "Enter your project name",
Placeholder: proj.Name,
OnEnter: func(input string) error {
proj.Name = input
return nil
},
OnEnterEmpty: func() error {
return nil
},
Next: nextModel,
NextEmpty: nextModel,
}
return inputmodels.NewTextInput(opts)
}
func selectWebLibrary(proj *project.Configuration) tea.Model {
next := func() tea.Model { return selectDBLibrary(proj) }
return selectWebLibraryWithNext(proj, next)
}
func selectWebLibraryWithNext(proj *project.Configuration, next func() tea.Model) tea.Model {
opts := inputmodels.RadioSelectOptions{
Choices: []string{
"Gin",
"Fiber",
"Gorilla/mux",
"net/http (and other compatible libraries)",
"other",
},
Values: []string{
project.WebLibraryGin,
project.WebLibraryFiber,
project.WebLibraryGorillamux,
project.WebLibraryHttp,
project.WebLibraryNone,
},
Header: "Choose your base library",
OnEnter: func(selected string, _ int) error {
switch selected {
case project.WebLibraryGin:
proj.WebLibrary = selected
case project.WebLibraryFiber:
proj.WebLibrary = selected
case project.WebLibraryGorillamux:
proj.WebLibrary = selected
case project.WebLibraryHttp:
proj.WebLibrary = selected
default:
proj.WebLibrary = project.WebLibraryNone
}
return nil
},
Next: nextFunc(next),
}
return inputmodels.NewRadioSelect(opts)
}
func selectDBLibrary(proj *project.Configuration) tea.Model {
next := func() tea.Model { return commonPackages(proj) }
return selectDBLibraryWithNext(proj, next)
}
func selectDBLibraryWithNext(proj *project.Configuration, next func() tea.Model) tea.Model {
selectDBProvider := func() tea.Model {
switch proj.DBLibrary {
case project.DBLibraryGorm:
return selectGormDBProviderWithNext(proj, next)
case project.DBLibrarySql, project.DBLibrarySqlx, project.DBLibrarySqlscan:
return selectDBProviderWithNext(proj, next)
default:
return next()
}
}
opts := inputmodels.RadioSelectOptions{
Header: "Select a db library",
Choices: []string{
"sql",
"sqlscan",
"sqlx (it is recommended to use sqlscan instead, it's better maintained)",
"gorm",
"None",
},
Values: []string{
project.DBLibrarySql,
project.DBLibrarySqlscan,
project.DBLibrarySqlx,
project.DBLibraryGorm,
project.DBLibraryNone,
},
OnEnter: func(selection string, _ int) error {
proj.DBLibrary = selection
return nil
},
Next: func() (tea.Model, tea.Cmd) {
nextModel := selectDBProvider()
cmd := nextModel.Init()
return nextModel, cmd
},
}
return inputmodels.NewRadioSelect(opts)
}
func selectDBProviderWithNext(proj *project.Configuration, next func() tea.Model) tea.Model {
opts := inputmodels.RadioSelectOptions{
Header: "Select a DBMS",
Choices: []string{"PostgreSQL", "MySQL/MariaDB"},
Values: []string{project.DBProviderPostgres, project.DBProviderMysql},
OnEnter: func(selection string, _ int) error {
proj.DBProvider = selection
return nil
},
Next: nextFunc(next),
}
return inputmodels.NewRadioSelect(opts)
}
func selectGormDBProviderWithNext(proj *project.Configuration, next func() tea.Model) tea.Model {
opts := inputmodels.RadioSelectOptions{
Header: "Select a DBMS",
Choices: []string{"PostgreSQL", "MySQL/MariaDB"},
Values: []string{project.DBProviderGormPostgres, project.DBProviderGormMysql},
OnEnter: func(selection string, _ int) error {
proj.DBProvider = selection
return nil
},
Next: nextFunc(next),
}
return inputmodels.NewRadioSelect(opts)
}
func commonPackages(proj *project.Configuration) tea.Model {
next := func() tea.Model { return otherPackages(proj) }
return commonPackagesWithNext(proj, next)
}
func commonPackagesWithNext(proj *project.Configuration, next func() tea.Model) tea.Model {
opts := inputmodels.ChoiceModelOptions{
Choices: []string{
"Use spf13/cobra",
"Use redis",
"Use cleanenv",
"Use MongoDB",
"Use AMQP",
"Use stretchr/testify",
},
Values: []string{
"github.com/spf13/cobra",
"github.com/go-redis/redis",
"github.com/ilyakaznacheev/cleanenv",
"github.com/mongodb/mongo-go-driver/mongo",
"github.com/rabbitmq/amqp091-go",
"github/com/stretchr/testify",
},
Header: "Choose between common packages",
OnEnter: func(selected []inputmodels.Selection) error {
for _, sel := range selected {
if sel.IsSelected {
proj.Dependencies[sel.Value()] = struct{}{}
}
}
return nil
},
Next: nextFunc(next),
}
return inputmodels.NewchoiceModel(opts)
}
func otherPackages(proj *project.Configuration) tea.Model {
next := func() tea.Model { return selectVendoring(proj) }
return otherPackagesWithNext(proj, next)
}
func otherPackagesWithNext(proj *project.Configuration, next func() tea.Model) tea.Model {
opts := inputmodels.TextInputOptions{
Header: "Enter other packages you want to use",
Placeholder: "Leave empty if you don't want to use any other packages",
OnEnter: func(input string) error {
proj.Dependencies[input] = struct{}{}
return nil
},
Next: func() (tea.Model, tea.Cmd) {
next := otherPackagesWithNext(proj, next)
cmd := next.Init()
return next, cmd
},
OnEnterEmpty: func() error { return nil },
NextEmpty: nextFunc(next),
}
return inputmodels.NewTextInput(opts)
}
func selectVendoring(proj *project.Configuration) tea.Model {
next := func() tea.Model { return showSummary(proj, 0) }
return selectVendorigWithNext(proj, next)
}
func selectVendorigWithNext(proj *project.Configuration, next func() tea.Model) tea.Model {
choices := []string{"Yes", "No"}
values := []string{"yes", "no"}
opts := inputmodels.RadioSelectOptions{
Header: "Do you want to use vendoring?",
Choices: choices,
Values: values,
OnEnter: func(selection string, _ int) error {
if selection == values[0] {
proj.DoVendor = true
} else {
proj.DoVendor = false
}
return nil
},
Next: nextFunc(next),
}
return inputmodels.NewRadioSelect(opts)
}
func showSummary(proj *project.Configuration, cursorPosition int) tea.Model {
var next func() tea.Model
choices, values := buildChoicesAndValues(proj)
opts := inputmodels.RadioSelectOptions{
Header: "Summary",
Choices: choices,
CursorStartingPosition: cursorPosition,
Values: values,
OnEnter: func(input string, cursorPosition int) error {
switch {
case input == projName:
next = func() tea.Model {
return projectNameWithNext(proj,
func() tea.Model { return showSummary(proj, cursorPosition) })
}
case input == web:
next = func() tea.Model {
return selectWebLibraryWithNext(proj,
func() tea.Model { return showSummary(proj, cursorPosition) })
}
case input == db:
next = func() tea.Model {
return selectDBLibraryWithNext(proj,
func() tea.Model { return showSummary(proj, cursorPosition) })
}
case strings.HasPrefix(input, removeDep):
dep := strings.TrimPrefix(input, removeDep)
delete(proj.Dependencies, dep)
next = func() tea.Model {
return showSummary(proj, cursorPosition)
}
case input == addDep:
next = func() tea.Model {
return otherPackagesWithNext(proj, func() tea.Model { return showSummary(proj, cursorPosition) })
}
case input == vendor:
proj.DoVendor = !proj.DoVendor
next = func() tea.Model {
return showSummary(proj, cursorPosition)
}
case input == build:
go func() {
err := proj.Start()
if err != nil {
log.Fatal(err)
}
}()
next = func() tea.Model { return buildingBoilerplate(proj) }
default:
return errors.New("unexpected input")
}
return nil
},
// I can't use nextFunc here because I need to the closure with next
Next: func() (tea.Model, tea.Cmd) {
nextModel := next()
cmd := nextModel.Init()
return nextModel, cmd
},
}
return inputmodels.NewRadioSelect(opts)
}
func buildChoicesAndValues(proj *project.Configuration) ([]string, []string) {
choices := []string{}
values := []string{}
choices = append(choices, "Project name: "+proj.Name)
values = append(values, projName)
if proj.WebLibrary != project.WebLibraryNone {
choices = append(choices, "Web library: "+proj.WebLibrary)
} else {
choices = append(choices, "No web library (no helper/util functions will be written)")
}
values = append(values, web)
if proj.DBLibrary != project.DBLibraryNone {
choices = append(choices, "DB Library: "+proj.DBLibrary+", with driver: "+proj.DBProvider)
} else {
choices = append(choices, "No DB library selected (no code example will be generated)")
}
values = append(values, db)
if len(proj.Dependencies) > 0 {
for dep := range proj.Dependencies {
choices = append(choices, "Remove dependency: "+dep)
values = append(values, removeDep+dep)
}
}
choices = append(choices, "Add dependency")
values = append(values, addDep)
choices = append(choices, "Vendoring: "+strconv.FormatBool(proj.DoVendor))
values = append(values, vendor)
choices = append(choices, "Build")
values = append(values, build)
return choices, values
}
func buildingBoilerplate(proj *project.Configuration) tea.Model {
return progressloader.NewLoader(proj)
}
func nextFunc(next func() tea.Model) func() (tea.Model, tea.Cmd) {
return func() (tea.Model, tea.Cmd) {
nextModel := next()
cmd := nextModel.Init()
return nextModel, cmd
}
}