Skip to content

Commit

Permalink
Add required CLI tools checker
Browse files Browse the repository at this point in the history
  • Loading branch information
koddr committed Aug 15, 2023
1 parent c571bfa commit 0e0bfd1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
36 changes: 26 additions & 10 deletions cmd/cgapp/cgapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ func main() {
configPath := flag.String("p", "", "set a path (or URL) to the config file")
flag.Parse()

helpers.PrintStyled("👋 Hello and welcome to the Create Go App CLI!", "", "margin-top")
helpers.PrintStyled("👋 Hello and welcome to the Create Go App CLI (v5)!", "", "margin-top")

// Check, if '-init' flag is true.
if *initDefaultConfig {
// Check, if the default config file ('.cgapp.yml') is existing in the current dir.
_, err := os.Stat(".cgapp.yml")
if err == nil {
// If exists, skip a generation process.
helpers.PrintStyled("Please, fix errors:", "error", "margin-top-bottom")
helpers.PrintStyled("Please, fix error(s):", "error", "margin-top-bottom")
helpers.PrintStyled("the configuration file '.cgapp.yml' is found in the current dir, cannot be overwritten (data erasure protection)", "", "margin-left")
helpers.PrintStyled("For more information, see https://github.com/create-go-app/cli/wiki", "warning", "margin-top-bottom")
os.Exit(1)
Expand All @@ -59,21 +59,21 @@ func main() {
// If not exists, get data from embed files.
embedDefaultConfig, err := embed.ConfigsFiles.ReadFile("configs/default.yml")
if err != nil {
helpers.PrintStyled("Please, fix errors:", "error", "margin-top-bottom")
helpers.PrintStyled("Please, fix error(s):", "error", "margin-top-bottom")
helpers.PrintStyled(err.Error(), "", "margin-left")
helpers.PrintStyled("For more information, see https://github.com/create-go-app/cli/wiki", "warning", "margin-top-bottom")
os.Exit(1)
}

// Create a new config file.
if err = helpers.MakeFile(".cgapp.yml", embedDefaultConfig); err != nil {
helpers.PrintStyled("Please, fix errors:", "error", "margin-top-bottom")
helpers.PrintStyled("Please, fix error(s):", "error", "margin-top-bottom")
helpers.PrintStyled(err.Error(), "", "margin-left")
helpers.PrintStyled("For more information, see https://github.com/create-go-app/cli/wiki", "warning", "margin-top-bottom")
os.Exit(1)
}

helpers.PrintStyled("Successfully generated '.cgapp.yml' config file in the current dir!", "success", "margin-top-bottom")
helpers.PrintStyled("Successfully generated '.cgapp.yml' config file in the current dir!", "success", "margin-bottom")
helpers.PrintStyled("Next steps:", "info", "margin-left")
helpers.PrintStyled("Edit config file with your options and parameters", "info", "margin-left-2")
helpers.PrintStyled("Make awesome backend, frontend and setting up proxy", "info", "margin-left-2")
Expand All @@ -82,17 +82,27 @@ func main() {
os.Exit(0)
}

// Check, if required tools (git, npm, docker) was installed on the local system.
if err := helpers.CheckCLITools([]string{"git", "npm", "docker"}); err != nil {
// If not installed, skip a generation process.
helpers.PrintStyled("Please, fix error(s):", "error", "margin-top-bottom")
helpers.PrintStyled(err.Error(), "", "margin-left")
helpers.PrintStyled("For more information, see https://github.com/create-go-app/cli/wiki", "warning", "margin-top-bottom")
os.Exit(1)
}

helpers.PrintStyled("Successfully checked required tools (git, npm, docker) on your local system!", "success", "margin-top")
helpers.PrintStyled(fmt.Sprintf("Analyzing the given configuration file ('%s')...", *configPath), "info", "margin-top")

// Check, if '-config' flag has path (or URL).
// Check, if '-p' flag has path (or URL).
if *configPath == "" {
helpers.PrintStyled("Config path (or URL) is not set, try to found '.cgapp.yml' file in the current dir...", "info", "margin-top")

// Check, if the default config file ('.cgapp.yml') is existing in the current dir.
_, err := os.Stat(".cgapp.yml")
if err != nil {
// If not exists, skip an initialization process.
helpers.PrintStyled("Please, fix errors:", "error", "margin-top-bottom")
helpers.PrintStyled("Please, fix error(s):", "error", "margin-top-bottom")
helpers.PrintStyled("the configuration file '.cgapp.yml' is not found", "", "margin-left")
helpers.PrintStyled("For more information, see https://github.com/create-go-app/cli/wiki", "warning", "margin-top-bottom")
os.Exit(1)
Expand All @@ -107,7 +117,7 @@ func main() {
// Initialize app with config path.
app, err := initialize(*configPath)

Check failure on line 118 in cmd/cgapp/cgapp.go

View workflow job for this annotation

GitHub Actions / build (stable, ubuntu-latest)

undefined: initialize
if err != nil {
helpers.PrintStyled("Please, fix errors:", "error", "margin-top-bottom")
helpers.PrintStyled("Please, fix error(s):", "error", "margin-top-bottom")
helpers.PrintStyled(err.Error(), "", "margin-left")
helpers.PrintStyled("For more information, see https://github.com/create-go-app/cli/wiki", "warning", "margin-top-bottom")
os.Exit(1)
Expand All @@ -121,12 +131,17 @@ func main() {

// Create a new project.
if err = app.Create(); err != nil {
helpers.PrintStyled("Please, fix errors:", "error", "margin-top-bottom")
helpers.PrintStyled("Please, fix error(s):", "error", "margin-top-bottom")
helpers.PrintStyled(err.Error(), "", "margin-left")
helpers.PrintStyled("For more information, see https://github.com/create-go-app/cli/wiki", "warning", "margin-top-bottom")
os.Exit(1)
}

helpers.PrintStyled("Successfully created project in the current dir!", "success", "margin-top-bottom")
helpers.PrintStyled("Next steps:", "info", "margin-left")
helpers.PrintStyled("Make awesome backend, frontend and setting up proxy", "info", "margin-left-2")
helpers.PrintStyled("Run deploy process for your project", "info", "margin-left-2")
helpers.PrintStyled("For more information, see https://github.com/create-go-app/cli/wiki", "warning", "margin-top-bottom")
os.Exit(0)
}

Expand All @@ -136,12 +151,13 @@ func main() {

// Deploy project to your remote host.
if err = app.Deploy(); err != nil {
helpers.PrintStyled("Please, fix errors:", "error", "margin-top-bottom")
helpers.PrintStyled("Please, fix error(s):", "error", "margin-top-bottom")
helpers.PrintStyled(err.Error(), "", "margin-left")
helpers.PrintStyled("For more information, see https://github.com/create-go-app/cli/wiki", "warning", "margin-top-bottom")
os.Exit(1)
}

helpers.PrintStyled("Successfully deployed project to the remote server!", "success", "margin-top-bottom")
os.Exit(0)
}
}
14 changes: 6 additions & 8 deletions internal/helpers/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import (
// MakeFile function for single file create.
func MakeFile(name string, data []byte) error {
// Check, if file is existing.
fileInfo, err := os.Stat(name)
if err != nil && !fileInfo.IsDir() {
return err
}

// Write to created file.
if err = os.WriteFile(name, data, 0o644); err != nil {
return err
_, err := os.Stat(name)
if err != nil {
// Write to created file.
if err = os.WriteFile(name, data, 0o644); err != nil {
return err
}
}

return nil
Expand Down

0 comments on commit 0e0bfd1

Please sign in to comment.