-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (60 loc) · 1.33 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
67
68
69
package main
import (
"context"
"fmt"
"os"
"os/signal"
"runtime"
"syscall"
"github.com/bufbuild/protoplugin"
"github.com/mfridman/buildversion"
"github.com/mfridman/protoc-gen-connectclient-go/internal/plugin"
)
func main() {
runArgs(os.Args[1:])
ctx, stop := newContext()
defer stop()
environ := os.Environ()
environ = append(environ, plugin.PLUGIN_VERSION+"="+buildversion.New())
go func() {
defer stop()
if err := protoplugin.Run(
ctx,
protoplugin.Env{
Args: os.Args[1:],
Environ: environ,
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
},
protoplugin.HandlerFunc(plugin.Handle),
); err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", plugin.Name, err)
os.Exit(1)
}
}()
select {
case <-ctx.Done():
stop()
}
}
func newContext() (context.Context, context.CancelFunc) {
signals := []os.Signal{os.Interrupt}
if runtime.GOOS != "windows" {
signals = append(signals, syscall.SIGTERM)
}
return signal.NotifyContext(context.Background(), signals...)
}
func runArgs(args []string) error {
for _, arg := range args {
switch arg {
case "--version", "-version":
fmt.Fprintf(os.Stdout, "%s version: %s\n", plugin.Name, buildversion.New())
os.Exit(0)
default:
fmt.Fprintf(os.Stderr, "%s: unknown argument: %s\n", plugin.Name, arg)
os.Exit(1)
}
}
return nil
}