Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ func codegen(ctx context.Context, combo config.CombinedSettings, sql OutputPair,
switch {
case plug.Process != nil:
handler = &process.Runner{
GoPkg: plug.Process.GoPkg,
Cmd: plug.Process.Cmd,
Env: plug.Env,
Format: plug.Process.Format,
Expand Down
4 changes: 3 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type Plugin struct {
Name string `json:"name" yaml:"name"`
Env []string `json:"env" yaml:"env"`
Process *struct {
GoPkg string `json:"go_package" yaml:"go_package"`
Cmd string `json:"cmd" yaml:"cmd"`
Format string `json:"format" yaml:"format"`
} `json:"process" yaml:"process"`
Expand Down Expand Up @@ -159,7 +160,8 @@ var ErrPluginExists = errors.New("a plugin with that name already exists")
var ErrPluginNotFound = errors.New("no plugin found")
var ErrPluginNoType = errors.New("plugin: field `process` or `wasm` required")
var ErrPluginBothTypes = errors.New("plugin: `process` and `wasm` cannot both be defined")
var ErrPluginProcessNoCmd = errors.New("plugin: missing process command")
var ErrPluginProcessTooManyCmd = errors.New("plugin: only one of `cmd` or `go_package` is allowed for process plugin")
var ErrPluginProcessNoCmd = errors.New("plugin: missing `cmd` or `go_package` for process plugin")

var ErrInvalidDatabase = errors.New("database must be managed or have a non-empty URI")
var ErrManagedDatabaseNoProject = errors.New(`managed databases require a cloud project
Expand Down
7 changes: 5 additions & 2 deletions internal/config/v_two.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ func v2ParseConfig(rd io.Reader) (Config, error) {
if conf.Plugins[i].Process != nil && conf.Plugins[i].WASM != nil {
return conf, ErrPluginBothTypes
}
if conf.Plugins[i].Process != nil {
if conf.Plugins[i].Process.Cmd == "" {
if r := conf.Plugins[i].Process; r != nil {
switch {
case r.Cmd != "" && r.GoPkg != "":
return conf, ErrPluginProcessTooManyCmd
case r.Cmd == "" && r.GoPkg == "":
return conf, ErrPluginProcessNoCmd
}
}
Expand Down
37 changes: 32 additions & 5 deletions internal/ext/process/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
)

type Runner struct {
GoPkg string
Cmd string
Format string
Env []string
Expand Down Expand Up @@ -53,13 +54,39 @@ func (r *Runner) Invoke(ctx context.Context, method string, args any, reply any,
return fmt.Errorf("unknown plugin format: %s", r.Format)
}

// Check if the output plugin exists
path, err := exec.LookPath(r.Cmd)
if err != nil {
return fmt.Errorf("process: %s not found", r.Cmd)
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, method)
cmd.Args = []string{"go", "run", r.GoPkg}
r.Env = append(r.Env, []string{
"GO111MODULE",
"GOROOT",
"GOPATH",
"GOPROXY",
"GOPRIVATE",
"GONOPROXY",
"GONOSUMDB",
"GOMODCACHE",
"GOFLAGS",
"GOCACHE",
"GOENV",
"HOME",
}...)
}

cmd := exec.CommandContext(ctx, path, method)
cmd.Stdin = bytes.NewReader(stdin)
cmd.Env = []string{
fmt.Sprintf("SQLC_VERSION=%s", info.Version),
Expand Down
Loading