forked from opendoor-labs/protoc-gen-graphql
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
57 lines (45 loc) · 1.05 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
49
50
51
52
53
54
55
56
57
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/golang/protobuf/proto"
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
)
func main() {
input, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fail("error reading input: " + err.Error())
}
req := &plugin.CodeGeneratorRequest{}
resp := &plugin.CodeGeneratorResponse{}
if err := proto.Unmarshal(input, req); err != nil {
fail("error parsing input: " + err.Error())
}
generator := New(req, resp)
err = generator.Generate()
if err != nil {
writeErr(resp, err)
}
writeOutput(resp)
}
func writeErr(resp *plugin.CodeGeneratorResponse, err error) {
msg := err.Error()
resp.Error = &msg
writeOutput(resp)
os.Exit(0)
}
func writeOutput(resp *plugin.CodeGeneratorResponse) {
output, err := proto.Marshal(resp)
if err != nil {
fail("error serializing output: " + err.Error())
}
_, err = os.Stdout.Write(output)
if err != nil {
fail("error writing output: " + err.Error())
}
}
func fail(msg string) {
_, _ = fmt.Fprintf(os.Stderr, "protoc-gen-graphql: %s", msg)
os.Exit(1)
}