Compare commits

...

5 Commits

Author SHA1 Message Date
haiyux 1c7e75a43a fix lint 1 year ago
haiyux 6db1653a74 fix 1 year ago
haiyux ba3f92dc11 fix lint 1 year ago
haiyux 1be7c93fc4 fix lint 1 year ago
haiyux 0b228bf724 fix kratos new 1 year ago
  1. 25
      cmd/kratos/internal/project/add.go
  2. 39
      cmd/kratos/internal/project/add_test.go
  3. 9
      cmd/kratos/internal/project/new.go
  4. 46
      cmd/kratos/internal/project/project.go
  5. 29
      cmd/kratos/internal/project/project_linux_test.go
  6. 8
      cmd/kratos/internal/project/project_test.go
  7. 30
      cmd/kratos/internal/project/project_windows_test.go

@ -4,7 +4,9 @@ import (
"context"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/fatih/color"
@ -16,9 +18,8 @@ var repoAddIgnores = []string{
".git", ".github", "api", "README.md", "LICENSE", "go.mod", "go.sum", "third_party", "openapi.yaml", ".gitignore",
}
func (p *Project) Add(ctx context.Context, dir string, layout string, branch string, mod string) error {
to := filepath.Join(dir, p.Name)
func (p *Project) Add(ctx context.Context, wd string, layout string, branch string, mod string, modPath string) error {
to := p.Path
if _, err := os.Stat(to); !os.IsNotExist(err) {
fmt.Printf("🚫 %s already exists\n", p.Name)
override := false
@ -39,20 +40,20 @@ func (p *Project) Add(ctx context.Context, dir string, layout string, branch str
fmt.Printf("🚀 Add service %s, layout repo is %s, please wait a moment.\n\n", p.Name, layout)
repo := base.NewRepo(layout, branch)
if err := repo.CopyToV2(ctx, to, filepath.Join(mod, p.Path), repoAddIgnores, []string{filepath.Join(p.Path, "api"), "api"}); err != nil {
subPath := subtractPath(to, modPath)
if err := repo.CopyToV2(ctx, to, path.Join(mod, subPath), repoAddIgnores, []string{filepath.Join(subPath, "api"), "api"}); err != nil {
return err
}
e := os.Rename(
filepath.Join(to, "cmd", "server"),
filepath.Join(to, "cmd", p.Name),
filepath.Join(to, "cmd", path.Base(p.Name)),
)
if e != nil {
return e
}
base.Tree(to, dir)
base.Tree(to, wd)
fmt.Printf("\n🍺 Repository creation succeeded %s\n", color.GreenString(p.Name))
fmt.Print("💻 Use the following command to add a project 👇:\n\n")
@ -65,3 +66,13 @@ func (p *Project) Add(ctx context.Context, dir string, layout string, branch str
fmt.Println(" 📚 Tutorial: https://go-kratos.dev/docs/getting-started/start")
return nil
}
func subtractPath(basePath, subtractPath string) string {
if !strings.HasPrefix(basePath, subtractPath) {
return basePath
}
remainingPath := strings.TrimPrefix(basePath, subtractPath)
remainingPath = strings.TrimPrefix(remainingPath, "/")
return remainingPath
}

@ -0,0 +1,39 @@
package project
import "testing"
func Test_subtractPath(t *testing.T) {
type args struct {
basePath string
subtractPath string
}
tests := []struct {
name string
args args
want string
}{
{
name: "test-1",
args: args{
basePath: "/home/kratos/code/test/a/c",
subtractPath: "/home/kratos/code/test",
},
want: "a/c",
},
{
name: "test-2",
args: args{
basePath: "code/test/go-kratos/kratos/app/user-service",
subtractPath: "code/test/go-kratos/kratos",
},
want: "app/user-service",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := subtractPath(tt.args.basePath, tt.args.subtractPath); got != tt.want {
t.Errorf("subtractPath() = %v, want %v", got, tt.want)
}
})
}
}

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"path"
"path/filepath"
"github.com/AlecAivazis/survey/v2"
@ -19,8 +20,8 @@ type Project struct {
}
// New new a project from remote repo.
func (p *Project) New(ctx context.Context, dir string, layout string, branch string) error {
to := filepath.Join(dir, p.Name)
func (p *Project) New(ctx context.Context, wd string, layout string, branch string) error {
to := p.Path
if _, err := os.Stat(to); !os.IsNotExist(err) {
fmt.Printf("🚫 %s already exists\n", p.Name)
prompt := &survey.Confirm{
@ -44,12 +45,12 @@ func (p *Project) New(ctx context.Context, dir string, layout string, branch str
}
e := os.Rename(
filepath.Join(to, "cmd", "server"),
filepath.Join(to, "cmd", p.Name),
filepath.Join(to, "cmd", path.Base(p.Name)),
)
if e != nil {
return e
}
base.Tree(to, dir)
base.Tree(to, wd)
fmt.Printf("\n🍺 Project creation succeeded %s\n", color.GreenString(p.Name))
fmt.Print("💻 Use the following command to start the project 👇:\n\n")

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

@ -1,29 +0,0 @@
//go:build linux
// +build linux
package project
import (
"testing"
)
func Test_processProjectParams(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 := processProjectParams(tt.args.projectName, tt.args.fallbackPlaceDir); got != tt.want {
t.Errorf("processProjectParams() = %v, want %v", got, tt.want)
}
})
}
}

@ -5,6 +5,7 @@ import (
"go/parser"
"go/token"
"os"
"path"
"path/filepath"
"testing"
@ -50,8 +51,13 @@ func TestCmdNewNoMod(t *testing.T) {
t.Fatalf("executing command: %v", err)
}
// ch to new project
if err := os.Chdir(path.Join(cwd, "project")); err != nil {
t.Fatalf("changing working directory: %v", err)
}
// add new app with --nomod flag
CmdNew.SetArgs([]string{"--nomod", "project/app/user"})
CmdNew.SetArgs([]string{"--nomod", "app/user"})
if err := CmdNew.Execute(); err != nil {
t.Fatalf("executing command: %v", err)
}

@ -1,30 +0,0 @@
//go:build windows
// +build windows
package project
import (
"testing"
)
func Test_processProjectParams(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 := processProjectParams(tt.args.projectName, tt.args.fallbackPlaceDir); got != tt.want {
t.Errorf("getProjectPlaceDir() = %v, want %v", got, tt.want)
}
})
}
}
Loading…
Cancel
Save