protocgengogrpc.go 1000 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package protocgengogrpc
  2. import (
  3. "strings"
  4. "github.com/zeromicro/go-zero/tools/goctl/pkg/goctl"
  5. "github.com/zeromicro/go-zero/tools/goctl/pkg/golang"
  6. "github.com/zeromicro/go-zero/tools/goctl/rpc/execx"
  7. "github.com/zeromicro/go-zero/tools/goctl/util/env"
  8. )
  9. const (
  10. Name = "protoc-gen-go-grpc"
  11. url = "google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest"
  12. )
  13. func Install(cacheDir string) (string, error) {
  14. return goctl.Install(cacheDir, Name, func(dest string) (string, error) {
  15. err := golang.Install(url)
  16. return dest, err
  17. })
  18. }
  19. func Exists() bool {
  20. _, err := env.LookUpProtocGenGoGrpc()
  21. return err == nil
  22. }
  23. // Version is used to get the version of the protoc-gen-go-grpc plugin.
  24. func Version() (string, error) {
  25. path, err := env.LookUpProtocGenGoGrpc()
  26. if err != nil {
  27. return "", err
  28. }
  29. version, err := execx.Run(path+" --version", "")
  30. if err != nil {
  31. return "", err
  32. }
  33. fields := strings.Fields(version)
  34. if len(fields) > 1 {
  35. return fields[1], nil
  36. }
  37. return "", nil
  38. }