Feat project create with dir name add test (#2576)

* fix issue:#2358 Support for creating a project with specifying the name for its place dir

* add test and fixed path of dir

* fixed bug:nomod param not working as expect

* fix test

* fix go1.16 unit test

Co-authored-by: czyt <czyt@w.cn>
pull/2598/head
虫子樱桃 2 years ago committed by GitHub
parent 6f78ccacd9
commit 852a77faa6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      cmd/kratos/internal/project/add.go
  2. 30
      cmd/kratos/internal/project/project.go
  3. 29
      cmd/kratos/internal/project/project_linux_test.go
  4. 30
      cmd/kratos/internal/project/project_windows_test.go

@ -17,7 +17,7 @@ var repoAddIgnores = []string{
} }
func (p *Project) Add(ctx context.Context, dir string, layout string, branch string, mod string) error { func (p *Project) Add(ctx context.Context, dir string, layout string, branch string, mod string) error {
to := path.Join(dir, p.Path) to := path.Join(dir, p.Name)
if _, err := os.Stat(to); !os.IsNotExist(err) { if _, err := os.Stat(to); !os.IsNotExist(err) {
fmt.Printf("🚫 %s already exists\n", p.Name) fmt.Printf("🚫 %s already exists\n", p.Name)

@ -6,6 +6,8 @@ import (
"fmt" "fmt"
"os" "os"
"path" "path"
"path/filepath"
"strings"
"time" "time"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
@ -64,7 +66,8 @@ func run(cmd *cobra.Command, args []string) {
} else { } else {
name = args[0] name = args[0]
} }
p := &Project{Name: path.Base(name), Path: name} wd = getProjectPlaceDir(name, wd)
p := &Project{Name: filepath.Base(name), Path: name}
done := make(chan error, 1) done := make(chan error, 1)
go func() { go func() {
if !nomod { if !nomod {
@ -95,3 +98,28 @@ 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, "~") {
homeDir, err := os.UserHomeDir()
if err != nil {
// cannot get user home return fallback place dir
return fallbackPlaceDir
}
projectFullPath = filepath.Join(homeDir, projectName[2:])
}
// check path is relative
if !filepath.IsAbs(projectFullPath) {
absPath, err := filepath.Abs(projectFullPath)
if err != nil {
return fallbackPlaceDir
}
projectFullPath = absPath
}
// create project logic will check stat,so not check path stat here
return filepath.Dir(projectFullPath)
}

@ -0,0 +1,29 @@
//go:build linux
// +build linux
package project
import (
"testing"
)
func Test_getProjectPlaceDir(t *testing.T) {
type args struct {
projectName string
fallbackPlaceDir string
}
tests := []struct {
name string
args args
want string
}{
{"absLinux", args{projectName: "/home/kratos/awesome/go/demo", fallbackPlaceDir: ""}, "/home/kratos/awesome/go"},
}
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)
}
})
}
}

@ -0,0 +1,30 @@
//go:build windows
// +build windows
package project
import (
"testing"
)
func Test_getProjectPlaceDir(t *testing.T) {
type args struct {
projectName string
fallbackPlaceDir string
}
tests := []struct {
name string
args args
want string
}{
{"absWindows", args{projectName: "c:\\kratos\\awesome\\go\\demo", fallbackPlaceDir: ""}, "c:\\kratos\\awesome\\go"},
//{"relativeWindows", args{projectName: "/home/kratos/awesome/go/demo", fallbackPlaceDir: ""}, "/home/kratos/awesome/go"},
}
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)
}
})
}
}
Loading…
Cancel
Save