From 9a98fcb04fbe33cfb2661f779b4d651921141096 Mon Sep 17 00:00:00 2001 From: haiyux Date: Wed, 27 Apr 2022 20:12:44 +0800 Subject: [PATCH] feat:new and add karge warehouse (#1953) * feat:new and add karge warehouse --- cmd/kratos/internal/base/repo.go | 13 +++++ cmd/kratos/internal/project/add.go | 66 ++++++++++++++++++++++++++ cmd/kratos/internal/project/project.go | 18 ++++++- 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 cmd/kratos/internal/project/add.go diff --git a/cmd/kratos/internal/base/repo.go b/cmd/kratos/internal/base/repo.go index 86d4a1546..cd7b650f0 100644 --- a/cmd/kratos/internal/base/repo.go +++ b/cmd/kratos/internal/base/repo.go @@ -114,3 +114,16 @@ func (r *Repo) CopyTo(ctx context.Context, to string, modPath string, ignores [] } return copyDir(r.Path(), to, []string{mod, modPath}, ignores) } + +// CopyToV2 copies the repository to project path +func (r *Repo) CopyToV2(ctx context.Context, to string, modPath string, ignores, replaces []string) error { + if err := r.Clone(ctx); err != nil { + return err + } + mod, err := ModulePath(path.Join(r.Path(), "go.mod")) + if err != nil { + return err + } + replaces = append([]string{mod, modPath}, replaces...) + return copyDir(r.Path(), to, replaces, ignores) +} diff --git a/cmd/kratos/internal/project/add.go b/cmd/kratos/internal/project/add.go new file mode 100644 index 000000000..98661cd1d --- /dev/null +++ b/cmd/kratos/internal/project/add.go @@ -0,0 +1,66 @@ +package project + +import ( + "context" + "fmt" + "os" + "path" + + "github.com/AlecAivazis/survey/v2" + "github.com/fatih/color" + "github.com/go-kratos/kratos/cmd/kratos/v2/internal/base" +) + +var repoAddIgnores = []string{ + ".git", ".github", "api", "README.md", "LICENSE", "go.mod", "go.sum", "third_party", +} + +func (p *Project) Add(ctx context.Context, dir string, layout string, branch string, mod string) error { + to := path.Join(dir, p.Path) + + if _, err := os.Stat(to); !os.IsNotExist(err) { + fmt.Printf("šŸš« %s already exists\n", p.Name) + override := false + prompt := &survey.Confirm{ + Message: "šŸ“‚ Do you want to override the folder ?", + Help: "Delete the existing folder and create the project.", + } + e := survey.AskOne(prompt, &override) + if e != nil { + return e + } + if !override { + return err + } + os.RemoveAll(to) + } + + 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, path.Join(mod, p.Path), repoAddIgnores, []string{path.Join(p.Path, "api"), "api"}); err != nil { + return err + } + + e := os.Rename( + path.Join(to, "cmd", "server"), + path.Join(to, "cmd", p.Name), + ) + if e != nil { + return e + } + + base.Tree(to, dir) + + fmt.Printf("\nšŸŗ Repository creation succeeded %s\n", color.GreenString(p.Name)) + fmt.Print("šŸ’» Use the following command to add a project šŸ‘‡:\n\n") + + fmt.Println(color.WhiteString("$ cd %s", p.Name)) + fmt.Println(color.WhiteString("$ go generate ./...")) + fmt.Println(color.WhiteString("$ go build -o ./bin/ ./... ")) + fmt.Println(color.WhiteString("$ ./bin/%s -conf ./configs\n", p.Name)) + fmt.Println(" šŸ¤ Thanks for using Kratos") + fmt.Println(" šŸ“š Tutorial: https://go-kratos.dev/docs/getting-started/start") + return nil +} diff --git a/cmd/kratos/internal/project/project.go b/cmd/kratos/internal/project/project.go index 0abc45775..bfad066bf 100644 --- a/cmd/kratos/internal/project/project.go +++ b/cmd/kratos/internal/project/project.go @@ -9,6 +9,7 @@ import ( "time" "github.com/AlecAivazis/survey/v2" + "github.com/go-kratos/kratos/cmd/kratos/v2/internal/base" "github.com/spf13/cobra" ) @@ -24,6 +25,7 @@ var ( repoURL string branch string timeout string + nomod bool ) func init() { @@ -34,6 +36,7 @@ func init() { CmdNew.Flags().StringVarP(&repoURL, "repo-url", "r", repoURL, "layout repo") CmdNew.Flags().StringVarP(&branch, "branch", "b", branch, "repo branch") CmdNew.Flags().StringVarP(&timeout, "timeout", "t", timeout, "time out") + CmdNew.Flags().BoolVarP(&nomod, "nomod", "", nomod, "retain go mod") } func run(cmd *cobra.Command, args []string) { @@ -63,7 +66,20 @@ func run(cmd *cobra.Command, args []string) { p := &Project{Name: path.Base(name), Path: name} done := make(chan error, 1) go func() { - done <- p.New(ctx, wd, repoURL, branch) + if !nomod { + done <- p.New(ctx, wd, repoURL, branch) + } else { + if _, e := os.Stat(path.Join(wd, "go.mod")); os.IsNotExist(e) { + done <- fmt.Errorf("šŸš« go.mod don't exists in %s", wd) + return + } + + mod, e := base.ModulePath(path.Join(wd, "go.mod")) + if e != nil { + panic(e) + } + done <- p.Add(ctx, wd, repoURL, branch, mod) + } }() select { case <-ctx.Done():