fix: refactor project creation params process (#2714)

* add http.ResponseController type alias

* refactor:project creation

* Delete codec_go1.20.go

---------

Co-authored-by: czyt <czyt@w.cn>
Co-authored-by: 包子 <baozhecheng@foxmail.com>
pull/2730/head
虫子樱桃 2 years ago committed by GitHub
parent 33cb4576e9
commit ae4dd7f4a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 38
      cmd/kratos/internal/project/project.go
  2. 6
      cmd/kratos/internal/project/project_linux_test.go
  3. 4
      cmd/kratos/internal/project/project_windows_test.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 {

@ -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)
}
})
}

@ -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)
}
})

Loading…
Cancel
Save