forked from lyft/protoc-gen-star
/
main.go
48 lines (40 loc) · 1.21 KB
/
main.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
// protoc-gen-debug emits the raw encoded CodeGeneratorRequest from a protoc
// execution to a file. This is particularly useful for testing (see the
// testdata/graph package for test cases).
package main
import (
"bytes"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/plugin"
)
func main() {
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatal("unable to read input: ", err)
}
req := &plugin_go.CodeGeneratorRequest{}
if err = proto.Unmarshal(data, req); err != nil {
log.Fatal("unable to unmarshal request: ", err)
}
path := req.GetParameter()
if path == "" {
log.Fatal(`please execute the plugin with the output path to properly write the output file: --debug_out="{PATH}:{PATH}"`)
}
err = ioutil.WriteFile(filepath.Join(path, "code_generator_request.pb.bin"), data, 0644)
if path == "" {
log.Fatal("unable to write request to disk: ", err)
}
data, err = proto.Marshal(&plugin_go.CodeGeneratorResponse{})
if err != nil {
log.Fatal("unable to marshal response payload: ", err)
}
_, err = io.Copy(os.Stdout, bytes.NewReader(data))
if err != nil {
log.Fatal("unable to write response to stdout: ", err)
}
}