-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
61 lines (55 loc) · 1.33 KB
/
plugin.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
// Plugin provides a helper methods to build custom protoc code generators.
// It is used for gunk-related repos, but can be used outside of gunk.
package plugin
import (
"fmt"
"io"
"io/ioutil"
"os"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"
)
// Plugin provides plugin method.
type Plugin interface {
Generate(req *pluginpb.CodeGeneratorRequest) (*pluginpb.CodeGeneratorResponse, error)
}
func RunMain(p Plugin) {
req, err := readRequest(os.Stdin)
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
resp, err := p.Generate(req)
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
if err := writeResponse(os.Stdout, resp); err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
}
func readRequest(r io.Reader) (*pluginpb.CodeGeneratorRequest, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
req := new(pluginpb.CodeGeneratorRequest)
if err = proto.Unmarshal(data, req); err != nil {
return nil, err
}
if len(req.GetFileToGenerate()) == 0 {
return nil, fmt.Errorf("no files were supplied to the generator")
}
return req, nil
}
func writeResponse(w io.Writer, resp *pluginpb.CodeGeneratorResponse) error {
data, err := proto.Marshal(resp)
if err != nil {
return err
}
if _, err := w.Write(data); err != nil {
return err
}
return nil
}