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
|
/*Command protoc-gen-gitaly is designed to be used as a protobuf compiler
plugin to verify Gitaly processes are being followed when writing RPC's.
Usage
The protoc-gen-gitaly linter can be chained into any protoc workflow that
requires verification that Gitaly RPC guidelines are followed. Typically
this can be done by adding the following argument to an existing protoc
command:
--gitaly_out=.
For example, you may add the linter as an argument to the command responsible
for generating Go code:
protoc --go_out=. --gitaly_out=. *.proto
Or, you can run the Gitaly linter by itself. To try out, run the following
command while in the project root:
protoc --gitaly_out=. ./go/internal/linter/testdata/incomplete.proto
You should see some errors printed to screen for improperly written
RPC's in the incomplete.proto file.
Prerequisites
The protobuf compiler (protoc) can be obtained from the GitHub page:
https://github.com/protocolbuffers/protobuf/releases
Background
The protobuf compiler accepts plugins to analyze protobuf files and generate
language specific code.
These plugins require the following executable naming convention:
protoc-gen-$NAME
Where $NAME is the plugin name of the compiler desired. The protobuf compiler
will search the PATH until an executable with that name is found for a
desired plugin. For example, the following protoc command:
protoc --gitaly_out=. *.proto
The above will search the PATH for an executable named protoc-gen-gitaly
The plugin accepts a protobuf message in STDIN that describes the parsed
protobuf files. A response is sent back on STDOUT that contains any errors.
*/
package main
import (
"bytes"
"fmt"
"go/format"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/golang/protobuf/proto"
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
"gitlab.com/gitlab-org/gitaly/proto/go/internal/linter"
)
const (
gitalyProtoDirArg = "proto_dir"
gitalypbDirArg = "gitalypb_dir"
)
func main() {
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("reading input: %s", err)
}
req := new(plugin.CodeGeneratorRequest)
if err := proto.Unmarshal(data, req); err != nil {
log.Fatalf("parsing input proto: %s", err)
}
if err := lintProtos(req); err != nil {
log.Fatal(err)
}
if err := generateProtolistGo(req); err != nil {
log.Fatal(err)
}
}
func parseArgs(argString string) (gitalyProtoDir string, gitalypbDir string) {
for _, arg := range strings.Split(argString, ",") {
argKeyValue := strings.Split(arg, "=")
if len(argKeyValue) != 2 {
continue
}
switch argKeyValue[0] {
case gitalyProtoDirArg:
gitalyProtoDir = argKeyValue[1]
case gitalypbDirArg:
gitalypbDir = argKeyValue[1]
}
}
return gitalyProtoDir, gitalypbDir
}
func lintProtos(req *plugin.CodeGeneratorRequest) error {
var errMsgs []string
for _, pf := range req.GetProtoFile() {
errs := linter.LintFile(pf, req)
for _, err := range errs {
errMsgs = append(errMsgs, err.Error())
}
}
resp := new(plugin.CodeGeneratorResponse)
if len(errMsgs) > 0 {
errMsg := strings.Join(errMsgs, "\n\t")
resp.Error = &errMsg
}
// Send back the results.
data, err := proto.Marshal(resp)
if err != nil {
return fmt.Errorf("failed to marshal output proto: %s", err)
}
_, err = os.Stdout.Write(data)
if err != nil {
return fmt.Errorf("failed to write output proto: %s", err)
}
return nil
}
func generateProtolistGo(req *plugin.CodeGeneratorRequest) error {
var err error
gitalyProtoDir, gitalypbDir := parseArgs(req.GetParameter())
if gitalyProtoDir == "" {
return fmt.Errorf("%s not provided", gitalyProtoDirArg)
}
if gitalypbDir == "" {
return fmt.Errorf("%s not provided", gitalypbDirArg)
}
var protoNames []string
if gitalyProtoDir, err = filepath.Abs(gitalyProtoDir); err != nil {
return fmt.Errorf("failed to get absolute path for %s: %v", gitalyProtoDir, err)
}
files, err := ioutil.ReadDir(gitalyProtoDir)
if err != nil {
return fmt.Errorf("failed to read %s: %v", gitalyProtoDir, err)
}
for _, fi := range files {
if !fi.IsDir() && strings.HasSuffix(fi.Name(), ".proto") {
protoNames = append(protoNames, fmt.Sprintf(`"%s"`, fi.Name()))
}
}
f, err := os.Create(filepath.Join(gitalypbDir, "protolist.go"))
if err != nil {
return fmt.Errorf("could not create protolist.go: %v", err)
}
defer f.Close()
if err = renderProtoList(f, protoNames); err != nil {
return fmt.Errorf("could not render go code: %v", err)
}
return nil
}
// renderProtoList generate a go file with a list of gitaly protos
func renderProtoList(dest io.WriteCloser, protoNames []string) error {
var joinFunc = template.FuncMap{"join": strings.Join}
protoList := `package gitalypb
// Code generated by protoc-gen-gitaly. DO NOT EDIT
// GitalyProtos is a list of gitaly protobuf files
var GitalyProtos = []string{
{{join . ",\n"}},
}
`
protoListTempl, err := template.New("protoList").Funcs(joinFunc).Parse(protoList)
if err != nil {
return fmt.Errorf("could not create go code template: %v", err)
}
var rawGo bytes.Buffer
if err := protoListTempl.Execute(&rawGo, protoNames); err != nil {
return fmt.Errorf("could not execute go code template: %v", err)
}
formattedGo, err := format.Source(rawGo.Bytes())
if err != nil {
return fmt.Errorf("could not format go code: %v", err)
}
if _, err = io.Copy(dest, bytes.NewBuffer(formattedGo)); err != nil {
return fmt.Errorf("failed to write protolist.go file: %v", err)
}
return nil
}
|