From ae4dd7f4a8f29394ab8b3663de11cb4a5d944031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=99=AB=E5=AD=90=E6=A8=B1=E6=A1=83?= Date: Mon, 13 Mar 2023 17:01:12 +0800 Subject: [PATCH] fix: refactor project creation params process (#2714) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add http.ResponseController type alias * refactor:project creation * Delete codec_go1.20.go --------- Co-authored-by: czyt Co-authored-by: 包子 --- cmd/kratos/internal/project/project.go | 38 +++++++++---------- .../internal/project/project_linux_test.go | 6 +-- .../internal/project/project_windows_test.go | 4 +- 3 files changed, 24 insertions(+), 24 deletions(-) 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) } })