fix/cmd
haiyux 1 year ago
parent ba3f92dc11
commit 6db1653a74
  1. 2
      cmd/kratos/internal/base/path.go
  2. 17
      cmd/kratos/internal/project/add.go
  3. 39
      cmd/kratos/internal/project/add_test.go
  4. 2
      cmd/kratos/internal/project/project.go
  5. 8
      cmd/kratos/internal/project/project_test.go

@ -55,7 +55,7 @@ func copyFile(src, dst string, replaces []string) error {
return os.WriteFile(dst, buf, srcinfo.Mode())
}
func copyDir(src, dst string, replaces, ignores []string) error {
func copyDir(src, dst string, replaces, ignores []string) error {
srcinfo, err := os.Stat(src)
if err != nil {
return err

@ -6,6 +6,7 @@ import (
"os"
"path"
"path/filepath"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/fatih/color"
@ -17,7 +18,7 @@ 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, wd string, layout string, branch string, mod string) error {
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)
@ -39,8 +40,8 @@ func (p *Project) Add(ctx context.Context, wd string, layout string, branch stri
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, mod, 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
}
@ -65,3 +66,13 @@ func (p *Project) Add(ctx context.Context, wd string, layout string, branch stri
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)
}
})
}
}

@ -85,7 +85,7 @@ func run(_ *cobra.Command, args []string) {
return
}
p.Path = filepath.Join(wd, p.Name)
done <- p.Add(ctx, wd, repoURL, branch, mod)
done <- p.Add(ctx, wd, repoURL, branch, mod, projectRoot)
}()
select {
case <-ctx.Done():

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

Loading…
Cancel
Save