-
Notifications
You must be signed in to change notification settings - Fork 246
/
create.go
289 lines (253 loc) · 6.91 KB
/
create.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
// Copyright 2022 Vic Shóstak and Create Go App Contributors. All rights reserved.
// Use of this source code is governed by Apache 2.0 license
// that can be found in the LICENSE file.
package cmd
import (
"fmt"
"strings"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
"github.com/create-go-app/cli/v3/pkg/cgapp"
"github.com/create-go-app/cli/v3/pkg/registry"
)
func init() {
rootCmd.AddCommand(createCmd)
createCmd.Flags().BoolVarP(
&useCustomTemplate,
"template", "t", false,
"enables to use custom backend and frontend templates",
)
}
// createCmd represents the `create` command.
var createCmd = &cobra.Command{
Use: "create",
Aliases: []string{"new"},
Short: "Create a new project via interactive UI",
Long: "\nCreate a new project via interactive UI.",
RunE: runCreateCmd,
}
// runCreateCmd represents runner for the `create` command.
func runCreateCmd(cmd *cobra.Command, args []string) error {
// Start message.
cgapp.ShowMessage(
"",
fmt.Sprintf(
"Create a new project via Create Go App CLI v%v...",
registry.CLIVersion,
),
true, true,
)
// Start survey.
if useCustomTemplate {
// Custom survey.
if err := survey.Ask(
registry.CustomCreateQuestions,
&customCreateAnswers,
survey.WithIcons(surveyIconsConfig),
); err != nil {
return cgapp.ShowError(err.Error())
}
// Define variables for better display.
backend = customCreateAnswers.Backend
frontend = customCreateAnswers.Frontend
proxy = customCreateAnswers.Proxy
} else {
// Default survey.
if err := survey.Ask(
registry.CreateQuestions,
&createAnswers,
survey.WithIcons(surveyIconsConfig),
); err != nil {
return cgapp.ShowError(err.Error())
}
// Define variables for better display.
backend = fmt.Sprintf(
"github.com/create-go-app/%v-go-template",
strings.ReplaceAll(createAnswers.Backend, "/", "_"),
)
frontend = createAnswers.Frontend
proxy = createAnswers.Proxy
}
// Catch the cancel action (hit "n" in the last question).
if !createAnswers.AgreeCreation {
cgapp.ShowMessage(
"",
"Oh no! You said \"no\", so I won't create anything. Hope to see you soon!",
true, true,
)
return nil
}
// Start timer.
startTimer := time.Now()
/*
The project's backend part creation.
*/
// Clone backend files from git repository.
if err := cgapp.GitClone("backend", backend); err != nil {
return cgapp.ShowError(err.Error())
}
// Show success report.
cgapp.ShowMessage(
"success",
fmt.Sprintf("Backend was created with template `%v`!", backend),
true, false,
)
/*
The project's frontend part creation.
*/
if frontend != "none" {
// Checking, if use custom templates.
if useCustomTemplate {
// Clone frontend files from git repository.
if err := cgapp.GitClone("frontend", frontend); err != nil {
return cgapp.ShowError(err.Error())
}
} else {
switch {
case frontend == "next" || frontend == "next-ts":
var isTypeScript string
if frontend == "next-ts" {
isTypeScript = "--typescript"
}
// Create a default frontend template with Next.js (React).
if err := cgapp.ExecCommand(
"npx", []string{"create-next-app@latest", "frontend", isTypeScript}, true,
); err != nil {
return err
}
case frontend == "nuxt3":
// Create a default frontend template with Nuxt 3 (Vue.js 3, TypeScript).
if err := cgapp.ExecCommand(
"npx", []string{"nuxi", "init", "frontend"}, true,
); err != nil {
return err
}
default:
// Create a default frontend template from Vite (Pure JS/TS, React, Preact, Vue, Svelte, Lit).
if err := cgapp.ExecCommand(
"npm", []string{"init", "vite@latest", "frontend", "--", "--template", frontend}, true,
); err != nil {
return err
}
}
}
// Show success report.
cgapp.ShowMessage(
"success",
fmt.Sprintf("Frontend was created with template `%v`!", frontend),
false, false,
)
}
/*
The project's webserver part creation.
*/
// Copy Ansible playbook, inventory and roles from embedded file system.
if err := cgapp.CopyFromEmbeddedFS(
&cgapp.EmbeddedFileSystem{
Name: registry.EmbedTemplates,
RootFolder: "templates",
SkipDir: true,
},
); err != nil {
return cgapp.ShowError(err.Error())
}
// Set template variables for Ansible playbook and inventory files.
inventory = registry.AnsibleInventoryVariables[proxy].List
playbook = registry.AnsiblePlaybookVariables[proxy].List
// Generate Ansible inventory file.
if err := cgapp.GenerateFileFromTemplate("hosts.ini.tmpl", inventory); err != nil {
return cgapp.ShowError(err.Error())
}
// Generate Ansible playbook file.
if err := cgapp.GenerateFileFromTemplate("playbook.yml.tmpl", playbook); err != nil {
return cgapp.ShowError(err.Error())
}
// Show success report.
if proxy != "none" {
cgapp.ShowMessage(
"success",
fmt.Sprintf("Web/Proxy server configuration for `%v` was created!", proxy),
false, false,
)
}
/*
The project's Ansible roles part creation.
*/
// Copy Ansible roles from embedded file system.
if err := cgapp.CopyFromEmbeddedFS(
&cgapp.EmbeddedFileSystem{
Name: registry.EmbedRoles,
RootFolder: "roles",
SkipDir: false,
},
); err != nil {
return cgapp.ShowError(err.Error())
}
// Show success report.
cgapp.ShowMessage(
"success",
"Ansible inventory, playbook and roles for deploying your project was created!",
false, false,
)
/*
The project's misc files part creation.
*/
// Copy from embedded file system.
if err := cgapp.CopyFromEmbeddedFS(
&cgapp.EmbeddedFileSystem{
Name: registry.EmbedMiscFiles,
RootFolder: "misc",
SkipDir: true,
},
); err != nil {
return cgapp.ShowError(err.Error())
}
/*
Cleanup project.
*/
// Set unused proxy roles.
switch proxy {
case "traefik", "traefik-acme-dns":
proxyList = []string{"nginx"}
case "nginx":
proxyList = []string{"traefik"}
default:
proxyList = []string{"traefik", "nginx"}
}
// Delete unused roles, backend and frontend files.
cgapp.RemoveFolders("roles", proxyList)
cgapp.RemoveFolders("backend", []string{".git", ".github"})
cgapp.RemoveFolders("frontend", []string{".git", ".github"})
// Stop timer.
stopTimer := cgapp.CalculateDurationTime(startTimer)
cgapp.ShowMessage(
"info",
fmt.Sprintf("Completed in %v seconds!", stopTimer),
true, true,
)
// Ending messages.
cgapp.ShowMessage(
"",
"* Please put credentials into the Ansible inventory file (`hosts.ini`) before you start deploying a project!",
false, false,
)
if !useCustomTemplate && frontend != "none" {
cgapp.ShowMessage(
"",
fmt.Sprintf("* Visit https://vitejs.dev/guide/ for more info about using the `%v` frontend template!", frontend),
false, false,
)
}
cgapp.ShowMessage(
"",
"* A helpful documentation and next steps with your project is here https://create-go.app/wiki",
false, true,
)
cgapp.ShowMessage(
"",
"Have a happy new project! :)",
false, true,
)
return nil
}