forked from alpancs/protoc-gen-pubsub-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
47 lines (39 loc) · 1016 Bytes
/
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
package main
import (
"io"
"os"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"
)
func main() {
req, err := decodeRequest(os.Stdin)
if err != nil {
exitWithResponseError(err.Error(), os.Stdout)
}
builder, err := newResponseBuilder(req)
if err != nil {
exitWithResponseError(err.Error(), os.Stdout)
}
encodeResponse(builder.build(), os.Stdout)
}
func exitWithResponseError(errorMessage string, output io.Writer) {
encodeResponse(&pluginpb.CodeGeneratorResponse{Error: &errorMessage}, output)
os.Exit(0)
}
func decodeRequest(input io.Reader) (*pluginpb.CodeGeneratorRequest, error) {
rawInput, err := io.ReadAll(input)
if err != nil {
return nil, err
}
req := new(pluginpb.CodeGeneratorRequest)
err = proto.Unmarshal(rawInput, req)
return req, err
}
func encodeResponse(resp *pluginpb.CodeGeneratorResponse, output io.Writer) error {
rawOutput, err := proto.Marshal(resp)
if err != nil {
return err
}
_, err = output.Write(rawOutput)
return err
}