Skip to content

Commit b8042be

Browse files
committed
feat: add configuration for plugin to run go package
1 parent b807fe9 commit b8042be

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

‎internal/cmd/generate.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ func codegen(ctx context.Context, combo config.CombinedSettings, sql OutputPair,
349349
switch {
350350
case plug.Process != nil:
351351
handler = &process.Runner{
352+
GoPkg: plug.Process.GoPkg,
352353
Cmd: plug.Process.Cmd,
353354
Env: plug.Env,
354355
Format: plug.Process.Format,

‎internal/config/config.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ type Plugin struct {
8989
Name string `json:"name" yaml:"name"`
9090
Env []string `json:"env" yaml:"env"`
9191
Process *struct {
92+
GoPkg string `json:"go_package" yaml:"go_package"`
9293
Cmd string `json:"cmd" yaml:"cmd"`
9394
Format string `json:"format" yaml:"format"`
9495
} `json:"process" yaml:"process"`

‎internal/ext/process/gen.go‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
)
2020

2121
type Runner struct {
22+
GoPkg string
2223
Cmd string
2324
Format string
2425
Env []string
@@ -53,13 +54,27 @@ func (r *Runner) Invoke(ctx context.Context, method string, args any, reply any,
5354
return fmt.Errorf("unknown plugin format: %s", r.Format)
5455
}
5556

56-
// Check if the output plugin exists
57-
path, err := exec.LookPath(r.Cmd)
58-
if err != nil {
59-
return fmt.Errorf("process: %s not found", r.Cmd)
57+
var cmd *exec.Cmd
58+
switch {
59+
case r.Cmd != "" && r.GoPkg == "":
60+
// Check if the output plugin exists
61+
path, err := exec.LookPath(r.Cmd)
62+
if err != nil {
63+
return fmt.Errorf("process: %s not found", r.Cmd)
64+
}
65+
cmd = exec.CommandContext(ctx, path, method)
66+
case r.Cmd == "" && r.GoPkg != "":
67+
// Check if the go binary exists
68+
path, err := exec.LookPath("go")
69+
if err != nil {
70+
return fmt.Errorf("go binary required to run go package %s", r.GoPkg)
71+
}
72+
cmd = exec.CommandContext(ctx, path, method)
73+
cmd.Args = []string{"run", r.GoPkg}
74+
default:
75+
return fmt.Errorf("cmd and go_package cannot both be empty for process plugin")
6076
}
6177

62-
cmd := exec.CommandContext(ctx, path, method)
6378
cmd.Stdin = bytes.NewReader(stdin)
6479
cmd.Env = []string{
6580
fmt.Sprintf("SQLC_VERSION=%s", info.Version),

0 commit comments

Comments
 (0)