-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathgen.go
More file actions
118 lines (104 loc) · 2.92 KB
/
gen.go
File metadata and controls
118 lines (104 loc) · 2.92 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package process
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"os/exec"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"github.com/sqlc-dev/sqlc/internal/info"
)
type Runner struct {
GoPkg string
Cmd string
Format string
Env []string
}
func (r *Runner) Invoke(ctx context.Context, method string, args any, reply any, opts ...grpc.CallOption) error {
req, ok := args.(protoreflect.ProtoMessage)
if !ok {
return fmt.Errorf("args isn't a protoreflect.ProtoMessage")
}
var stdin []byte
var err error
switch r.Format {
case "json":
m := &protojson.MarshalOptions{
EmitUnpopulated: true,
Indent: "",
UseProtoNames: true,
}
stdin, err = m.Marshal(req)
if err != nil {
return fmt.Errorf("failed to encode codegen request: %w", err)
}
case "", "protobuf":
stdin, err = proto.Marshal(req)
if err != nil {
return fmt.Errorf("failed to encode codegen request: %w", err)
}
default:
return fmt.Errorf("unknown plugin format: %s", r.Format)
}
var cmd *exec.Cmd
switch {
case r.Cmd != "":
// Check if the output plugin exists
path, err := exec.LookPath(r.Cmd)
if err != nil {
return fmt.Errorf("process: %s not found", r.Cmd)
}
cmd = exec.CommandContext(ctx, path, method)
case r.GoPkg != "":
// Check if the go binary exists
path, err := exec.LookPath("go")
if err != nil {
return fmt.Errorf("go binary required to run go package %s", r.GoPkg)
}
cmd = exec.CommandContext(ctx, path, "run", r.GoPkg, method)
r.Env = append(r.Env, []string{"GO111MODULE", "GOROOT", "GOPATH", "GOPROXY", "GOPRIVATE", "GONOPROXY", "GONOSUMDB", "GOMODCACHE", "GOFLAGS", "GOCACHE", "GOENV", "HOME"}...)
}
cmd.Stdin = bytes.NewReader(stdin)
cmd.Env = []string{
fmt.Sprintf("SQLC_VERSION=%s", info.Version),
}
for _, key := range r.Env {
if key == "SQLC_AUTH_TOKEN" {
continue
}
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", key, os.Getenv(key)))
}
out, err := cmd.Output()
if err != nil {
stderr := err.Error()
var exit *exec.ExitError
if errors.As(err, &exit) {
stderr = string(exit.Stderr)
}
return fmt.Errorf("process: error running command %s", stderr)
}
resp, ok := reply.(protoreflect.ProtoMessage)
if !ok {
return fmt.Errorf("reply isn't a protoreflect.ProtoMessage")
}
switch r.Format {
case "json":
if err := protojson.Unmarshal(out, resp); err != nil {
return fmt.Errorf("process: failed to read codegen resp: %w", err)
}
default:
if err := proto.Unmarshal(out, resp); err != nil {
return fmt.Errorf("process: failed to read codegen resp: %w", err)
}
}
return nil
}
func (r *Runner) NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error) {
return nil, status.Error(codes.Unimplemented, "")
}