fix kratos new

fix/cmd
haiyux 1 year ago
parent fcd3b18e83
commit 0b228bf724
  1. 12
      cmd/kratos/internal/project/add.go
  2. 9
      cmd/kratos/internal/project/new.go
  3. 47
      cmd/kratos/internal/project/project.go

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"path"
"path/filepath"
"github.com/AlecAivazis/survey/v2"
@ -16,9 +17,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) error {
to := p.Path
if _, err := os.Stat(to); !os.IsNotExist(err) {
fmt.Printf("🚫 %s already exists\n", p.Name)
override := false
@ -40,19 +40,19 @@ func (p *Project) Add(ctx context.Context, dir string, layout string, branch str
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 {
if err := repo.CopyToV2(ctx, to, mod, repoAddIgnores, []string{filepath.Join(p.Path, "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")

@ -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,28 @@ 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)
fmt.Println(wd, p.Name, p.Path)
done <- p.Add(ctx, wd, repoURL, branch, mod)
}()
select {
case <-ctx.Done():
@ -108,31 +102,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

Loading…
Cancel
Save