-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
76 lines (67 loc) · 1.39 KB
/
Copy pathmain.go
File metadata and controls
76 lines (67 loc) · 1.39 KB
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
70
71
72
73
74
75
76
package main
import (
"context"
"fmt"
"os"
"os/signal"
"runtime"
"runtime/debug"
"syscall"
"github.com/bufbuild/protoplugin"
"github.com/mfridman/protoc-gen-go-json/internal/plugin"
)
func main() {
runArgs(os.Args[1:])
ctx, stop := newContext()
defer stop()
go func() {
defer stop()
if err := protoplugin.Run(
ctx,
protoplugin.Env{
Args: os.Args[1:],
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Environ: os.Environ(),
},
protoplugin.HandlerFunc(plugin.Handle),
); err != nil {
fmt.Fprintf(os.Stderr, "protoc-gen-go-json: %v\n", err)
os.Exit(1)
}
}()
select {
case <-ctx.Done():
stop()
}
}
func runArgs(args []string) error {
if len(args) > 0 {
for _, arg := range args {
switch arg {
case "--version", "-version":
fmt.Fprintf(os.Stdout, "protoc-gen-go-json version: %s\n", version())
os.Exit(0)
default:
fmt.Fprintf(os.Stderr, "protoc-gen-go-json: unknown argument: %s\n", arg)
os.Exit(1)
}
}
}
return nil
}
func version() string {
info, ok := debug.ReadBuildInfo()
if !ok || info.Main.Version == "" {
return "(devel)"
}
return info.Main.Version
}
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...)
}