Skip to content

Commit 0678dc0

Browse files
authored
Merge pull request #82 from create-go-app/dev
Add option to create a new project from the custom templates
2 parents edc3dc0 + ab4660b commit 0678dc0

File tree

7 files changed

+140
-56
lines changed

7 files changed

+140
-56
lines changed

.goreleaser.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ brews:
4949

5050
# Caveats for the user of your binary.
5151
# Default is empty.
52-
caveats: "Create a new project via interactive console UI into current folder by using `cgapp create` command."
52+
caveats: "Create a new project via interactive console UI into current folder by using `cgapp create` command. A helpful documentation and next steps with your project is here https://create-go.app/"
5353

5454
# Your app's homepage.
5555
# Default is empty.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ delete-tag:
2727
git tag --delete $(VERSION)
2828

2929
update-pkg-cache:
30-
curl -i https://proxy.golang.org/github.com/create-go-app/cli/@v/$(VERSION).info
30+
curl -i https://proxy.golang.org/github.com/create-go-app/cli/v2/@v/$(VERSION).info

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?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: 72 additions & 34 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",
@@ -29,21 +38,46 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
2938
// Start message.
3039
cgapp.ShowMessage(
3140
"",
32-
fmt.Sprintf("Create a new project via Create Go App CLI v%v...", registry.CLIVersion),
41+
fmt.Sprintf(
42+
"Create a new project via Create Go App CLI v%v...",
43+
registry.CLIVersion,
44+
),
3345
true, true,
3446
)
3547

3648
// Start survey.
37-
if err := survey.Ask(
38-
registry.CreateQuestions, &createAnswers, survey.WithIcons(surveyIconsConfig),
39-
); err != nil {
40-
return cgapp.ShowError(err.Error())
41-
}
49+
if useCustomTemplate {
50+
// Custom survey.
51+
if err := survey.Ask(
52+
registry.CustomCreateQuestions,
53+
&customCreateAnswers,
54+
survey.WithIcons(surveyIconsConfig),
55+
); err != nil {
56+
return cgapp.ShowError(err.Error())
57+
}
4258

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

4882
// Start timer.
4983
startTimer := time.Now()
@@ -53,10 +87,7 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
5387
*/
5488

5589
// 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 {
90+
if err := cgapp.GitClone("backend", backend); err != nil {
6091
return cgapp.ShowError(err.Error())
6192
}
6293

@@ -72,13 +103,21 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
72103
*/
73104

74105
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())
106+
// Checking, if use custom templates.
107+
if useCustomTemplate {
108+
// Clone frontend files from git repository.
109+
if err := cgapp.GitClone("frontend", frontend); err != nil {
110+
return cgapp.ShowError(err.Error())
111+
}
112+
} else {
113+
// Create a default frontend template from Vite.js.
114+
if err := cgapp.ExecCommand(
115+
"npm",
116+
[]string{"init", "@vitejs/app", "frontend", "--", "--template", frontend},
117+
true,
118+
); err != nil {
119+
return cgapp.ShowError(err.Error())
120+
}
82121
}
83122

84123
// Show success report.
@@ -119,11 +158,13 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
119158
}
120159

121160
// Show success report.
122-
cgapp.ShowMessage(
123-
"success",
124-
fmt.Sprintf("Web/Proxy server configuration for `%v` was created!", proxy),
125-
false, false,
126-
)
161+
if proxy != "none" {
162+
cgapp.ShowMessage(
163+
"success",
164+
fmt.Sprintf("Web/Proxy server configuration for `%v` was created!", proxy),
165+
false, false,
166+
)
167+
}
127168

128169
/*
129170
The project's Ansible roles part creation.
@@ -143,7 +184,7 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
143184
// Show success report.
144185
cgapp.ShowMessage(
145186
"success",
146-
"Ansible inventory, playbook and roles for deploying was created!",
187+
"Ansible inventory, playbook and roles for deploying your project was created!",
147188
false, false,
148189
)
149190

@@ -175,9 +216,10 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
175216
proxyList = []string{"traefik", "nginx"}
176217
}
177218

178-
// Delete unused roles and backend files.
219+
// Delete unused roles, backend and frontend files.
179220
cgapp.RemoveFolders("roles", proxyList)
180221
cgapp.RemoveFolders("backend", []string{".git", ".github"})
222+
cgapp.RemoveFolders("frontend", []string{".git", ".github"})
181223

182224
// Stop timer.
183225
stopTimer := cgapp.CalculateDurationTime(startTimer)
@@ -193,7 +235,7 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
193235
"* Please put credentials into the Ansible inventory file (`hosts.ini`) before you start deploying a project!",
194236
false, false,
195237
)
196-
if frontend != "none" {
238+
if !useCustomTemplate && frontend != "none" {
197239
cgapp.ShowMessage(
198240
"",
199241
fmt.Sprintf("* Visit https://vitejs.dev/guide/ for more info about using the `%v` frontend template!", frontend),
@@ -202,7 +244,7 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
202244
}
203245
cgapp.ShowMessage(
204246
"",
205-
"* A helpful documentation and next steps with your project is here https://create-go.app/",
247+
"* A helpful documentation and next steps with your project is here https://create-go.app/wiki",
206248
false, true,
207249
)
208250
cgapp.ShowMessage(
@@ -213,7 +255,3 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
213255

214256
return nil
215257
}
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)