-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
66 lines (56 loc) · 1.37 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
58
59
60
61
62
63
64
65
66
package main
import (
"flag"
"fmt"
"io"
"os"
"strings"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"
)
func main() {
if err := run(); err != nil {
logf("error: %s", err)
os.Exit(1)
}
}
func run() error {
// Note: flags will not actually be available at this point when invoked via
// protoc. This is just to support running `./protoc-gen-protobufjs -help`
// so that we can get the list of supported flags.
flag.Parse()
req, err := readRequest()
if err != nil {
return fmt.Errorf("failed to read CodeGenerationRequest from stdin: %s", err)
}
// CodeGenerationRequest only supports passing one "parameter", but we allow
// passing multiple flags by separating them with ":" in the parameter.
os.Args = append(os.Args, strings.Split(req.GetParameter(), ":")...)
flag.Parse()
res, err := generateCode(req)
if err != nil {
return err
}
return writeResponse(res)
}
func readRequest() (*pluginpb.CodeGeneratorRequest, error) {
b, err := io.ReadAll(os.Stdin)
if err != nil {
return nil, err
}
req := &pluginpb.CodeGeneratorRequest{}
if err := proto.Unmarshal(b, req); err != nil {
return nil, err
}
return req, nil
}
func writeResponse(res *pluginpb.CodeGeneratorResponse) error {
b, err := proto.Marshal(res)
if err != nil {
return err
}
if _, err := os.Stdout.Write(b); err != nil {
return err
}
return nil
}