-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
65 lines (55 loc) · 1.29 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
package main
import (
"flag"
"fmt"
"log/slog"
"os"
"runtime"
"google.golang.org/protobuf/proto"
pluginpb "google.golang.org/protobuf/types/pluginpb"
"github.com/lmittmann/tint"
"github.com/sudorandom/protoc-gen-connect-openapi/internal/converter"
)
var (
version = "dev"
commit = "none"
date = "unknown"
)
func main() {
showVersion := flag.Bool("version", false, "print the version and exit")
flag.Parse()
if *showVersion {
fmt.Printf("protoc-gen-connect-openapi %s\n", fullVersion())
return
}
resp, err := converter.ConvertFrom(os.Stdin)
if err != nil {
message := fmt.Sprintf("Failed to read input: %v", err)
slog.Error(message)
renderResponse(&pluginpb.CodeGeneratorResponse{
Error: &message,
})
os.Exit(1)
}
slog.SetDefault(slog.New(
tint.NewHandler(os.Stderr, &tint.Options{
Level: slog.LevelDebug,
}),
))
renderResponse(resp)
}
func renderResponse(resp *pluginpb.CodeGeneratorResponse) {
data, err := proto.Marshal(resp)
if err != nil {
slog.Error("failed to marshal response", slog.Any("error", err))
return
}
_, err = os.Stdout.Write(data)
if err != nil {
slog.Error("failed to write response", slog.Any("error", err))
return
}
}
func fullVersion() string {
return fmt.Sprintf("%s (%s) @ %s; %s", version, commit, date, runtime.Version())
}