From 70e3692698e057606bab1e053b0577cf8eadad48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8C=85=E5=AD=90?= Date: Sun, 4 Jul 2021 00:08:08 +0800 Subject: [PATCH] feat(cmd): add kratos run command and clean code (#1145) * feat(cmd): add kratos run command and clean code --- README.md | 5 +- README_zh.md | 5 +- cmd/kratos/internal/base/get.go | 4 +- cmd/kratos/internal/change/change.go | 6 +- cmd/kratos/internal/run/run.go | 89 ++++++++++++++++++++++++++++ cmd/kratos/main.go | 5 +- 6 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 cmd/kratos/internal/run/run.go diff --git a/README.md b/README.md index fbe11ffc6..97fc836c8 100644 --- a/README.md +++ b/README.md @@ -79,10 +79,9 @@ kratos proto server api/helloworld/helloworld.proto -t internal/service # generate all proto source code, wire, etc. go generate ./... -# compile -go build -o ./bin/ ./... + # run -./bin/helloworld -conf ./configs +kratos run ``` ### Kratos Boot diff --git a/README_zh.md b/README_zh.md index 5febb45d3..4eccdba0f 100644 --- a/README_zh.md +++ b/README_zh.md @@ -79,10 +79,9 @@ kratos proto server api/helloworld/helloworld.proto -t internal/service # 生成所有proto源码、wire等等 go generate ./... -# 编译成可执行文件 -go build -o ./bin/ ./... + # 运行程序 -./bin/helloworld -conf ./configs +kratos run ``` ### Kratos Boot diff --git a/cmd/kratos/internal/base/get.go b/cmd/kratos/internal/base/get.go index 0cbe99d5b..d00f7b11d 100644 --- a/cmd/kratos/internal/base/get.go +++ b/cmd/kratos/internal/base/get.go @@ -77,7 +77,7 @@ func (g *GithubApi) GetCommitsInfo() []CommitInfo { page := 1 var list []CommitInfo for { - url := fmt.Sprintf("https://api.github.com/repos/%s/%s/commits?page=%d&since=%s", g.Owner, g.Repo, page, info.PublishedAt) + url := fmt.Sprintf("https://api.github.com/repos/%s/%s/commits?pre_page=100&page=%d&since=%s", g.Owner, g.Repo, page, info.PublishedAt) resp, code := requestGithubAPI(url, "GET", nil, g.Token) if code != 200 { printGithubErrorInfo(resp) @@ -88,7 +88,7 @@ func (g *GithubApi) GetCommitsInfo() []CommitInfo { fatal(err) } list = append(list, res...) - if len(res) < 30 || len(res) == 0 { + if len(res) < 100 { break } page++ diff --git a/cmd/kratos/internal/change/change.go b/cmd/kratos/internal/change/change.go index 3548e8c35..3c132c117 100644 --- a/cmd/kratos/internal/change/change.go +++ b/cmd/kratos/internal/change/change.go @@ -8,8 +8,8 @@ import ( "github.com/spf13/cobra" ) -// CmdNew represents the new command. -var CmdNew = &cobra.Command{ +// CmdChange is kratos change log tool +var CmdChange = &cobra.Command{ Use: "changelog", Short: "Get a kratos change log", Long: "Get a kratos release or commits info. Example: kratos changelog dev or kratos changelog {version}", @@ -22,7 +22,7 @@ func init() { if repoURL = os.Getenv("KRATOS_REPO"); repoURL == "" { repoURL = "https://github.com/go-kratos/kratos.git" } - CmdNew.Flags().StringVarP(&repoURL, "repo-url", "r", repoURL, "github repo") + CmdChange.Flags().StringVarP(&repoURL, "repo-url", "r", repoURL, "github repo") token = os.Getenv("GITHUB_TOKEN") } diff --git a/cmd/kratos/internal/run/run.go b/cmd/kratos/internal/run/run.go new file mode 100644 index 000000000..7e1ff467f --- /dev/null +++ b/cmd/kratos/internal/run/run.go @@ -0,0 +1,89 @@ +package run + +import ( + "fmt" + "io/ioutil" + "os" + "os/exec" + "path" + "path/filepath" + "strings" + + "github.com/AlecAivazis/survey/v2" + "github.com/spf13/cobra" +) + +// CmdRun run project command. +var CmdRun = &cobra.Command{ + Use: "run", + Short: "run project", + Long: "run project. Example: kratos run", + Run: Run, +} + +// Run run project. +func Run(cmd *cobra.Command, args []string) { + var dir string + if len(args) > 0 { + dir = args[0] + } + base, err := os.Getwd() + if err != nil { + fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err) + return + } + if dir == "" { + // find the directory containing the cmd/* + cmdDir, err := findCMD() + if err != nil { + fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err) + return + } + if len(cmdDir) == 0 { + fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", "The cmd directory cannot be found in the current directory") + return + } else if len(cmdDir) == 1 { + dir = path.Join(base, cmdDir[0]) + } else { + prompt := &survey.Select{ + Message: "Which directory do you want to run?", + Options: cmdDir, + } + survey.AskOne(prompt, &dir) + dir = path.Join(base, dir) + } + } + fd := exec.Command("go", "run", ".") + fd.Stdout = os.Stdout + fd.Stderr = os.Stderr + fd.Dir = dir + if err := fd.Run(); err != nil { + fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err.Error()) + return + } + return +} + +func findCMD() ([]string, error) { + var hasCMDPaths []string + err := filepath.Walk(".", func(walkPath string, info os.FileInfo, err error) error { + // multi level directory is not allowed under the cmd directory, so it is judged that the path ends with cmd. + if strings.HasSuffix(walkPath, "cmd") { + paths, err := ioutil.ReadDir(walkPath) + if err != nil { + return err + } + for _, fileInfo := range paths { + if fileInfo.IsDir() { + hasCMDPaths = append(hasCMDPaths, path.Join(walkPath, fileInfo.Name())) + } + } + return nil + } + return nil + }) + if err != nil { + return []string{}, err + } + return hasCMDPaths, err +} diff --git a/cmd/kratos/main.go b/cmd/kratos/main.go index b5c077f25..33a3229e2 100644 --- a/cmd/kratos/main.go +++ b/cmd/kratos/main.go @@ -6,8 +6,8 @@ import ( "github.com/go-kratos/kratos/cmd/kratos/v2/internal/change" "github.com/go-kratos/kratos/cmd/kratos/v2/internal/project" "github.com/go-kratos/kratos/cmd/kratos/v2/internal/proto" + "github.com/go-kratos/kratos/cmd/kratos/v2/internal/run" "github.com/go-kratos/kratos/cmd/kratos/v2/internal/upgrade" - "github.com/spf13/cobra" ) @@ -26,7 +26,8 @@ func init() { rootCmd.AddCommand(project.CmdNew) rootCmd.AddCommand(proto.CmdProto) rootCmd.AddCommand(upgrade.CmdUpgrade) - rootCmd.AddCommand(change.CmdNew) + rootCmd.AddCommand(change.CmdChange) + rootCmd.AddCommand(run.CmdRun) } func main() {