Skip to content

Commit 7e27a6f

Browse files
committed
Add option to create from the custom back/front templates
1 parent 98f3579 commit 7e27a6f

File tree

5 files changed

+130
-53
lines changed

5 files changed

+130
-53
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</h1>
55
<p align="center">Create a new production-ready project with <b>backend</b> (Golang), <b>frontend</b> (JavaScript, TypeScript)<br/>and <b>deploy automation</b> (Ansible, Docker) by running one CLI command.<br/><br/>Focus on <b>writing</b> code and <b>thinking</b> of business-logic! The CLI will take care of the rest.</p>
66

7-
<p align="center"><a href="https://github.com/create-go-app/cli/releases" target="_blank"><img src="https://img.shields.io/badge/version-v2.1.1-blue?style=for-the-badge&logo=none" alt="cli version" /></a>&nbsp;<a href="https://pkg.go.dev/github.com/create-go-app/cli/v2?tab=doc" target="_blank"><img src="https://img.shields.io/badge/Go-1.16+-00ADD8?style=for-the-badge&logo=go" alt="go version" /></a>&nbsp;<a href="https://gocover.io/github.com/create-go-app/cli/pkg/cgapp" target="_blank"><img src="https://img.shields.io/badge/Go_Cover-89%25-success?style=for-the-badge&logo=none" alt="go cover" /></a>&nbsp;<a href="https://goreportcard.com/report/github.com/create-go-app/cli" target="_blank"><img src="https://img.shields.io/badge/Go_report-A+-success?style=for-the-badge&logo=none" alt="go report" /></a>&nbsp;<img src="https://img.shields.io/badge/license-apache_2.0-red?style=for-the-badge&logo=none" alt="license" /></p>
7+
<p align="center"><a href="https://github.com/create-go-app/cli/releases" target="_blank"><img src="https://img.shields.io/badge/version-v2.2.0-blue?style=for-the-badge&logo=none" alt="cli version" /></a>&nbsp;<a href="https://pkg.go.dev/github.com/create-go-app/cli/v2?tab=doc" target="_blank"><img src="https://img.shields.io/badge/Go-1.16+-00ADD8?style=for-the-badge&logo=go" alt="go version" /></a>&nbsp;<a href="https://gocover.io/github.com/create-go-app/cli/pkg/cgapp" target="_blank"><img src="https://img.shields.io/badge/Go_Cover-89%25-success?style=for-the-badge&logo=none" alt="go cover" /></a>&nbsp;<a href="https://goreportcard.com/report/github.com/create-go-app/cli" target="_blank"><img src="https://img.shields.io/badge/Go_report-A+-success?style=for-the-badge&logo=none" alt="go report" /></a>&nbsp;<img src="https://img.shields.io/badge/license-apache_2.0-red?style=for-the-badge&logo=none" alt="license" /></p>
88

99
## ⚡️ Quick start
1010

@@ -61,9 +61,13 @@ The best way to better explore all the features of the **Create Go App CLI** is
6161
CLI command for create a new project with the interactive console UI.
6262

6363
```bash
64-
cgapp create
64+
cgapp create [OPTION]
6565
```
6666

