feat(cmd): add kratos run command and clean code (#1145)

* feat(cmd): add kratos run command and clean code
pull/1146/head
包子 3 years ago committed by GitHub
parent f10605fd51
commit 70e3692698
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      README.md
  2. 5
      README_zh.md
  3. 4
      cmd/kratos/internal/base/get.go
  4. 6
      cmd/kratos/internal/change/change.go
  5. 89
      cmd/kratos/internal/run/run.go
  6. 5
      cmd/kratos/main.go

@ -79,10 +79,9 @@ kratos proto server api/helloworld/helloworld.proto -t internal/service
# generate all proto source code, wire, etc. # generate all proto source code, wire, etc.
go generate ./... go generate ./...
# compile
go build -o ./bin/ ./...
# run # run
./bin/helloworld -conf ./configs kratos run
``` ```
### Kratos Boot ### Kratos Boot

@ -79,10 +79,9 @@ kratos proto server api/helloworld/helloworld.proto -t internal/service
# 生成所有proto源码、wire等等 # 生成所有proto源码、wire等等
go generate ./... go generate ./...
# 编译成可执行文件
go build -o ./bin/ ./...
# 运行程序 # 运行程序
./bin/helloworld -conf ./configs kratos run
``` ```
### Kratos Boot ### Kratos Boot

@ -77,7 +77,7 @@ func (g *GithubApi) GetCommitsInfo() []CommitInfo {
page := 1 page := 1
var list []CommitInfo var list []CommitInfo
for { 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) resp, code := requestGithubAPI(url, "GET", nil, g.Token)
if code != 200 { if code != 200 {
printGithubErrorInfo(resp) printGithubErrorInfo(resp)
@ -88,7 +88,7 @@ func (g *GithubApi) GetCommitsInfo() []CommitInfo {
fatal(err) fatal(err)
} }
list = append(list, res...) list = append(list, res...)
if len(res) < 30 || len(res) == 0 { if len(res) < 100 {
break break
} }
page++ page++

@ -8,8 +8,8 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// CmdNew represents the new command. // CmdChange is kratos change log tool
var CmdNew = &cobra.Command{ var CmdChange = &cobra.Command{
Use: "changelog", Use: "changelog",
Short: "Get a kratos change log", Short: "Get a kratos change log",
Long: "Get a kratos release or commits info. Example: kratos changelog dev or kratos changelog {version}", 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 == "" { if repoURL = os.Getenv("KRATOS_REPO"); repoURL == "" {
repoURL = "https://github.com/go-kratos/kratos.git" 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") token = os.Getenv("GITHUB_TOKEN")
} }

@ -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
}

@ -6,8 +6,8 @@ import (
"github.com/go-kratos/kratos/cmd/kratos/v2/internal/change" "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/project"
"github.com/go-kratos/kratos/cmd/kratos/v2/internal/proto" "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/go-kratos/kratos/cmd/kratos/v2/internal/upgrade"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -26,7 +26,8 @@ func init() {
rootCmd.AddCommand(project.CmdNew) rootCmd.AddCommand(project.CmdNew)
rootCmd.AddCommand(proto.CmdProto) rootCmd.AddCommand(proto.CmdProto)
rootCmd.AddCommand(upgrade.CmdUpgrade) rootCmd.AddCommand(upgrade.CmdUpgrade)
rootCmd.AddCommand(change.CmdNew) rootCmd.AddCommand(change.CmdChange)
rootCmd.AddCommand(run.CmdRun)
} }
func main() { func main() {

Loading…
Cancel
Save