Skip to content

Commit

Permalink
Add option to create from the custom back/front templates
Browse files Browse the repository at this point in the history
  • Loading branch information
koddr committed May 18, 2021
1 parent 98f3579 commit 7e27a6f
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 53 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</h1>
<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>

<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>
<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>

## ⚡️ Quick start

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

```bash
cgapp create
cgapp create [OPTION]
```

| Option | Description | Type | Default | Required? |
| ------ | -------------------------------------------------------- | ------ | ------- | --------- |
| `-t` | Enables to define custom backend and frontend templates. | `bool` | `false` | No |

![cgapp_create](https://user-images.githubusercontent.com/11155743/116796937-38160080-aae9-11eb-8e21-fb1be2750aa4.gif)

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

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

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

Expand Down
97 changes: 64 additions & 33 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ import (
"github.com/spf13/cobra"
)

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",
Expand All @@ -34,16 +43,34 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
)

// Start survey.
if err := survey.Ask(
registry.CreateQuestions, &createAnswers, survey.WithIcons(surveyIconsConfig),
); err != nil {
return cgapp.ShowError(err.Error())
}
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 = strings.Replace(createAnswers.Backend, "/", "_", -1)
frontend = createAnswers.Frontend
proxy = createAnswers.Proxy
// 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.Replace(createAnswers.Backend, "/", "_", -1),
)
frontend = createAnswers.Frontend
proxy = createAnswers.Proxy
}

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

// Clone backend files from git repository.
if err := cgapp.GitClone(
"backend",
fmt.Sprintf("github.com/create-go-app/%v-go-template", backend),
); err != nil {
if err := cgapp.GitClone("backend", backend); err != nil {
return cgapp.ShowError(err.Error())
}

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

if frontend != "none" {
// Create frontend files.
if err := cgapp.ExecCommand(
"npm",
[]string{"init", "@vitejs/app", "frontend", "--", "--template", frontend},
true,
); err != nil {
return cgapp.ShowError(err.Error())
// 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 {
// Create a default frontend template from Vite.js.
if err := cgapp.ExecCommand(
"npm",
[]string{"init", "@vitejs/app", "frontend", "--", "--template", frontend},
true,
); err != nil {
return cgapp.ShowError(err.Error())
}
}

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

// Show success report.
cgapp.ShowMessage(
"success",
fmt.Sprintf("Web/Proxy server configuration for `%v` was created!", proxy),
false, false,
)
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.
Expand All @@ -143,7 +177,7 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
// Show success report.
cgapp.ShowMessage(
"success",
"Ansible inventory, playbook and roles for deploying was created!",
"Ansible inventory, playbook and roles for deploying your project was created!",
false, false,
)

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

// Delete unused roles and backend files.
// 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)
Expand All @@ -193,7 +228,7 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
"* Please put credentials into the Ansible inventory file (`hosts.ini`) before you start deploying a project!",
false, false,
)
if frontend != "none" {
if !useCustomTemplate && frontend != "none" {
cgapp.ShowMessage(
"",
fmt.Sprintf("* Visit https://vitejs.dev/guide/ for more info about using the `%v` frontend template!", frontend),
Expand All @@ -202,7 +237,7 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {
}
cgapp.ShowMessage(
"",
"* A helpful documentation and next steps with your project is here https://create-go.app/",
"* A helpful documentation and next steps with your project is here https://create-go.app/wiki",
false, true,
)
cgapp.ShowMessage(
Expand All @@ -213,7 +248,3 @@ func runCreateCmd(cmd *cobra.Command, args []string) error {

return nil
}

func init() {
rootCmd.AddCommand(createCmd)
}
18 changes: 9 additions & 9 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import (
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(deployCmd)
deployCmd.Flags().BoolVarP(
&askBecomePass,
"ask-become-pass", "k", false,
"prompt you to provide the remote user sudo password (standard Ansible `--ask-become-pass` option)",
)
}

// deployCmd represents the `deploy` command.
var deployCmd = &cobra.Command{
Use: "deploy",
Expand Down Expand Up @@ -72,12 +81,3 @@ func runDeployCmd(cmd *cobra.Command, args []string) error {

return nil
}

func init() {
rootCmd.AddCommand(deployCmd)
deployCmd.PersistentFlags().BoolVarP(
&askBecomePass,
"", "K", false,
"prompt you to provide the remote user sudo password (standard Ansible `--ask-become-pass` option)",
)
}
11 changes: 6 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
)

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

// Config for survey icons and colors.
// See: https://github.com/mgutz/ansi#style-format
Expand Down
43 changes: 42 additions & 1 deletion pkg/registry/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// CLIVersion version of Create Go App CLI.
const CLIVersion string = "2.1.1"
const CLIVersion string = "2.2.0"

// Variables struct for Ansible variables (inventory, hosts).
type Variables struct {
Expand Down Expand Up @@ -101,6 +101,47 @@ var (
},
}

// CustomCreateQuestions survey's questions for `create -c` command.
CustomCreateQuestions = []*survey.Question{
{
Name: "backend",
Prompt: &survey.Input{
Message: "Enter URL to the custom backend repository:",
Help: "No need to specify `http://` or `https://` protocol.",
},
Validate: survey.Required,
},
{
Name: "frontend",
Prompt: &survey.Input{
Message: "Enter URL to the custom frontend repository:",
Help: "No need to specify `http://` or `https://` protocol.",
Default: "none",
},
},
{
Name: "proxy",
Prompt: &survey.Select{
Message: "Choose a web/proxy server:",
Options: []string{
"none",
"traefik",
"traefik-acme-dns",
"nginx",
},
Default: "none",
PageSize: 4,
},
},
{
Name: "agree",
Prompt: &survey.Confirm{
Message: "If everything is okay, can I create this project for you? ;)",
Default: true,
},
},
}

// AnsibleInventoryVariables list of variables for inventory.
AnsibleInventoryVariables = map[string]*Variables{
"none": {
Expand Down

0 comments on commit 7e27a6f

Please sign in to comment.