67+
| Option | Description | Type | Default | Required? |
68+
| ------ | -------------------------------------------------------- | ------ | ------- | --------- |
69+
| `-t` | Enables to define custom backend and frontend templates. | `bool` | `false` | No |
70+
6771
![cgapp_create](https://user-images.githubusercontent.com/11155743/116796937-38160080-aae9-11eb-8e21-fb1be2750aa4.gif)
6872

6973
- 📺 Full demo video: https://recordit.co/OQAwkZBrjN
@@ -79,9 +83,9 @@ CLI command for deploy Docker containers with your project via Ansible to the re
7983
cgapp deploy [OPTION]
8084
```
8185

82-
| Option | Description | Type | Default | Required? |
83-
| ------ | ------------------------------------------------------------------------------------------------------ | --------- | ------- | --------- |
84-
| `-K` | Prompt you to provide the remote user sudo password (_a standard Ansible `--ask-become-pass` option_). | `boolean` | `false` | No |
86+
| Option | Description | Type | Default | Required? |
87+
| ------ | ------------------------------------------------------------------------------------------------------ | ------ | ------- | --------- |
88+
| `-k` | Prompt you to provide the remote user sudo password (_a standard Ansible `--ask-become-pass` option_). | `bool` | `false` | No |
8589

8690
![cgapp_deploy](https://user-images.githubusercontent.com/11155743/116796941-3c421e00-aae9-11eb-9575-d72550814d7a.gif)
8791

cmd/create.go

Lines changed: 64 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ import (
1515
"github.com/spf13/cobra"
1616
)
1717

18+
func init() {
19+
rootCmd.AddCommand(createCmd)
20+
createCmd.Flags().BoolVarP(
21+
&useCustomTemplate,
22+
"template", "t", false,
23+
"enables to use custom backend and frontend templates",
24+
)
25+
}
26+
1827
// createCmd represents the `create` command.
1928
var createCmd = &cobra.Command{
2029
Use: "create",
@@ -34,16 +43,34 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
3443
)
3544

3645
// Start survey.
37-
if err := survey.Ask(
38-
registry.CreateQuestions, &createAnswers, survey.WithIcons(surveyIconsConfig),
39-
); err != nil {
40-
return cgapp.ShowError(err.Error())
41-
}
46+
if useCustomTemplate {
47+
// Custom survey.
48+
if err := survey.Ask(
49+
registry.CustomCreateQuestions, &customCreateAnswers, survey.WithIcons(surveyIconsConfig),
50+
); err != nil {
51+
return cgapp.ShowError(err.Error())
52+
}
4253

43-
// Define variables for better display.
44-
backend = strings.Replace(createAnswers.Backend, "/", "_", -1)
45-
frontend = createAnswers.Frontend
46-
proxy = createAnswers.Proxy
54+
// Define variables for better display.
55+
backend = customCreateAnswers.Backend
56+
frontend = customCreateAnswers.Frontend
57+
proxy = customCreateAnswers.Proxy
58+
} else {
59+
// Default survey.
60+
if err := survey.Ask(
61+
registry.CreateQuestions, &createAnswers, survey.WithIcons(surveyIconsConfig),
62+
); err != nil {
63+
return cgapp.ShowError(err.Error())
64+
}
65+
66+
// Define variables for better display.
67+
backend = fmt.Sprintf(
68+
"github.com/create-go-app/%v-go-template",
69+
strings.Replace(createAnswers.Backend, "/", "_", -1),
70+
)
71+
frontend = createAnswers.Frontend
72+
proxy = createAnswers.Proxy
73+
}
4774

4875
// Start timer.
4976
startTimer := time.Now()
@@ -53,10 +80,7 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
5380
*/
5481

5582
// Clone backend files from git repository.
56-
if err := cgapp.GitClone(
57-
"backend",
58-
fmt.Sprintf("github.com/create-go-app/%v-go-template", backend),
59-
); err != nil {
83+
if err := cgapp.GitClone("backend", backend); err != nil {
6084
return cgapp.ShowError(err.Error())
6185
}
6286

@@ -72,13 +96,21 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
7296
*/
7397

7498
if frontend != "none" {
75-
// Create frontend files.
76-
if err := cgapp.ExecCommand(
77-
"npm",
78-
[]string{"init", "@vitejs/app", "frontend", "--", "--template", frontend},
79-
true,
80-
); err != nil {
81-
return cgapp.ShowError(err.Error())
99+
// Checking, if use custom templates.
100+
if useCustomTemplate {
101+
// Clone frontend files from git repository.
102+
if err := cgapp.GitClone("frontend", frontend); err != nil {
103+
return cgapp.ShowError(err.Error())
104+
}
105+
} else {
106+
// Create a default frontend template from Vite.js.
107+
if err := cgapp.ExecCommand(
108+
"npm",
109+
[]string{"init", "@vitejs/app", "frontend", "--", "--template", frontend},
110+
true,
111+
); err != nil {
112+
return cgapp.ShowError(err.Error())
113+
}
82114
}
83115

84116
// Show success report.
@@ -119,11 +151,13 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
119151
}
120152

121153
// Show success report.
122-
cgapp.ShowMessage(
123-
"success",
124-
fmt.Sprintf("Web/Proxy server configuration for `%v` was created!", proxy),
125-
false, false,
126-
)
154+
if proxy != "none" {
155+
cgapp.ShowMessage(
156+
"success",
157+
fmt.Sprintf("Web/Proxy server configuration for `%v` was created!", proxy),
158+
false, false,
159+
)
160+
}
127161

128162
/*
129163
The project's Ansible roles part creation.
@@ -143,7 +177,7 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
143177
// Show success report.
144178
cgapp.ShowMessage(
145179
"success",
146-
"Ansible inventory, playbook and roles for deploying was created!",
180+
"Ansible inventory, playbook and roles for deploying your project was created!",
147181
false, false,
148182
)
149183

@@ -175,9 +209,10 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
175209
proxyList = []string{"traefik", "nginx"}
176210
}
177211

178-
// Delete unused roles and backend files.
212+
// Delete unused roles, backend and frontend files.
179213
cgapp.RemoveFolders("roles", proxyList)
180214
cgapp.RemoveFolders("backend", []string{".git", ".github"})
215+
cgapp.RemoveFolders("frontend", []string{".git", ".github"})
181216

182217
// Stop timer.
183218
stopTimer := cgapp.CalculateDurationTime(startTimer)
@@ -193,7 +228,7 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
193228
"* Please put credentials into the Ansible inventory file (`hosts.ini`) before you start deploying a project!",
194229
false, false,
195230
)
196-
if frontend != "none" {
231+
if !useCustomTemplate && frontend != "none" {
197232
cgapp.ShowMessage(
198233
"",
199234
fmt.Sprintf("* Visit https://vitejs.dev/guide/ for more info about using the `%v` frontend template!", frontend),
@@ -202,7 +237,7 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
202237
}
203238
cgapp.ShowMessage(
204239
"",
205-
"* A helpful documentation and next steps with your project is here https://create-go.app/",
240+
"* A helpful documentation and next steps with your project is here https://create-go.app/wiki",
206241
false, true,
207242
)
208243
cgapp.ShowMessage(
@@ -213,7 +248,3 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
213248

214249
return nil
215250
}
216-
217-
func init() {
218-
rootCmd.AddCommand(createCmd)
219-
}

cmd/deploy.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ import (
1313
"github.com/spf13/cobra"
1414
)
1515

16+
func init() {
17+
rootCmd.AddCommand(deployCmd)
18+
deployCmd.Flags().BoolVarP(
19+
&askBecomePass,
20+
"ask-become-pass", "k", false,
21+
"prompt you to provide the remote user sudo password (standard Ansible `--ask-become-pass` option)",
22+
)
23+
}
24+
1625
// deployCmd represents the `deploy` command.
1726
var deployCmd = &cobra.Command{
1827
Use: "deploy",
@@ -72,12 +81,3 @@ func runDeployCmd(cmd *cobra.Command, args []string) error {
7281

7382
return nil
7483
}
75-
76-
func init() {
77-
rootCmd.AddCommand(deployCmd)
78-
deployCmd.PersistentFlags().BoolVarP(
79-
&askBecomePass,
80-
"", "K", false,
81-
"prompt you to provide the remote user sudo password (standard Ansible `--ask-become-pass` option)",
82-
)
83-
}

cmd/root.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ import (
1111
)
1212

1313
var (
14-
backend, frontend, proxy string // define project variables
15-
inventory, playbook map[string]interface{} // define template variables
16-
options, proxyList []string // define options, proxy list
17-
askBecomePass bool // install Ansible roles, ask become pass
18-
createAnswers registry.CreateAnswers // define answers variable for `create` command
14+
backend, frontend, proxy string // define project variables
15+
inventory, playbook map[string]interface{} // define template variables
16+
options, proxyList []string // define options, proxy list
17+
useCustomTemplate bool // define custom templates
18+
askBecomePass bool // install Ansible roles, ask become pass
19+
createAnswers, customCreateAnswers registry.CreateAnswers // define answers variable for `create` command
1920

2021
// Config for survey icons and colors.
2122
// See: https://github.com/mgutz/ansi#style-format

pkg/registry/defaults.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
// CLIVersion version of Create Go App CLI.
14-
const CLIVersion string = "2.1.1"
14+
const CLIVersion string = "2.2.0"
1515

1616
// Variables struct for Ansible variables (inventory, hosts).
1717
type Variables struct {
@@ -101,6 +101,47 @@ var (
101101
},
102102
}
103103

104+
// CustomCreateQuestions survey's questions for `create -c` command.
105+
CustomCreateQuestions = []*survey.Question{
106+
{
107+
Name: "backend",
108+
Prompt: &survey.Input{
109+
Message: "Enter URL to the custom backend repository:",
110+
Help: "No need to specify `http://` or `https://` protocol.",
111+
},
112+
Validate: survey.Required,
113+
},
114+
{
115+
Name: "frontend",
116+
Prompt: &survey.Input{
117+
Message: "Enter URL to the custom frontend repository:",
118+
Help: "No need to specify `http://` or `https://` protocol.",
119+
Default: "none",
120+
},
121+
},
122+
{
123+
Name: "proxy",
124+
Prompt: &survey.Select{
125+
Message: "Choose a web/proxy server:",
126+
Options: []string{
127+
"none",
128+
"traefik",
129+
"traefik-acme-dns",
130+
"nginx",
131+
},
132+
Default: "none",
133+
PageSize: 4,
134+
},
135+
},
136+
{
137+
Name: "agree",
138+
Prompt: &survey.Confirm{
139+
Message: "If everything is okay, can I create this project for you? ;)",
140+
Default: true,
141+
},
142+
},
143+
}
144+
104145
// AnsibleInventoryVariables list of variables for inventory.
105146
AnsibleInventoryVariables = map[string]*Variables{
106147
"none": {

0 commit comments

Comments
 (0)