fix: Fix lint of files in cmd/kratos (#1442)

pull/1449/head
yuemoxi 3 years ago committed by GitHub
parent f5339e3ea3
commit 6ca81236b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      cmd/kratos/internal/base/path.go
  2. 4
      cmd/kratos/internal/base/repo.go
  3. 11
      cmd/kratos/internal/change/change.go
  4. 18
      cmd/kratos/internal/change/get.go
  5. 11
      cmd/kratos/internal/project/new.go
  6. 10
      cmd/kratos/internal/project/project.go
  7. 4
      cmd/kratos/internal/proto/add/proto.go
  8. 4
      cmd/kratos/internal/proto/client/client.go
  9. 1
      cmd/kratos/internal/proto/proto.go
  10. 8
      cmd/kratos/internal/proto/server/server.go
  11. 13
      cmd/kratos/internal/proto/server/template.go
  12. 9
      cmd/kratos/internal/run/run.go
  13. 1
      hack/.lintcheck_failures

@ -20,7 +20,7 @@ func kratosHome() string {
} }
home := path.Join(dir, ".kratos") home := path.Join(dir, ".kratos")
if _, err := os.Stat(home); os.IsNotExist(err) { if _, err := os.Stat(home); os.IsNotExist(err) {
if err := os.MkdirAll(home, 0700); err != nil { if err := os.MkdirAll(home, 0o700); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
@ -30,7 +30,7 @@ func kratosHome() string {
func kratosHomeWithDir(dir string) string { func kratosHomeWithDir(dir string) string {
home := path.Join(kratosHome(), dir) home := path.Join(kratosHome(), dir)
if _, err := os.Stat(home); os.IsNotExist(err) { if _, err := os.Stat(home); os.IsNotExist(err) {
if err := os.MkdirAll(home, 0700); err != nil { if err := os.MkdirAll(home, 0o700); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
@ -103,7 +103,7 @@ func hasSets(name string, sets []string) bool {
} }
func Tree(path string, dir string) { func Tree(path string, dir string) {
filepath.Walk(path, func(path string, info os.FileInfo, err error) error { _ = filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() { if !info.IsDir() {
fmt.Printf("%s %s (%v bytes)\n", color.GreenString("CREATED"), strings.Replace(path, dir+"/", "", -1), info.Size()) fmt.Printf("%s %s (%v bytes)\n", color.GreenString("CREATED"), strings.Replace(path, dir+"/", "", -1), info.Size())
} }

@ -39,7 +39,7 @@ func (r *Repo) Path() string {
if end == -1 { if end == -1 {
end = len(r.url) end = len(r.url)
} }
branch := "" var branch string
if r.branch == "" { if r.branch == "" {
branch = "@main" branch = "@main"
} else { } else {
@ -69,7 +69,7 @@ func (r *Repo) Clone(ctx context.Context) error {
if _, err := os.Stat(r.Path()); !os.IsNotExist(err) { if _, err := os.Stat(r.Path()); !os.IsNotExist(err) {
return r.Pull(ctx) return r.Pull(ctx)
} }
cmd := &exec.Cmd{} var cmd *exec.Cmd
if r.branch == "" { if r.branch == "" {
cmd = exec.Command("git", "clone", r.url, r.Path()) cmd = exec.Command("git", "clone", r.url, r.Path())
} else { } else {

@ -14,8 +14,11 @@ var CmdChange = &cobra.Command{
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}",
Run: run, Run: run,
} }
var token string
var repoURL string var (
token string
repoURL string
)
func init() { func init() {
if repoURL = os.Getenv("KRATOS_REPO"); repoURL == "" { if repoURL = os.Getenv("KRATOS_REPO"); repoURL == "" {
@ -26,8 +29,8 @@ func init() {
} }
func run(cmd *cobra.Command, args []string) { func run(cmd *cobra.Command, args []string) {
owner, repo := ParseGithubUrl(repoURL) owner, repo := ParseGithubURL(repoURL)
api := GithubApi{Owner: owner, Repo: repo, Token: token} api := GithubAPI{Owner: owner, Repo: repo, Token: token}
version := "latest" version := "latest"
if len(args) > 0 { if len(args) > 0 {
version = args[0] version = args[0]

@ -19,7 +19,7 @@ type ReleaseInfo struct {
} `json:"author"` } `json:"author"`
PublishedAt string `json:"published_at"` PublishedAt string `json:"published_at"`
Body string `json:"body"` Body string `json:"body"`
HtmlUrl string `json:"html_url"` HTMLURL string `json:"html_url"`
} }
type CommitInfo struct { type CommitInfo struct {
@ -32,20 +32,20 @@ type ErrorInfo struct {
Message string Message string
} }
type GithubApi struct { type GithubAPI struct {
Owner string Owner string
Repo string Repo string
Token string Token string
} }
// GetReleaseInfo for getting kratos release info. // GetReleaseInfo for getting kratos release info.
func (g *GithubApi) GetReleaseInfo(version string) ReleaseInfo { func (g *GithubAPI) GetReleaseInfo(version string) ReleaseInfo {
api := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", g.Owner, g.Repo) api := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", g.Owner, g.Repo)
if version != "latest" { if version != "latest" {
api = fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/tags/%s", g.Owner, g.Repo, version) api = fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/tags/%s", g.Owner, g.Repo, version)
} }
resp, code := requestGithubAPI(api, "GET", nil, g.Token) resp, code := requestGithubAPI(api, "GET", nil, g.Token)
if code != 200 { if code != http.StatusOK {
printGithubErrorInfo(resp) printGithubErrorInfo(resp)
} }
releaseInfo := ReleaseInfo{} releaseInfo := ReleaseInfo{}
@ -57,14 +57,14 @@ func (g *GithubApi) GetReleaseInfo(version string) ReleaseInfo {
} }
// GetCommitsInfo for getting kratos commits info. // GetCommitsInfo for getting kratos commits info.
func (g *GithubApi) GetCommitsInfo() []CommitInfo { func (g *GithubAPI) GetCommitsInfo() []CommitInfo {
info := g.GetReleaseInfo("latest") info := g.GetReleaseInfo("latest")
page := 1 page := 1
var list []CommitInfo var list []CommitInfo
for { for {
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) 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 != http.StatusOK {
printGithubErrorInfo(resp) printGithubErrorInfo(resp)
} }
var res []CommitInfo var res []CommitInfo
@ -73,7 +73,7 @@ func (g *GithubApi) GetCommitsInfo() []CommitInfo {
fatal(err) fatal(err)
} }
list = append(list, res...) list = append(list, res...)
if len(res) < 100 { if len(res) < http.StatusContinue {
break break
} }
page++ page++
@ -179,14 +179,14 @@ func ParseReleaseInfo(info ReleaseInfo) string {
"Author: %s\nDate: %s\nUrl: %s\n\n%s\n\n%s\n\n%s\n", "Author: %s\nDate: %s\nUrl: %s\n\n%s\n\n%s\n\n%s\n",
info.Author.Login, info.Author.Login,
info.PublishedAt, info.PublishedAt,
info.HtmlUrl, info.HTMLURL,
splitters, splitters,
body, body,
splitters, splitters,
) )
} }
func ParseGithubUrl(url string) (owner string, repo string) { func ParseGithubURL(url string) (owner string, repo string) {
var start int var start int
start = strings.Index(url, "//") start = strings.Index(url, "//")
if start == -1 { if start == -1 {

@ -20,7 +20,6 @@ type Project struct {
// New new a project from remote repo. // New new a project from remote repo.
func (p *Project) New(ctx context.Context, dir string, layout string, branch string) error { func (p *Project) New(ctx context.Context, dir string, layout string, branch string) error {
to := path.Join(dir, p.Name) to := path.Join(dir, p.Name)
if _, err := os.Stat(to); !os.IsNotExist(err) { if _, err := os.Stat(to); !os.IsNotExist(err) {
fmt.Printf("🚫 %s already exists\n", p.Name) fmt.Printf("🚫 %s already exists\n", p.Name)
@ -29,7 +28,10 @@ func (p *Project) New(ctx context.Context, dir string, layout string, branch str
Message: "📂 Do you want to override the folder ?", Message: "📂 Do you want to override the folder ?",
Help: "Delete the existing folder and create the project.", Help: "Delete the existing folder and create the project.",
} }
survey.AskOne(prompt, &override) e := survey.AskOne(prompt, &override)
if e != nil {
return e
}
if !override { if !override {
return err return err
} }
@ -40,10 +42,13 @@ func (p *Project) New(ctx context.Context, dir string, layout string, branch str
if err := repo.CopyTo(ctx, to, p.Path, []string{".git", ".github"}); err != nil { if err := repo.CopyTo(ctx, to, p.Path, []string{".git", ".github"}); err != nil {
return err return err
} }
os.Rename( e := os.Rename(
path.Join(to, "cmd", "server"), path.Join(to, "cmd", "server"),
path.Join(to, "cmd", p.Name), path.Join(to, "cmd", p.Name),
) )
if e != nil {
return e
}
base.Tree(to, dir) base.Tree(to, dir)
fmt.Printf("\n🍺 Project creation succeeded %s\n", color.GreenString(p.Name)) fmt.Printf("\n🍺 Project creation succeeded %s\n", color.GreenString(p.Name))

@ -19,8 +19,10 @@ var CmdNew = &cobra.Command{
Run: run, Run: run,
} }
var repoURL string var (
var branch string repoURL string
branch string
)
func init() { func init() {
if repoURL = os.Getenv("KRATOS_LAYOUT_REPO"); repoURL == "" { if repoURL = os.Getenv("KRATOS_LAYOUT_REPO"); repoURL == "" {
@ -43,8 +45,8 @@ func run(cmd *cobra.Command, args []string) {
Message: "What is project name ?", Message: "What is project name ?",
Help: "Created project name.", Help: "Created project name.",
} }
survey.AskOne(prompt, &name) err = survey.AskOne(prompt, &name)
if name == "" { if err != nil || name == "" {
return return
} }
} else { } else {

@ -29,7 +29,7 @@ func (p *Proto) Generate() error {
} }
to := path.Join(wd, p.Path) to := path.Join(wd, p.Path)
if _, err := os.Stat(to); os.IsNotExist(err) { if _, err := os.Stat(to); os.IsNotExist(err) {
if err := os.MkdirAll(to, 0700); err != nil { if err := os.MkdirAll(to, 0o700); err != nil {
return err return err
} }
} }
@ -37,5 +37,5 @@ func (p *Proto) Generate() error {
if _, err := os.Stat(name); !os.IsNotExist(err) { if _, err := os.Stat(name); !os.IsNotExist(err) {
return fmt.Errorf("%s already exists", p.Name) return fmt.Errorf("%s already exists", p.Name)
} }
return ioutil.WriteFile(name, body, 0644) return ioutil.WriteFile(name, body, 0o644)
} }

@ -14,15 +14,13 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var (
// CmdClient represents the source command. // CmdClient represents the source command.
CmdClient = &cobra.Command{ var CmdClient = &cobra.Command{
Use: "client", Use: "client",
Short: "Generate the proto client code", Short: "Generate the proto client code",
Long: "Generate the proto client code. Example: kratos proto client helloworld.proto", Long: "Generate the proto client code. Example: kratos proto client helloworld.proto",
Run: run, Run: run,
} }
)
var protoPath string var protoPath string

@ -23,5 +23,4 @@ func init() {
} }
func run(cmd *cobra.Command, args []string) { func run(cmd *cobra.Command, args []string) {
} }

@ -60,8 +60,10 @@ func run(cmd *cobra.Command, args []string) {
for _, e := range s.Elements { for _, e := range s.Elements {
r, ok := e.(*proto.RPC) r, ok := e.(*proto.RPC)
if ok { if ok {
cs.Methods = append(cs.Methods, &Method{Service: s.Name, Name: r.Name, Request: r.RequestType, cs.Methods = append(cs.Methods, &Method{
Reply: r.ReturnsType, Type: getMethodType(r.StreamsRequest, r.StreamsReturns)}) Service: s.Name, Name: r.Name, Request: r.RequestType,
Reply: r.ReturnsType, Type: getMethodType(r.StreamsRequest, r.StreamsReturns),
})
} }
} }
res = append(res, cs) res = append(res, cs)
@ -81,7 +83,7 @@ func run(cmd *cobra.Command, args []string) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := ioutil.WriteFile(to, b, 0644); err != nil { if err := ioutil.WriteFile(to, b, 0o644); err != nil {
log.Fatal(err) log.Fatal(err)
} }
fmt.Println(to) fmt.Println(to)

@ -34,7 +34,8 @@ func New{{ .Service }}Service() *{{ .Service }}Service {
{{- $s1 := "google.protobuf.Empty" }} {{- $s1 := "google.protobuf.Empty" }}
{{ range .Methods }} {{ range .Methods }}
{{- if eq .Type 1 }} {{- if eq .Type 1 }}
func (s *{{ .Service }}Service) {{ .Name }}(ctx context.Context, req {{ if eq .Request $s1 }}*emptypb.Empty{{ else }}*pb.{{ .Request }}{{ end }}) ({{ if eq .Reply $s1 }}*emptypb.Empty{{ else }}*pb.{{ .Reply }}{{ end }}, error) { func (s *{{ .Service }}Service) {{ .Name }}(ctx context.Context, req {{ if eq .Request $s1 }}*emptypb.Empty
{{ else }}*pb.{{ .Request }}{{ end }}) ({{ if eq .Reply $s1 }}*emptypb.Empty{{ else }}*pb.{{ .Reply }}{{ end }}, error) {
return {{ if eq .Reply $s1 }}&emptypb.Empty{}{{ else }}&pb.{{ .Reply }}{}{{ end }}, nil return {{ if eq .Reply $s1 }}&emptypb.Empty{}{{ else }}&pb.{{ .Reply }}{}{{ end }}, nil
} }
@ -70,7 +71,8 @@ func (s *{{ .Service }}Service) {{ .Name }}(conn pb.{{ .Service }}_{{ .Name }}Se
} }
{{- else if eq .Type 4 }} {{- else if eq .Type 4 }}
func (s *{{ .Service }}Service) {{ .Name }}(req {{ if eq .Request $s1 }}*emptypb.Empty{{ else }}*pb.{{ .Request }}{{ end }}, conn pb.{{ .Service }}_{{ .Name }}Server) error { func (s *{{ .Service }}Service) {{ .Name }}(req {{ if eq .Request $s1 }}*emptypb.Empty
{{ else }}*pb.{{ .Request }}{{ end }}, conn pb.{{ .Service }}_{{ .Name }}Server) error {
for { for {
err := conn.Send(&pb.{{ .Reply }}{}) err := conn.Send(&pb.{{ .Reply }}{})
if err != nil { if err != nil {
@ -115,13 +117,14 @@ type Method struct {
} }
func (s *Service) execute() ([]byte, error) { func (s *Service) execute() ([]byte, error) {
const empty = "google.protobuf.Empty"
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
for _, method := range s.Methods { for _, method := range s.Methods {
if (method.Type == unaryType && (method.Request == "google.protobuf.Empty" || method.Reply == "google.protobuf.Empty")) || if (method.Type == unaryType && (method.Request == empty || method.Reply == empty)) ||
(method.Type == returnsStreamsType && method.Request == "google.protobuf.Empty") { (method.Type == returnsStreamsType && method.Request == empty) {
s.GoogleEmpty = true s.GoogleEmpty = true
} }
if method.Type == twoWayStreamsType || method.Type == returnsStreamsType { if method.Type == twoWayStreamsType || method.Type == requestStreamsType {
s.UseIO = true s.UseIO = true
} }
if method.Type == unaryType { if method.Type == unaryType {

@ -48,15 +48,15 @@ func Run(cmd *cobra.Command, args []string) {
} }
} else { } else {
var cmdPaths []string var cmdPaths []string
for k, _ := range cmdPath { for k := range cmdPath {
cmdPaths = append(cmdPaths, k) cmdPaths = append(cmdPaths, k)
} }
prompt := &survey.Select{ prompt := &survey.Select{
Message: "Which directory do you want to run?", Message: "Which directory do you want to run?",
Options: cmdPaths, Options: cmdPaths,
} }
survey.AskOne(prompt, &dir) e := survey.AskOne(prompt, &dir)
if dir == "" { if e != nil || dir == "" {
return return
} }
dir = path.Join(cmdPath[dir], dir) dir = path.Join(cmdPath[dir], dir)
@ -70,7 +70,6 @@ func Run(cmd *cobra.Command, args []string) {
fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err.Error()) fmt.Fprintf(os.Stderr, "\033[31mERROR: %s\033[m\n", err.Error())
return return
} }
return
} }
func findCMD(base string) (map[string]string, error) { func findCMD(base string) (map[string]string, error) {
@ -110,7 +109,7 @@ func findCMD(base string) (map[string]string, error) {
if root { if root {
break break
} }
tmp = filepath.Join(base, "..") _ = filepath.Join(base, "..")
} }
return map[string]string{"": base}, nil return map[string]string{"": base}, nil
} }

@ -1,5 +1,4 @@
./examples ./examples
./cmd/kratos
./contrib/metrics/datadog ./contrib/metrics/datadog
./contrib/config/nacos ./contrib/config/nacos
./contrib/config/apollo ./contrib/config/apollo

Loading…
Cancel
Save