diff --git a/cmd/kratos/internal/project/add.go b/cmd/kratos/internal/project/add.go index df5eff11025..33dd07b6962 100644 --- a/cmd/kratos/internal/project/add.go +++ b/cmd/kratos/internal/project/add.go @@ -16,7 +16,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, dir string, layout string, branch string, mod string) error { +func (p *Project) Add(ctx context.Context, dir string, layout string, branch string, mod string, pkgPath string) error { to := filepath.Join(dir, p.Name) if _, err := os.Stat(to); !os.IsNotExist(err) { @@ -38,9 +38,10 @@ 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) + pkgPath = fmt.Sprintf("%s/%s", mod, pkgPath) 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 { + err := repo.CopyToV2(ctx, to, pkgPath, repoAddIgnores, []string{filepath.Join(p.Path, "api"), "api"}) + if err != nil { return err } diff --git a/cmd/kratos/internal/project/project.go b/cmd/kratos/internal/project/project.go index ef3bcad800d..1f3b657e3bc 100644 --- a/cmd/kratos/internal/project/project.go +++ b/cmd/kratos/internal/project/project.go @@ -79,11 +79,12 @@ func run(_ *cobra.Command, args []string) { return } - p.Path, err = filepath.Rel(projectRoot, filepath.Join(workingDir, projectName)) - if err != nil { + packagePath, e := filepath.Rel(projectRoot, filepath.Join(workingDir, projectName)) + if e != nil { done <- fmt.Errorf("🚫 failed to get relative path: %v", err) return } + packagePath = strings.ReplaceAll(packagePath, "\\", "/") mod, e := base.ModulePath(filepath.Join(projectRoot, "go.mod")) if e != nil { @@ -92,7 +93,7 @@ func run(_ *cobra.Command, args []string) { } // 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) + done <- p.Add(ctx, workingDir, repoURL, branch, mod, packagePath) }() select { case <-ctx.Done():