Skip to content

Commit

Permalink
fix(kratos cli): kratos cli gives wrong import path when creating app…
Browse files Browse the repository at this point in the history
… with --nomod parameter (#2938)

* fix: kratos cli gives wrong import path when creating app with --nomod parameter

* style(kratos): solute lint error.

* style(kratos): solute lint error in project.go.
  • Loading branch information
qzwxsaedc committed Aug 7, 2023
1 parent 79e862d commit d2b1119
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
7 changes: 4 additions & 3 deletions cmd/kratos/internal/project/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/kratos/internal/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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():
Expand Down

0 comments on commit d2b1119

Please sign in to comment.