|
|
|
@ -5,8 +5,8 @@ import ( |
|
|
|
|
"errors" |
|
|
|
|
"fmt" |
|
|
|
|
"os" |
|
|
|
|
"path" |
|
|
|
|
"path/filepath" |
|
|
|
|
"strings" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
"github.com/AlecAivazis/survey/v2" |
|
|
|
@ -65,34 +65,27 @@ func run(_ *cobra.Command, args []string) { |
|
|
|
|
} else { |
|
|
|
|
name = args[0] |
|
|
|
|
} |
|
|
|
|
projectName, workingDir := processProjectParams(name, wd) |
|
|
|
|
p := &Project{Name: projectName} |
|
|
|
|
p := &Project{Name: name} |
|
|
|
|
done := make(chan error, 1) |
|
|
|
|
go func() { |
|
|
|
|
if !nomod { |
|
|
|
|
done <- p.New(ctx, workingDir, repoURL, branch) |
|
|
|
|
p.Path = path.Join(wd, path.Base(name)) |
|
|
|
|
done <- p.New(ctx, wd, repoURL, branch) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
projectRoot := getgomodProjectRoot(workingDir) |
|
|
|
|
projectRoot := getgomodProjectRoot(wd) |
|
|
|
|
if gomodIsNotExistIn(projectRoot) { |
|
|
|
|
done <- fmt.Errorf("🚫 go.mod don't exists in %s", projectRoot) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
p.Path, err = filepath.Rel(projectRoot, filepath.Join(workingDir, projectName)) |
|
|
|
|
if err != nil { |
|
|
|
|
done <- fmt.Errorf("🚫 failed to get relative path: %v", err) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
mod, e := base.ModulePath(filepath.Join(projectRoot, "go.mod")) |
|
|
|
|
if e != nil { |
|
|
|
|
done <- fmt.Errorf("🚫 failed to parse `go.mod`: %v", e) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
// Get the relative path for adding a project based on Go modules
|
|
|
|
|
p.Path = filepath.Join(strings.TrimPrefix(workingDir, projectRoot+"/"), p.Name) |
|
|
|
|
done <- p.Add(ctx, workingDir, repoURL, branch, mod) |
|
|
|
|
p.Path = filepath.Join(wd, p.Name) |
|
|
|
|
done <- p.Add(ctx, wd, repoURL, branch, mod, projectRoot) |
|
|
|
|
}() |
|
|
|
|
select { |
|
|
|
|
case <-ctx.Done(): |
|
|
|
@ -108,31 +101,6 @@ func run(_ *cobra.Command, args []string) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func processProjectParams(projectName string, workingDir string) (projectNameResult, workingDirResult string) { |
|
|
|
|
_projectDir := projectName |
|
|
|
|
_workingDir := workingDir |
|
|
|
|
// Process ProjectName with system variable
|
|
|
|
|
if strings.HasPrefix(projectName, "~") { |
|
|
|
|
homeDir, err := os.UserHomeDir() |
|
|
|
|
if err != nil { |
|
|
|
|
// cannot get user home return fallback place dir
|
|
|
|
|
return _projectDir, _workingDir |
|
|
|
|
} |
|
|
|
|
_projectDir = filepath.Join(homeDir, projectName[2:]) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// check path is relative
|
|
|
|
|
if !filepath.IsAbs(projectName) { |
|
|
|
|
absPath, err := filepath.Abs(projectName) |
|
|
|
|
if err != nil { |
|
|
|
|
return _projectDir, _workingDir |
|
|
|
|
} |
|
|
|
|
_projectDir = absPath |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return filepath.Base(_projectDir), filepath.Dir(_projectDir) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func getgomodProjectRoot(dir string) string { |
|
|
|
|
if dir == filepath.Dir(dir) { |
|
|
|
|
return dir |
|
|
|
|