Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
itpey committed Apr 29, 2024
1 parent 8149dc2 commit 7fe439f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion app/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func Create() *cli.App {
Name: "template",
Aliases: []string{"t"},
Usage: "Project template to use",
Value: "default",
Value: "figo-templates_default",
},
},
},
Expand Down
19 changes: 10 additions & 9 deletions app/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,14 @@ import (
"github.com/fatih/color"
)

func extractAllTemplates(sourceDir string) error {
repoName, err := getRepositoryName(sourceDir)
if err != nil {
fmt.Printf(color.RedString("Error: getting repository name for template '%s': %v\n"), sourceDir, err)
}
func extractAllTemplates(sourceDir string, repoName string) error {

if isGoModule(sourceDir) {
destPath := filepath.Join(templatesDirectory, repoName)
if err := copyTemplate(sourceDir, destPath); err != nil {
fmt.Printf(color.RedString("Error:Error copying template '%s': %v\n"), repoName, err)
}
fmt.Printf("Template '%s' extracted successfully\n", repoName)
fmt.Printf(color.GreenString("Template '%s' extracted successfully\n"), repoName)
}

files, err := os.ReadDir(sourceDir)
Expand All @@ -55,7 +51,7 @@ func extractAllTemplates(sourceDir string) error {
// Check if the directory contains a Go module
if isGoModule(templateDir) {
// Get the base directory name (to use as template name)
templateName := fmt.Sprintf("%s/%s", repoName, file.Name())
templateName := fmt.Sprintf("%s_%s", repoName, file.Name())

// Copy template directory to local templates directory
destPath := filepath.Join(templatesDirectory, templateName)
Expand All @@ -64,7 +60,7 @@ func extractAllTemplates(sourceDir string) error {
continue
}

fmt.Printf("Template '%s' extracted successfully\n", templateName)
fmt.Printf(color.GreenString("Template '%s' extracted successfully\n"), templateName)
}
}
}
Expand Down Expand Up @@ -158,13 +154,18 @@ func downloadTemplates(url string) error {

fmt.Print(color.YellowString("Downloading templates from repository: %s ...\n", url))

repoName, err := extractRepoNameFromURL(url)
if err != nil {
return fmt.Errorf("error extracting repository name: %v", err)
}

repoDir := repoDirectory
if err := gitClone(url, repoDir); err != nil {
return fmt.Errorf(color.RedString("Error: cloning repository: %v", err))
}
defer os.RemoveAll(repoDir)

if err := extractAllTemplates(repoDir); err != nil {
if err := extractAllTemplates(repoDir, repoName); err != nil {
return fmt.Errorf(color.RedString("Error: extracting templates: %v", err))
}

Expand Down
34 changes: 19 additions & 15 deletions app/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package app

import (
"encoding/json"
"fmt"
"net/url"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -151,24 +151,28 @@ func isGoModule(dir string) bool {
return true // go.mod file exists
}

// getRepositoryName retrieves the repository name from the Go module
func getRepositoryName(dir string) (string, error) {
cmd := exec.Command("go", "list", "-m", "-json")
cmd.Dir = dir
output, err := cmd.Output()
func extractRepoNameFromURL(repoURL string) (string, error) {
// Parse the repository URL
parsedURL, err := url.Parse(repoURL)
if err != nil {
return "", fmt.Errorf(color.RedString("Error: running 'go list -m -json' in directory '%s': %v", dir, err))
return "", fmt.Errorf(color.RedString("Error: failed to parse repository URL: %v"), err)
}

var moduleInfo struct {
Path string `json:"Path"`
}
if err := json.Unmarshal(output, &moduleInfo); err != nil {
return "", fmt.Errorf(color.RedString("Error: parsing module information from 'go list -m -json' output: %v", err))
// Remove .git extension and split the path into segments
pathSegments := strings.Split(strings.TrimSuffix(parsedURL.Path, ".git"), "/")

// Find the last non-empty segment in the path
var repoName string
for i := len(pathSegments) - 1; i >= 0; i-- {
if pathSegments[i] != "" {
repoName = pathSegments[i]
break
}
}

// Extract the repository name from the module import path
repoURL := moduleInfo.Path
repoName := filepath.Base(repoURL)
// Validate and return the repository name
if repoName == "" {
return "", fmt.Errorf(color.RedString("Error: unable to determine repository name from URL: %s"), repoURL)
}
return repoName, nil
}

0 comments on commit 7fe439f

Please sign in to comment.