/
check.go
89 lines (78 loc) · 2.34 KB
/
check.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package codegen
import (
"bytes"
"errors"
"github.com/lazygophers/codegen/state"
"github.com/lazygophers/log"
"github.com/lazygophers/utils/runtime"
"github.com/pterm/pterm"
"os"
"os/exec"
"strings"
)
func CheckEnvironments() (err error) {
// NOTE: 检查 protoc 是否存在
{
log.Debugf("check protoc:%v", state.Config.ProtocPath)
cmd := exec.Command(state.Config.ProtocPath, "--version")
cmd.Dir = runtime.Pwd()
cmd.Env = os.Environ()
output, err := cmd.Output()
if err != nil {
//goland:noinspection GoTypeAssertionOnErrors
switch x := err.(type) {
case *exec.Error:
if errors.Is(x.Err, exec.ErrNotFound) {
pterm.Error.Printfln("protoc not found, please download it at `https://github.com/protocolbuffers/protobuf/releases`")
log.Errorf("err:%v", err)
return errors.New("protoc not found, please download it at `https://github.com/protocolbuffers/protobuf/releases`")
}
default:
log.Errorf("err:%v", err)
return err
}
}
output = bytes.TrimSuffix(output, []byte("\n"))
log.Infof("protoc version:%s", output)
pterm.Success.Printfln("protoc version:%s", output)
}
// NOTE: 检查 protoc-gen-go 是否存在
{
// go install github.com/golang/protobuf/protoc-gen-go
log.Debugf("check protoc-gen-go:%v", state.Config.ProtoGenGoPath)
cmd := exec.Command(state.Config.ProtoGenGoPath, "--version")
cmd.Dir = runtime.Pwd()
cmd.Env = os.Environ()
_, err = cmd.Output()
if err != nil {
//goland:noinspection GoTypeAssertionOnErrors
switch x := err.(type) {
case *exec.ExitError:
switch x.ExitCode() {
case 1:
if !strings.Contains(string(x.Stderr), `unknown argument`) {
log.Errorf("err:%v", err)
return err
}
default:
log.Errorf("err:%v", err)
return err
}
case *exec.Error:
if errors.Is(x.Err, exec.ErrNotFound) {
pterm.Error.Printfln("protoc-gen-go not found, please install it by running `go install github.com/golang/protobuf/protoc-gen-go`")
log.Errorf("err:%v", err)
return errors.New("protoc-gen-go not found, please install it by running `go install github.com/golang/protobuf/protoc-gen-go`")
}
log.Errorf("err:%v", err)
return err
default:
log.Errorf("err:%v", err)
return err
}
}
}
return nil
}
// protoc-gen-go-grpc
// go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest