parent
5b0b39e3df
commit
7cbb0574b9
@ -0,0 +1,19 @@ |
||||
package base |
||||
|
||||
import ( |
||||
"os" |
||||
"os/exec" |
||||
) |
||||
|
||||
// GoGet go get path.
|
||||
func GoGet(path ...string) error { |
||||
for _, p := range path { |
||||
cmd := exec.Command("go", "get", "-u", p) |
||||
cmd.Stdout = os.Stdout |
||||
cmd.Stderr = os.Stderr |
||||
if err := cmd.Run(); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
return nil |
||||
} |
@ -0,0 +1,11 @@ |
||||
package base |
||||
|
||||
import "testing" |
||||
|
||||
func TestModuleVersion(t *testing.T) { |
||||
v, err := ModuleVersion("golang.org/x/mod") |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
t.Log(v) |
||||
} |
@ -0,0 +1,83 @@ |
||||
package client |
||||
|
||||
import ( |
||||
"fmt" |
||||
"os" |
||||
"os/exec" |
||||
"path/filepath" |
||||
"strings" |
||||
|
||||
"github.com/go-kratos/kratos/cmd/kratos/v2/internal/base" |
||||
"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, |
||||
} |
||||
) |
||||
|
||||
func run(cmd *cobra.Command, args []string) { |
||||
if len(args) == 0 { |
||||
fmt.Println("Please enter the proto file or directory") |
||||
return |
||||
} |
||||
var ( |
||||
err error |
||||
proto = strings.TrimSpace(args[0]) |
||||
) |
||||
if _, err = exec.LookPath("protoc-gen-go-http"); err != nil { |
||||
// update the kratos plugins
|
||||
if err := exec.Command("kratos", "upgrade").Run(); err != nil { |
||||
fmt.Println(err) |
||||
} |
||||
} |
||||
if strings.HasSuffix(proto, ".proto") { |
||||
err = generate(proto) |
||||
} else { |
||||
err = walk(proto) |
||||
} |
||||
if err != nil { |
||||
fmt.Println(err) |
||||
} |
||||
} |
||||
|
||||
func walk(dir string) error { |
||||
if dir == "" { |
||||
dir = "." |
||||
} |
||||
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { |
||||
if ext := filepath.Ext(path); ext != ".proto" { |
||||
return nil |
||||
} |
||||
return generate(path) |
||||
}) |
||||
} |
||||
|
||||
// generate is used to execute the generate command for the specified proto file
|
||||
func generate(proto string) error { |
||||
path, name := filepath.Split(proto) |
||||
fd := exec.Command("protoc", []string{ |
||||
"--proto_path=.", |
||||
"--proto_path=" + filepath.Join(base.KratosMod(), "api"), |
||||
"--proto_path=" + filepath.Join(base.KratosMod(), "third_party"), |
||||
"--proto_path=" + filepath.Join(os.Getenv("GOPATH"), "src"), |
||||
"--go_out=paths=source_relative:.", |
||||
"--go-grpc_out=paths=source_relative:.", |
||||
"--go-http_out=paths=source_relative:.", |
||||
"--go-errors_out=paths=source_relative:.", |
||||
name, |
||||
}...) |
||||
fd.Stdout = os.Stdout |
||||
fd.Stderr = os.Stderr |
||||
fd.Dir = path |
||||
if err := fd.Run(); err != nil { |
||||
return err |
||||
} |
||||
fmt.Printf("proto: %s\n", proto) |
||||
return nil |
||||
} |
@ -1,4 +1,4 @@ |
||||
package service |
||||
package server |
||||
|
||||
import ( |
||||
"bytes" |
@ -1,28 +0,0 @@ |
||||
package source |
||||
|
||||
import ( |
||||
"fmt" |
||||
"log" |
||||
"os/exec" |
||||
|
||||
"github.com/spf13/cobra" |
||||
) |
||||
|
||||
// CmdSource represents the source command.
|
||||
var CmdSource = &cobra.Command{ |
||||
Use: "source", |
||||
Short: "Generate the proto source code", |
||||
Long: "Generate the proto source code. Example: kratos proto source ./**/*.proto", |
||||
Run: run, |
||||
} |
||||
|
||||
func run(cmd *cobra.Command, args []string) { |
||||
input := []string{"--go_out=paths=source_relative:.", "--go-grpc_out=paths=source_relative:."} |
||||
input = append(input, args...) |
||||
do := exec.Command("protoc", input...) |
||||
out, err := do.CombinedOutput() |
||||
if err != nil { |
||||
log.Fatalf("failed to execute: %s\n", err) |
||||
} |
||||
fmt.Println(string(out)) |
||||
} |
@ -0,0 +1,30 @@ |
||||
package upgrade |
||||
|
||||
import ( |
||||
"fmt" |
||||
|
||||
"github.com/go-kratos/kratos/cmd/kratos/v2/internal/base" |
||||
"github.com/spf13/cobra" |
||||
) |
||||
|
||||
// CmdUpgrade represents the upgrade command.
|
||||
var CmdUpgrade = &cobra.Command{ |
||||
Use: "upgrade", |
||||
Short: "Upgrade the kratos tools", |
||||
Long: "Upgrade the kratos tools. Example: kratos upgrade", |
||||
Run: Run, |
||||
} |
||||
|
||||
// Run upgrade the kratos tools.
|
||||
func Run(cmd *cobra.Command, args []string) { |
||||
err := base.GoGet( |
||||
"github.com/go-kratos/kratos/cmd/kratos/v2", |
||||
"github.com/go-kratos/kratos/cmd/protoc-gen-go-http/v2", |
||||
"github.com/go-kratos/kratos/cmd/protoc-gen-go-errors/v2", |
||||
"google.golang.org/protobuf/cmd/protoc-gen-go", |
||||
"google.golang.org/grpc/cmd/protoc-gen-go-grpc", |
||||
) |
||||
if err != nil { |
||||
fmt.Println(err) |
||||
} |
||||
} |
@ -1,13 +0,0 @@ |
||||
package main |
||||
|
||||
// go build -ldflags "-X main.Version=x.y.yz"
|
||||
var ( |
||||
// Version is the version of the compiled software.
|
||||
Version string = "v2.0.0" |
||||
// Branch is current branch name the code is built off
|
||||
Branch string |
||||
// Revision is the short commit hash of source tree
|
||||
Revision string |
||||
// BuildDate is the date when the binary was built.
|
||||
BuildDate string |
||||
) |
Loading…
Reference in new issue