/
client.go
250 lines (228 loc) · 6.64 KB
/
client.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package cmd
import (
"bytes"
"fmt"
"github.com/ml444/gctl/config"
"github.com/ml444/gctl/util"
"go/build"
"io/fs"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/ml444/gctl/parser"
log "github.com/ml444/glog"
"github.com/spf13/cobra"
)
var clientCmd = &cobra.Command{
Use: "client",
Short: "Generate client lib",
Aliases: []string{"c"},
Run: func(cmd *cobra.Command, args []string) {
if protoPath == "" && len(args) == 0 {
log.Error("You must provide the file of proto: gctl client -p=<protoFilepath> or gctl client <NAME>")
return
}
if serviceGroup == "" && config.DefaultSvcGroup != "" {
serviceGroup = config.DefaultSvcGroup
}
baseDir := config.TargetRootPath
if protoPath == "" {
protoPath = args[0]
//protoPath = filepath.Join(baseDir, config.GoModulePrefix, fmt.Sprintf("%s.proto", arg))
}
protoPath = GetProtoAbsPath(protoPath)
tmpDir := GetTemplateClientDir()
onceFiles := config.OnceFiles
log.Info("root location of code generation:", baseDir)
log.Info("template path of code generation:", tmpDir)
log.Info("files that are executed only once during initialization:", onceFiles)
onceFileMap := map[string]bool{}
for _, fileName := range onceFiles {
onceFileMap[fileName] = true
}
var err error
pd, err := parser.ParseProto(protoPath)
if err != nil {
log.Errorf("err: %v", err)
return
}
//dataMap["GoVersion"] = strings.TrimPrefix(runtime.Version(), "go")
clientRootDir := filepath.Join(baseDir, strings.Split(pd.Options["go_package"], ";")[0])
err = filepath.Walk(tmpDir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
log.Errorf("prevent panic by handling failure accessing a path %q: %v\n", path, err)
return err
}
if info.IsDir() {
log.Warnf("skipping dir: %+v \n", info.Name())
return nil
}
fileName := strings.TrimSuffix(info.Name(), config.TmplConfigFile.Template.FilesFormatSuffix)
parentPath := strings.TrimRight(strings.TrimPrefix(path, tmpDir), info.Name())
targetFile := clientRootDir + parentPath + fileName
if util.IsFileExist(targetFile) && onceFileMap[fileName] {
log.Infof("[%s] file is exist in this directory, skip it", targetFile)
return nil
}
log.Infof("generating file: %s", targetFile)
err = parser.GenerateTemplate(targetFile, path, info.Name(), pd, funcMap)
if err != nil {
return err
}
return nil
})
if err != nil {
fmt.Printf("error walking the path %q: %v\n", tmpDir, err)
return
}
// generate protobuf file
{
if ok := checkProtoc(); !ok {
return
}
log.Info("generating protobuf file")
err = GenerateProtobuf(pd, baseDir, needGenGrpcPb)
if err != nil {
log.Error(err)
return
}
}
absPath, err := filepath.Abs(clientRootDir)
if err != nil {
log.Errorf("err: %v", err)
return
}
// inject tag
{
pbFilepath := filepath.Join(absPath, fmt.Sprintf("%s.pb.go", pd.PackageName))
areas, err := parser.ParsePbFile(pbFilepath, nil, nil)
if err != nil {
log.Fatal(err)
}
if err = parser.WritePbFile(pbFilepath, areas, false); err != nil {
log.Fatal(err)
}
}
// go mod tidy
{
cmd := exec.Command("bash", "-c", fmt.Sprintf("cd %s && go mod tidy && go fmt ./...", absPath))
log.Infof("exec: %s", cmd.String())
var outBuf, errBuf bytes.Buffer
cmd.Stdout = &outBuf
cmd.Stderr = &errBuf
err = cmd.Run()
if err != nil {
log.Infof("Err: %s \nStdout: %s \n Stderr: %s", err, outBuf.String(), errBuf.String())
return
}
log.Infof(" %s \n fmt files: %s", errBuf.String(), outBuf.String())
}
time.Sleep(time.Millisecond * 100)
},
}
func checkProtoc() bool {
p := exec.Command("protoc")
if p.Run() != nil {
log.Error("Please install protoc first and than rerun the command")
switch runtime.GOOS {
case "windows":
log.Warn(
`Install proto3.
https://github.com/google/protobuf/releases
Update protoc Go bindings via
> go get -u github.com/golang/protobuf/proto
> go get -u github.com/golang/protobuf/protoc-gen-go
See also
https://github.com/grpc/grpc-go/tree/master/examples`,
)
case "darwin":
log.Warn(
`Install proto3 from source macOS only.
> brew install autoconf automake libtool
> git clone https://github.com/google/protobuf
> ./autogen.sh ; ./configure ; make ; make install
Update protoc Go bindings via
> go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
See also
https://github.com/grpc/grpc-go/tree/master/examples`,
)
default:
log.Warn(`Install proto3
sudo apt-get install -y git autoconf automake libtool curl make g++ unzip
git clone https://github.com/google/protobuf.git
cd protobuf/
./autogen.sh
./configure
make
make check
sudo make install
sudo ldconfig # refresh shared library cache.`)
}
return false
}
return true
}
func GenerateProtobuf(pd *parser.ProtoData, basePath string, needGenGrpcPb bool) error {
var err error
var args []string
var protocName string
var protoGenGoName string
switch runtime.GOOS {
case "windows":
protocName = "protoc.exe"
protoGenGoName = "protoc-gen-go.exe"
default:
protocName = "protoc"
protoGenGoName = "protoc-gen-go"
}
goPath := os.Getenv("GOPATH")
if goPath == "" {
goPath = build.Default.GOPATH
}
protoGenGoPath := filepath.ToSlash(filepath.Join(goPath, "bin", protoGenGoName))
args = append(args, fmt.Sprintf("--plugin=protoc-gen-go=%s", protoGenGoPath))
args = append(args, fmt.Sprintf("--go_out=%s", filepath.ToSlash(basePath)))
args = append(args, fmt.Sprintf("--validate_out=lang=go:%s", filepath.ToSlash(basePath)))
if needGenGrpcPb {
args = append(args, fmt.Sprintf("--go-grpc_out=%s", filepath.ToSlash(basePath)))
}
// include proto
includePaths := getIncludePathList()
for _, x := range includePaths {
args = append(args, fmt.Sprintf("--proto_path=%s", x))
}
protoDir, protoName := filepath.Split(pd.FilePath)
args = append(args, fmt.Sprintf("-I=%s", protoDir), protoName)
protocPath := filepath.ToSlash(filepath.Join(goPath, "bin", protocName))
cmd := exec.Command(protocPath, args...)
log.Info("exec:", cmd.String())
var outBuf, errBuf bytes.Buffer
cmd.Stdout = &outBuf
cmd.Stderr = &errBuf
err = cmd.Run()
outStr := outBuf.String()
errStr := errBuf.String()
if err != nil {
log.Infof("Err: %s \nStdout: %s \nStderr: %s", err, outStr, errStr)
return err
}
if outStr != "" {
log.Info("out:", outStr)
}
if errStr != "" {
log.Error("err:", errStr)
}
return nil
}
func getIncludePathList() []string {
return config.ThirdPartyProtoPath
}
func GetTemplateClientDir() string {
var elems []string
elems = append(elems, config.TmplRootDir)
elems = append(elems, config.TmplConfigFile.Template.RelativeDir.Client...)
return filepath.Join(elems...)
}