1
0
mirror of https://github.com/go-kratos/kratos.git synced 2024-12-26 20:54:38 +02:00

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

This commit is contained in:
yuemoxi 2021-09-07 15:46:22 +08:00 committed by GitHub
parent f5339e3ea3
commit 6ca81236b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 61 additions and 51 deletions

View File

@ -20,7 +20,7 @@ func kratosHome() string {
}
home := path.Join(dir, ".kratos")
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)
}
}
@ -30,7 +30,7 @@ func kratosHome() string {
func kratosHomeWithDir(dir string) string {
home := path.Join(kratosHome(), dir)
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)
}
}
@ -103,7 +103,7 @@ func hasSets(name string, sets []string) bool {
}
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() {
fmt.Printf("%s %s (%v bytes)\n", color.GreenString("CREATED"), strings.Replace(path, dir+"/", "", -1), info.Size())
}

View File

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

View File

@ -14,8 +14,11 @@ var CmdChange = &cobra.Command{
Long: "Get a kratos release or commits info. Example: kratos changelog dev or kratos changelog {version}",
Run: run,
}
var token string
var repoURL string
var (
token string
repoURL string
)
func init() {
if repoURL = os.Getenv("KRATOS_REPO"); repoURL == "" {
@ -26,8 +29,8 @@ func init() {
}
func run(cmd *cobra.Command, args []string) {
owner, repo := ParseGithubUrl(repoURL)
api := GithubApi{Owner: owner, Repo: repo, Token: token}
owner, repo := ParseGithubURL(repoURL)
api := GithubAPI{Owner: owner, Repo: repo, Token: token}
version := "latest"
if len(args) > 0 {
version = args[0]

View File

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

View File

@ -20,7 +20,6 @@ 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 := path.Join(dir, p.Name)
if _, err := os.Stat(to); !os.IsNotExist(err) {
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 ?",
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 {
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 {
return err
}
os.Rename(
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🍺 Project creation succeeded %s\n", color.GreenString(p.Name))

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -48,15 +48,15 @@ func Run(cmd *cobra.Command, args []string) {
}
} else {
var cmdPaths []string
for k, _ := range cmdPath {
for k := range cmdPath {
cmdPaths = append(cmdPaths, k)
}
prompt := &survey.Select{
Message: "Which directory do you want to run?",
Options: cmdPaths,
}
survey.AskOne(prompt, &dir)
if dir == "" {
e := survey.AskOne(prompt, &dir)
if e != nil || dir == "" {
return
}
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())
return
}
return
}
func findCMD(base string) (map[string]string, error) {
@ -110,7 +109,7 @@ func findCMD(base string) (map[string]string, error) {
if root {
break
}
tmp = filepath.Join(base, "..")
_ = filepath.Join(base, "..")
}
return map[string]string{"": base}, nil
}

View File

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