From db7d0e92090f11e174f2cc18bfabce7b5381ad77 Mon Sep 17 00:00:00 2001 From: itpey Date: Thu, 2 May 2024 18:19:01 +0330 Subject: [PATCH] update --- app/cli.go | 18 ++++++++++++++++-- app/template.go | 18 +++++++++++++++++- app/util.go | 2 +- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/app/cli.go b/app/cli.go index 19445cf..8a69f39 100644 --- a/app/cli.go +++ b/app/cli.go @@ -57,7 +57,15 @@ func Create() *cli.App { } if len(templates) == 0 { - return fmt.Errorf(color.RedString("Error: no templates found in", templatesDirectory)) + if err := downloadTemplates(defaultTemplatesRepoURL); err != nil { + return err + } + templates, err = listTemplates() + if err != nil { + return err + } + + clearConsole() } selectedTemplate, err := selectTemplate(templates) @@ -112,7 +120,13 @@ func Create() *cli.App { } if len(templates) == 0 { - return fmt.Errorf(color.RedString("Error: no templates found in", templatesDirectory)) + if err := downloadTemplates(defaultTemplatesRepoURL); err != nil { + return err + } + templates, err = listTemplates() + if err != nil { + return err + } } fmt.Println(color.YellowString("Available templates:")) diff --git a/app/template.go b/app/template.go index 98c50cd..d335f2e 100644 --- a/app/template.go +++ b/app/template.go @@ -156,7 +156,7 @@ func downloadTemplates(url string) error { repoName, err := extractRepoNameFromURL(url) if err != nil { - return fmt.Errorf("error extracting repository name: %v", err) + return fmt.Errorf(color.RedString("Error: extracting repository name: %v"), err) } repoDir := repoDirectory @@ -204,6 +204,17 @@ func deleteTemplateByName(templateName string) error { } func listTemplates() ([]string, error) { + + if _, err := os.Stat(templatesDirectory); os.IsNotExist(err) { + if err := os.MkdirAll(templatesDirectory, 0755); err != nil { + return nil, fmt.Errorf(color.RedString("Error: creating templates directory: %v", err)) + } + if err := downloadTemplates(defaultTemplatesRepoURL); err != nil { + return nil, err + } + + } + var templates []string files, err := os.ReadDir(templatesDirectory) @@ -222,6 +233,7 @@ func listTemplates() ([]string, error) { func selectTemplate(templates []string) (string, error) { clearConsole() + fmt.Print(color.CyanString(appNameArt)) fmt.Println(color.YellowString("Select a template:")) currentIndex := 0 @@ -245,6 +257,8 @@ func selectTemplate(templates []string) (string, error) { if currentIndex > 0 { currentIndex-- clearConsole() + fmt.Print(color.CyanString(appNameArt)) + fmt.Println(color.YellowString("Select a template:")) printTemplateOptions(templates, currentIndex) } @@ -252,11 +266,13 @@ func selectTemplate(templates []string) (string, error) { if currentIndex < len(templates)-1 { currentIndex++ clearConsole() + fmt.Print(color.CyanString(appNameArt)) fmt.Println(color.YellowString("Select a template:")) printTemplateOptions(templates, currentIndex) } case keyboard.KeyEnter: clearConsole() + fmt.Print(color.CyanString(appNameArt)) selectedTemplate := templates[currentIndex] return selectedTemplate, nil case keyboard.KeyEsc: diff --git a/app/util.go b/app/util.go index 7bb1c83..7844ca3 100644 --- a/app/util.go +++ b/app/util.go @@ -49,7 +49,7 @@ func getDefaultDirectory(name string) string { func runCommand(command string, args []string, projectPath string, description string) error { cmd := exec.Command(command, args...) cmd.Dir = projectPath - fmt.Printf("Running %s ...\n", description) + fmt.Printf("Running %s...\n", description) // Execute the command output, err := cmd.CombinedOutput()