/
create.go
233 lines (202 loc) · 7.1 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
/*
Package cmd includes all of the Create Go App CLI commands.
Create a new production-ready project with backend (Golang),
frontend (JavaScript, TypeScript) and deploy automation
(Ansible, Docker) by running one CLI command.
-> Focus on writing code and thinking of business logic!
<- The Create Go App CLI will take care of the rest.
Copyright © 2019-present Vic Shóstak <truewebartisans@gmail.com> (https://1wa.co)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"os"
"strings"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/create-go-app/cli/pkg/cgapp"
"github.com/create-go-app/cli/pkg/embed"
"github.com/create-go-app/cli/pkg/registry"
"github.com/spf13/cobra"
)
// createCmd represents the `create` command.
var createCmd = &cobra.Command{
Use: "create",
Aliases: []string{"new"},
Short: "Create a new project via interactive UI or configuration file",
Long: "\nCreate a new project via interactive UI or configuration file.",
Run: runCreateCmd,
}
// runCreateCmd represents runner for the `create` command.
var runCreateCmd = func(cmd *cobra.Command, args []string) {
// Start message.
cgapp.SendMsg(true, "* * *", "Create a new project via Create Go App CLI v"+registry.CLIVersion+"...", "yellow", true)
// If config is set and correct, skip survey and use it.
if useConfigFile && projectConfig != nil {
// Re-define variables from config file (default is $PWD/.cgapp.yml).
backend = strings.ToLower(projectConfig["backend"].(string))
frontend = strings.ToLower(projectConfig["frontend"].(string))
webserver = strings.ToLower(projectConfig["webserver"].(string))
database = strings.ToLower(projectConfig["database"].(string))
// Check, if config file contains `roles` section
if rolesConfig != nil {
installAnsibleRoles = true
}
} else {
// Start survey.
if err := survey.Ask(
registry.CreateQuestions, &createAnswers, survey.WithIcons(surveyIconsConfig),
); err != nil {
cgapp.SendMsg(true, "[ERROR]", err.Error(), "red", true)
os.Exit(1)
}
// If something went wrong, cancel and exit.
if !createAnswers.AgreeCreation {
cgapp.SendMsg(true, "[!]", "You're stopped creation of a new project.", "red", false)
cgapp.SendMsg(false, "[!]", "Run `cgapp create` once again!", "red", true)
os.Exit(1)
}
// Insert empty line.
cgapp.SendMsg(false, "", "", "", false)
// Define variables for better display.
backend = strings.ToLower(createAnswers.Backend)
frontend = strings.ToLower(createAnswers.Frontend)
webserver = strings.ToLower(createAnswers.Webserver)
database = strings.ToLower(createAnswers.Database)
installAnsibleRoles = createAnswers.InstallAnsibleRoles
}
// Start timer.
startTimer := time.Now()
// Get current directory.
currentDir, err := os.Getwd()
if err != nil {
cgapp.SendMsg(true, "[ERROR]", err.Error(), "red", true)
os.Exit(1)
}
// Create config files for your project.
cgapp.SendMsg(false, "*", "Create config files for your project...", "cyan", true)
// Create configuration files.
filesToMake := map[string][]byte{
".gitignore": embed.Get("/.gitignore"),
".gitattributes": embed.Get("/.gitattributes"),
".editorconfig": embed.Get("/.editorconfig"),
"Taskfile.yml": embed.Get("/Taskfile.yml"),
}
if err := cgapp.MakeFiles(currentDir, filesToMake); err != nil {
cgapp.SendMsg(true, "[ERROR]", err.Error(), "red", true)
os.Exit(1)
}
// Create Ansible playbook and download roles, if not skipped.
if installAnsibleRoles {
cgapp.SendMsg(true, "*", "Create Ansible playbook and roles...", "cyan", true)
// Create playbook.
fileToMake := map[string][]byte{
"deploy-playbook.yml": embed.Get("/deploy-playbook.yml"),
}
if err := cgapp.MakeFiles(currentDir, fileToMake); err != nil {
cgapp.SendMsg(true, "[ERROR]", err.Error(), "red", true)
os.Exit(1)
}
// Create Ansible roles.
if err := cgapp.CreateProjectFromRegistry(
®istry.Project{
Type: "roles",
Name: "deploy",
RootFolder: currentDir,
},
registry.Repositories,
registry.RegexpAnsiblePattern,
); err != nil {
cgapp.SendMsg(true, "[ERROR]", err.Error(), "red", true)
os.Exit(1)
}
}
// Create backend files.
cgapp.SendMsg(true, "*", "Create project backend...", "cyan", true)
if err := cgapp.CreateProjectFromRegistry(
®istry.Project{
Type: "backend",
Name: backend,
RootFolder: currentDir,
},
registry.Repositories,
registry.RegexpBackendPattern,
); err != nil {
cgapp.SendMsg(true, "[ERROR]", err.Error(), "red", true)
os.Exit(1)
}
if frontend != "none" {
// Create frontend files.
cgapp.SendMsg(true, "*", "Create project frontend...", "cyan", false)
if err := cgapp.CreateProjectFromCmd(
®istry.Project{
Type: "frontend",
Name: frontend,
RootFolder: currentDir,
},
registry.Commands,
registry.RegexpFrontendPattern,
); err != nil {
cgapp.SendMsg(true, "[ERROR]", err.Error(), "red", true)
os.Exit(1)
}
}
// Docker containers.
if webserver != "none" || database != "none" {
cgapp.SendMsg(true, "* * *", "Configuring Docker containers...", "yellow", false)
if webserver != "none" {
// Create container with a web/proxy server.
cgapp.SendMsg(true, "*", "Create container with web/proxy server...", "cyan", true)
if err := cgapp.CreateProjectFromRegistry(
®istry.Project{
Type: "webserver",
Name: webserver,
RootFolder: currentDir,
},
registry.Repositories,
registry.RegexpWebServerPattern,
); err != nil {
cgapp.SendMsg(true, "[ERROR]", err.Error(), "red", true)
os.Exit(1)
}
}
if database != "none" {
// Create container with a database.
cgapp.SendMsg(true, "*", "Create container with database...", "cyan", true)
if err := cgapp.CreateProjectFromRegistry(
®istry.Project{
Type: "database",
Name: database,
RootFolder: currentDir,
},
registry.Repositories,
registry.RegexpDatabasePattern,
); err != nil {
cgapp.SendMsg(true, "[ERROR]", err.Error(), "red", true)
os.Exit(1)
}
}
}
// Stop timer
stopTimer := time.Since(startTimer).String()
// End message.
cgapp.SendMsg(true, "* * *", "Completed in "+stopTimer+"!", "yellow", true)
cgapp.SendMsg(false, "(i)", "A helpful documentation and next steps -> https://create-go.app/", "green", false)
cgapp.SendMsg(false, "(i)", "Run `cgapp deploy` to deploy your project to a remote server.", "green", true)
}
func init() {
rootCmd.AddCommand(createCmd)
createCmd.PersistentFlags().BoolVarP(
&useConfigFile,
"use-config", "c", false,
"use config file to create a new project or deploy to a remote server (default is $PWD/.cgapp.yml)",
)
}