diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index b599bf21ee..ec7fd76d82 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "context" + "errors" "fmt" "io" "os" @@ -19,6 +20,7 @@ import ( "github.com/kyleconroy/sqlc/internal/config" "github.com/kyleconroy/sqlc/internal/debug" "github.com/kyleconroy/sqlc/internal/info" + "github.com/kyleconroy/sqlc/internal/opts" "github.com/kyleconroy/sqlc/internal/tracer" ) @@ -106,16 +108,25 @@ var initCmd = &cobra.Command{ type Env struct { DryRun bool + Debug opts.Debug } func ParseEnv(c *cobra.Command) Env { dr := c.Flag("dry-run") return Env{ DryRun: dr != nil && dr.Changed, + Debug: opts.DebugFromEnv(), } } +var ErrPluginProcessDisabled = errors.New("plugin: process-based plugins disabled via SQLCDEBUG=processplugins=0") + func (e *Env) Validate(cfg *config.Config) error { + for _, plugin := range cfg.Plugins { + if plugin.Process != nil && !e.Debug.ProcessPlugins { + return ErrPluginProcessDisabled + } + } return nil } diff --git a/internal/endtoend/endtoend_test.go b/internal/endtoend/endtoend_test.go index e19b58bcb9..12f8aa5b21 100644 --- a/internal/endtoend/endtoend_test.go +++ b/internal/endtoend/endtoend_test.go @@ -14,6 +14,7 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/kyleconroy/sqlc/internal/cmd" + "github.com/kyleconroy/sqlc/internal/opts" ) func TestExamples(t *testing.T) { @@ -110,11 +111,14 @@ func TestReplay(t *testing.T) { } } + env := cmd.Env{ + Debug: opts.DebugFromString(args.Env["SQLCDEBUG"]), + } switch args.Command { case "diff": - err = cmd.Diff(ctx, cmd.Env{}, path, "", &stderr) + err = cmd.Diff(ctx, env, path, "", &stderr) case "generate": - output, err = cmd.Generate(ctx, cmd.Env{}, path, "", &stderr) + output, err = cmd.Generate(ctx, env, path, "", &stderr) if err == nil { cmpDirectory(t, path, output) } @@ -209,8 +213,9 @@ func expectedStderr(t *testing.T, dir string) string { } type exec struct { - Command string `json:"command"` - Process string `json:"process"` + Command string `json:"command"` + Process string `json:"process"` + Env map[string]string `json:"env"` } func parseExec(t *testing.T, dir string) exec { diff --git a/internal/endtoend/testdata/process_plugin_disabled/exec.json b/internal/endtoend/testdata/process_plugin_disabled/exec.json new file mode 100644 index 0000000000..d88b817f99 --- /dev/null +++ b/internal/endtoend/testdata/process_plugin_disabled/exec.json @@ -0,0 +1,6 @@ +{ + "process": "sqlc-gen-json", + "env": { + "SQLCDEBUG": "processplugins=0" + } +} diff --git a/internal/endtoend/testdata/process_plugin_disabled/query.sql b/internal/endtoend/testdata/process_plugin_disabled/query.sql new file mode 100644 index 0000000000..75e38b2caf --- /dev/null +++ b/internal/endtoend/testdata/process_plugin_disabled/query.sql @@ -0,0 +1,19 @@ +-- name: GetAuthor :one +SELECT * FROM authors +WHERE id = $1 LIMIT 1; + +-- name: ListAuthors :many +SELECT * FROM authors +ORDER BY name; + +-- name: CreateAuthor :one +INSERT INTO authors ( + name, bio +) VALUES ( + $1, $2 +) +RETURNING *; + +-- name: DeleteAuthor :exec +DELETE FROM authors +WHERE id = $1; diff --git a/internal/endtoend/testdata/process_plugin_disabled/schema.sql b/internal/endtoend/testdata/process_plugin_disabled/schema.sql new file mode 100644 index 0000000000..b4fad78497 --- /dev/null +++ b/internal/endtoend/testdata/process_plugin_disabled/schema.sql @@ -0,0 +1,5 @@ +CREATE TABLE authors ( + id BIGSERIAL PRIMARY KEY, + name text NOT NULL, + bio text +); diff --git a/internal/endtoend/testdata/process_plugin_disabled/sqlc.json b/internal/endtoend/testdata/process_plugin_disabled/sqlc.json new file mode 100644 index 0000000000..f47189143a --- /dev/null +++ b/internal/endtoend/testdata/process_plugin_disabled/sqlc.json @@ -0,0 +1,28 @@ +{ + "version": "2", + "sql": [ + { + "schema": "schema.sql", + "queries": "query.sql", + "engine": "postgresql", + "codegen": [ + { + "out": "gen", + "plugin": "jsonb", + "options": { + "indent": " ", + "filename": "codegen.json" + } + } + ] + } + ], + "plugins": [ + { + "name": "jsonb", + "process": { + "cmd": "sqlc-gen-json" + } + } + ] +} diff --git a/internal/endtoend/testdata/process_plugin_disabled/stderr.txt b/internal/endtoend/testdata/process_plugin_disabled/stderr.txt new file mode 100644 index 0000000000..85ee900cc6 --- /dev/null +++ b/internal/endtoend/testdata/process_plugin_disabled/stderr.txt @@ -0,0 +1 @@ +error validating sqlc.json: plugin: process-based plugins disabled via SQLCDEBUG=processplugins=0 diff --git a/internal/opts/debug.go b/internal/opts/debug.go index 7acfddd161..2c6f9bcaae 100644 --- a/internal/opts/debug.go +++ b/internal/opts/debug.go @@ -13,14 +13,20 @@ import ( // trace: setting trace= will output a trace type Debug struct { - DumpAST bool - DumpCatalog bool - Trace string + DumpAST bool + DumpCatalog bool + Trace string + ProcessPlugins bool } func DebugFromEnv() Debug { - d := Debug{} - val := os.Getenv("SQLCDEBUG") + return DebugFromString(os.Getenv("SQLCDEBUG")) +} + +func DebugFromString(val string) Debug { + d := Debug{ + ProcessPlugins: true, + } if val == "" { return d } @@ -38,6 +44,8 @@ func DebugFromEnv() Debug { } else { d.Trace = traceName } + case pair == "processplugins=0": + d.ProcessPlugins = false } } return d