-
Notifications
You must be signed in to change notification settings - Fork 244
/
actions.go
160 lines (133 loc) · 4.67 KB
/
actions.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
package cgapp
import (
"strings"
"time"
"github.com/create-go-app/cli/internal/embed"
"github.com/urfave/cli/v2"
)
// CreateCLIAction action for `create` CLI command
func CreateCLIAction(c *cli.Context) error {
// Start timer
startTimer := time.Now()
// START message
SendMsg(true, "*", "Create Go App v"+version, "yellow", false)
SendMsg(true, "START", "Creating a new project in `"+appPath+"` folder...", "green", false)
// Create main folder for app
SendMsg(true, "WAIT", "Create project folder and config files", "cyan", false)
if err := MakeFolder(appPath, 0750); err != nil {
return ThrowError(err.Error())
}
// Create configuration files
filesToMake := map[string][]byte{
".gitignore": embed.Get("/.gitignore"),
".editorconfig": embed.Get("/.editorconfig"),
"Taskfile.yml": embed.Get("/Taskfile.yml"),
}
if err := MakeFiles(appPath, filesToMake); err != nil {
return ThrowError(err.Error())
}
// Create Ansible playbook and download roles, if not skipped
if !c.Bool("skip-ansible-roles") {
SendMsg(true, "WAIT", "Create Ansible playbook and roles", "cyan", false)
// Create playbook
fileToMake := map[string][]byte{
"deploy-playbook.yml": embed.Get("/deploy-playbook.yml"),
}
if err := MakeFiles(appPath, fileToMake); err != nil {
return ThrowError(err.Error())
}
// Create Ansible roles
if err := CreateProjectFromRegistry(
&Project{Type: "roles", Name: "deploy", RootFolder: appPath},
registry,
); err != nil {
return ThrowError(err.Error())
}
}
// Create backend files
SendMsg(true, "WAIT", "Create project backend", "cyan", false)
if err := CreateProjectFromRegistry(
&Project{Type: "backend", Name: strings.ToLower(appBackend), RootFolder: appPath},
registry,
); err != nil {
return ThrowError(err.Error())
}
if appFrontend != "none" {
// Create frontend files
SendMsg(true, "WAIT", "Create project frontend", "cyan", false)
if err := CreateProjectFromCMD(
&Project{Type: "frontend", Name: strings.ToLower(appFrontend), RootFolder: appPath},
cmds,
); err != nil {
return ThrowError(err.Error())
}
}
// Docker containers
if appWebServer != "none" || appDatabase != "none" {
SendMsg(true, "NEXT", "Configuring Docker containers...", "yellow", false)
if appWebServer != "none" {
// Create container with web/proxy server
SendMsg(true, "WAIT", "Create container with web/proxy server", "cyan", false)
if err := CreateProjectFromRegistry(
&Project{Type: "webserver", Name: strings.ToLower(appWebServer), RootFolder: appPath},
registry,
); err != nil {
return ThrowError(err.Error())
}
}
if appDatabase != "none" {
// Create container with database
SendMsg(true, "WAIT", "Create container with database", "cyan", false)
if err := CreateProjectFromRegistry(
&Project{Type: "database", Name: strings.ToLower(appDatabase), RootFolder: appPath},
registry,
); err != nil {
return ThrowError(err.Error())
}
}
}
// Stop timer
stopTimer := time.Since(startTimer).String()
// END message
SendMsg(true, "FINISH", "Completed in "+stopTimer+"!", "green", false)
SendMsg(true, "DOCS", "A helpful documentation here → https://create-go.app", "yellow", false)
SendMsg(false, "!", "Go to the `"+appPath+"` folder and make something beautiful! :)", "yellow", true)
return nil
}
// DeployCLIAction action for `deploy` CLI command
func DeployCLIAction(c *cli.Context) error {
// Start timer
startTimer := time.Now()
// START message
SendMsg(true, "*", "Create Go App v"+version, "yellow", false)
SendMsg(true, "START", "Deploying project to the `"+deployHost+"`...", "green", false)
// Create main folder for app
SendMsg(true, "WAIT", "Run Ansible playbook `"+deployPlaybook+"`", "cyan", true)
// Collect options
options := []string{
deployPlaybook,
"-u", deployUsername,
"-e", "host=" + deployHost + " network_name=" + deployDockerNetwork,
}
// Check, if need to ask password for user
// See: https://docs.ansible.com/ansible/latest/user_guide/become.html#become-command-line-options
if c.Bool("ask-become-pass") {
options = []string{
deployPlaybook,
"-u", deployUsername,
"-e", "host=" + deployHost + " network_name=" + deployDockerNetwork,
"--ask-become-pass",
}
}
// Collect command line
if err := ExecCommand("ansible-playbook", options); err != nil {
return ThrowError(err.Error())
}
// Stop timer
stopTimer := time.Since(startTimer).String()
// END message
SendMsg(true, "FINISH", "Completed in "+stopTimer+".", "green", false)
SendMsg(true, "DOCS", "A helpful documentation here → https://create-go.app", "yellow", false)
SendMsg(false, "!", "Go to the `"+deployHost+"` to see your deployed project! :)", "yellow", true)
return nil
}