-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathmain.go
More file actions
89 lines (75 loc) · 1.81 KB
/
main.go
File metadata and controls
89 lines (75 loc) · 1.81 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
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"flag"
"fmt"
"log/slog"
"os"
"runtime"
"runtime/debug"
"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"
)
func getVersionInfo() (version, commit, date string) {
version = "dev"
commit = "none"
date = "unknown"
if info, ok := debug.ReadBuildInfo(); ok {
if info.Main.Version != "(devel)" && info.Main.Version != "" {
version = info.Main.Version
}
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
if len(setting.Value) >= 7 {
commit = setting.Value[:7] // Short commit hash
} else {
commit = setting.Value
}
case "vcs.time":
date = setting.Value
}
}
}
return version, commit, date
}
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 := err.Error()
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 {
version, commit, date := getVersionInfo()
return fmt.Sprintf("%s (%s) @ %s; %s", version, commit, date, runtime.Version())
}