feat(cmd): support the third party specify by self and fix the warning when third party is not in current directory (#1266)

* feat(cmd): support the third party specify by self and fix the warning when third party is not in current directory

* add env support and fix the directory of specify as full directory

* fix the env and flag variable name and rename the function name of the directory is exist check
pull/1275/head
opensite 3 years ago committed by GitHub
parent 352d6c8d66
commit a636fd52a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 25
      cmd/kratos/internal/proto/client/client.go

@ -20,11 +20,19 @@ var (
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",
DisableFlagParsing: true,
Run: run, Run: run,
} }
) )
var protoPath string
func init() {
if protoPath = os.Getenv("KRATOS_PROTO_PATH"); protoPath == "" {
protoPath = "./third_party"
}
CmdClient.Flags().StringVarP(&protoPath, "proto_path", "p", protoPath, "proto path")
}
func run(cmd *cobra.Command, args []string) { func run(cmd *cobra.Command, args []string) {
if len(args) == 0 { if len(args) == 0 {
fmt.Println("Please enter the proto file or directory") fmt.Println("Please enter the proto file or directory")
@ -79,7 +87,11 @@ func walk(dir string, args []string) error {
func generate(proto string, args []string) error { func generate(proto string, args []string) error {
input := []string{ input := []string{
"--proto_path=.", "--proto_path=.",
"--proto_path=./third_party", }
if pathExists(protoPath) {
input = append(input, "--proto_path="+protoPath)
}
inputExt := []string{
"--proto_path=" + base.KratosMod(), "--proto_path=" + base.KratosMod(),
"--proto_path=" + filepath.Join(base.KratosMod(), "third_party"), "--proto_path=" + filepath.Join(base.KratosMod(), "third_party"),
"--go_out=paths=source_relative:.", "--go_out=paths=source_relative:.",
@ -87,6 +99,7 @@ func generate(proto string, args []string) error {
"--go-http_out=paths=source_relative:.", "--go-http_out=paths=source_relative:.",
"--go-errors_out=paths=source_relative:.", "--go-errors_out=paths=source_relative:.",
} }
input = append(input, inputExt...)
protoBytes, err := ioutil.ReadFile(proto) protoBytes, err := ioutil.ReadFile(proto)
if err == nil && len(protoBytes) > 0 { if err == nil && len(protoBytes) > 0 {
if ok, _ := regexp.Match(`\n[^/]*(import)\s+"validate/validate.proto"`, protoBytes); ok { if ok, _ := regexp.Match(`\n[^/]*(import)\s+"validate/validate.proto"`, protoBytes); ok {
@ -109,3 +122,11 @@ func generate(proto string, args []string) error {
fmt.Printf("proto: %s\n", proto) fmt.Printf("proto: %s\n", proto)
return nil return nil
} }
func pathExists(path string) bool {
_, err := os.Stat(path)
if err != nil {
return os.IsExist(err)
}
return true
}

Loading…
Cancel
Save