-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc.go
74 lines (71 loc) · 2 KB
/
grpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package protoc
import (
"errors"
"fmt"
"github.com/xqk/ox/pkg/util/ocolor"
"os"
"os/exec"
"path/filepath"
"strings"
)
const (
_genGoFastAddress = "go get -u -v github.com/gogo/protobuf/protoc-gen-gofast"
_grpcProtocCmd = "protoc --gofast_out=plugins=grpc:%s %s"
)
func generateGRPC() (err error) {
if err = installGRPCGen(); err != nil {
return
}
if err = doGenerate(); err != nil {
return
}
return
}
func installGRPCGen() (err error) {
gofastPath := ""
if gofastPath, err = exec.LookPath("protoc-gen-gofast"); err != nil {
fmt.Println(ocolor.Green("start installing protoc-gen-gofast"))
if err = executeGoGet(_genGoFastAddress); err != nil {
return
}
}
fmt.Println(ocolor.Green("protoc-gen-gofast installation was successful, the installation path is:", gofastPath))
return
}
func executeGoGet(address string) error {
args := strings.Split(address, " ")
cmd := exec.Command(args[0], args[1:]...)
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func protocEnvCheck() (err error) {
protocPath := ""
if protocPath, err = exec.LookPath("protoc"); err != nil {
err = errors.New("You haven't installed Protobuf yet,Please visit this page to install with your own system:https://github.com/protocolbuffers/protobuf/releases")
return
}
fmt.Println(ocolor.Green("Protoc environment monitoring is successful , the installation path is:", protocPath))
return
}
func doGenerate() (err error) {
if err = os.MkdirAll(option.outputDir, 0755); err != nil {
return
}
cmdLine := fmt.Sprintf(_grpcProtocCmd, option.outputDir, option.protoFilePath)
fmt.Println("protocCmdLine:", cmdLine)
args := strings.Split(cmdLine, " ")
cmd := exec.Command(args[0], args[1:]...)
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return
}
if genAbsPath, err := filepath.Abs(option.outputDir); err == nil {
fmt.Println(ocolor.Green("pb.go file generated successfully. The path is as follows:", genAbsPath))
}
return
}