Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
itpey committed May 2, 2024
1 parent e59cf5d commit db7d0e9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
18 changes: 16 additions & 2 deletions app/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:"))
Expand Down
18 changes: 17 additions & 1 deletion app/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -245,18 +257,22 @@ 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)
}
case keyboard.KeyArrowDown:
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:
Expand Down
2 changes: 1 addition & 1 deletion app/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit db7d0e9

Please sign in to comment.