diff --git a/cmd/kratos/internal/project/project.go b/cmd/kratos/internal/project/project.go index 9abc98b75..ce0e1ed42 100644 --- a/cmd/kratos/internal/project/project.go +++ b/cmd/kratos/internal/project/project.go @@ -66,15 +66,15 @@ func run(cmd *cobra.Command, args []string) { } else { name = args[0] } - wd = getProjectPlaceDir(name, wd) - p := &Project{Name: filepath.Base(name), Path: name} + projectName, workingDir := processProjectParams(name, wd) + p := &Project{Name: projectName, Path: projectName} done := make(chan error, 1) go func() { if !nomod { - done <- p.New(ctx, wd, repoURL, branch) + done <- p.New(ctx, workingDir, repoURL, branch) return } - projectRoot := getgomodProjectRoot(wd) + projectRoot := getgomodProjectRoot(workingDir) if gomodIsNotExistIn(projectRoot) { done <- fmt.Errorf("🚫 go.mod don't exists in %s", projectRoot) return @@ -84,7 +84,7 @@ func run(cmd *cobra.Command, args []string) { if e != nil { panic(e) } - done <- p.Add(ctx, wd, repoURL, branch, mod) + done <- p.Add(ctx, workingDir, repoURL, branch, mod) }() select { case <-ctx.Done(): @@ -100,29 +100,29 @@ func run(cmd *cobra.Command, args []string) { } } -func getProjectPlaceDir(projectName string, fallbackPlaceDir string) string { - projectFullPath := projectName - - wd := filepath.Dir(projectName) - // check for home dir - if strings.HasPrefix(wd, "~") { +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 fallbackPlaceDir + return _projectDir, _workingDir } - projectFullPath = filepath.Join(homeDir, projectName[2:]) + _projectDir = filepath.Join(homeDir, projectName[2:]) } + // check path is relative - if !filepath.IsAbs(projectFullPath) { - absPath, err := filepath.Abs(projectFullPath) + if !filepath.IsAbs(projectName) { + absPath, err := filepath.Abs(projectName) if err != nil { - return fallbackPlaceDir + return _projectDir, _workingDir } - projectFullPath = absPath + _projectDir = absPath } - // create project logic will check stat,so not check path stat here - return filepath.Dir(projectFullPath) + + return filepath.Base(_projectDir), filepath.Dir(_projectDir) } func getgomodProjectRoot(dir string) string { diff --git a/cmd/kratos/internal/project/project_linux_test.go b/cmd/kratos/internal/project/project_linux_test.go index 271e13534..868cdf026 100644 --- a/cmd/kratos/internal/project/project_linux_test.go +++ b/cmd/kratos/internal/project/project_linux_test.go @@ -7,7 +7,7 @@ import ( "testing" ) -func Test_getProjectPlaceDir(t *testing.T) { +func Test_processProjectParams(t *testing.T) { type args struct { projectName string fallbackPlaceDir string @@ -21,8 +21,8 @@ func Test_getProjectPlaceDir(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := getProjectPlaceDir(tt.args.projectName, tt.args.fallbackPlaceDir); got != tt.want { - t.Errorf("getProjectPlaceDir() = %v, want %v", got, tt.want) + if _, got := processProjectParams(tt.args.projectName, tt.args.fallbackPlaceDir); got != tt.want { + t.Errorf("processProjectParams() = %v, want %v", got, tt.want) } }) } diff --git a/cmd/kratos/internal/project/project_windows_test.go b/cmd/kratos/internal/project/project_windows_test.go index ea60473ae..c86baf375 100644 --- a/cmd/kratos/internal/project/project_windows_test.go +++ b/cmd/kratos/internal/project/project_windows_test.go @@ -7,7 +7,7 @@ import ( "testing" ) -func Test_getProjectPlaceDir(t *testing.T) { +func Test_processProjectParams(t *testing.T) { type args struct { projectName string fallbackPlaceDir string @@ -22,7 +22,7 @@ func Test_getProjectPlaceDir(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := getProjectPlaceDir(tt.args.projectName, tt.args.fallbackPlaceDir); got != tt.want { + if _, got := processProjectParams(tt.args.projectName, tt.args.fallbackPlaceDir); got != tt.want { t.Errorf("getProjectPlaceDir() = %v, want %v", got, tt.want) } })