Skip to content

Commit 20cb9a4

Browse files
committed
feat(plugin): add configuration for plugin to run go package
1 parent 21e6557 commit 20cb9a4

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
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: 3 additions & 1 deletion
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"`
@@ -159,7 +160,8 @@ var ErrPluginExists = errors.New("a plugin with that name already exists")
159160
var ErrPluginNotFound = errors.New("no plugin found")
160161
var ErrPluginNoType = errors.New("plugin: field `process` or `wasm` required")
161162
var ErrPluginBothTypes = errors.New("plugin: `process` and `wasm` cannot both be defined")
162-
var ErrPluginProcessNoCmd = errors.New("plugin: missing process command")
163+
var ErrPluginProcessTooManyCmd = errors.New("plugin: only one of `cmd` or `go_package` is allowed for process plugin")
164+
var ErrPluginProcessNoCmd = errors.New("plugin: missing `cmd` or `go_package` for process plugin")
163165

164166
var ErrInvalidDatabase = errors.New("database must be managed or have a non-empty URI")
165167
var ErrManagedDatabaseNoProject = errors.New(`managed databases require a cloud project

internal/config/v_two.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ func v2ParseConfig(rd io.Reader) (Config, error) {
4848
if conf.Plugins[i].Process != nil && conf.Plugins[i].WASM != nil {
4949
return conf, ErrPluginBothTypes
5050
}
51-
if conf.Plugins[i].Process != nil {
52-
if conf.Plugins[i].Process.Cmd == "" {
51+
if r := conf.Plugins[i].Process; r != nil {
52+
switch {
53+
case r.Cmd != "" && r.GoPkg != "":
54+
return conf, ErrPluginProcessTooManyCmd
55+
case r.Cmd == "" && r.GoPkg == "":
5356
return conf, ErrPluginProcessNoCmd
5457
}
5558
}

internal/ext/process/gen.go

Lines changed: 18 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,25 @@ 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 != "":
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.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, "run", r.GoPkg, method)
73+
r.Env = append(r.Env, []string{"GO111MODULE", "GOROOT", "GOPATH", "GOPROXY", "GOPRIVATE", "GONOPROXY", "GONOSUMDB", "GOMODCACHE", "GOFLAGS", "GOCACHE", "GOENV", "HOME"}...)
6074
}
6175

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

0 commit comments

Comments
 (0)