Skip to content

Commit d4902a6

Browse files
authored
cmd/sqlc: Add option to disable process-based plugins (#2180)
Add a `processplugins=0` option to `SQLCDEBUG`. This type of configuration is modeled Go's own use of `GODEBUG`. See a full explanation of the design here: https://go.googlesource.com/proposal/+/master/design/56986-godebug.md
1 parent bfc48c7 commit d4902a6

File tree

8 files changed

+92
-9
lines changed

8 files changed

+92
-9
lines changed

internal/cmd/cmd.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"bytes"
66
"context"
7+
"errors"
78
"fmt"
89
"io"
910
"os"
@@ -19,6 +20,7 @@ import (
1920
"github.com/kyleconroy/sqlc/internal/config"
2021
"github.com/kyleconroy/sqlc/internal/debug"
2122
"github.com/kyleconroy/sqlc/internal/info"
23+
"github.com/kyleconroy/sqlc/internal/opts"
2224
"github.com/kyleconroy/sqlc/internal/tracer"
2325
)
2426

@@ -106,16 +108,25 @@ var initCmd = &cobra.Command{
106108

107109
type Env struct {
108110
DryRun bool
111+
Debug opts.Debug
109112
}
110113

111114
func ParseEnv(c *cobra.Command) Env {
112115
dr := c.Flag("dry-run")
113116
return Env{
114117
DryRun: dr != nil && dr.Changed,
118+
Debug: opts.DebugFromEnv(),
115119
}
116120
}
117121

122+
var ErrPluginProcessDisabled = errors.New("plugin: process-based plugins disabled via SQLCDEBUG=processplugins=0")
123+
118124
func (e *Env) Validate(cfg *config.Config) error {
125+
for _, plugin := range cfg.Plugins {
126+
if plugin.Process != nil && !e.Debug.ProcessPlugins {
127+
return ErrPluginProcessDisabled
128+
}
129+
}
119130
return nil
120131
}
121132

internal/endtoend/endtoend_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/google/go-cmp/cmp/cmpopts"
1515

1616
"github.com/kyleconroy/sqlc/internal/cmd"
17+
"github.com/kyleconroy/sqlc/internal/opts"
1718
)
1819

1920
func TestExamples(t *testing.T) {
@@ -110,11 +111,14 @@ func TestReplay(t *testing.T) {
110111
}
111112
}
112113

114+
env := cmd.Env{
115+
Debug: opts.DebugFromString(args.Env["SQLCDEBUG"]),
116+
}
113117
switch args.Command {
114118
case "diff":
115-
err = cmd.Diff(ctx, cmd.Env{}, path, "", &stderr)
119+
err = cmd.Diff(ctx, env, path, "", &stderr)
116120
case "generate":
117-
output, err = cmd.Generate(ctx, cmd.Env{}, path, "", &stderr)
121+
output, err = cmd.Generate(ctx, env, path, "", &stderr)
118122
if err == nil {
119123
cmpDirectory(t, path, output)
120124
}
@@ -209,8 +213,9 @@ func expectedStderr(t *testing.T, dir string) string {
209213
}
210214

211215
type exec struct {
212-
Command string `json:"command"`
213-
Process string `json:"process"`
216+
Command string `json:"command"`
217+
Process string `json:"process"`
218+
Env map[string]string `json:"env"`
214219
}
215220

216221
func parseExec(t *testing.T, dir string) exec {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"process": "sqlc-gen-json",
3+
"env": {
4+
"SQLCDEBUG": "processplugins=0"
5+
}
6+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- name: GetAuthor :one
2+
SELECT * FROM authors
3+
WHERE id = $1 LIMIT 1;
4+
5+
-- name: ListAuthors :many
6+
SELECT * FROM authors
7+
ORDER BY name;
8+
9+
-- name: CreateAuthor :one
10+
INSERT INTO authors (
11+
name, bio
12+
) VALUES (
13+
$1, $2
14+
)
15+
RETURNING *;
16+
17+
-- name: DeleteAuthor :exec
18+
DELETE FROM authors
19+
WHERE id = $1;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE authors (
2+
id BIGSERIAL PRIMARY KEY,
3+
name text NOT NULL,
4+
bio text
5+
);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"version": "2",
3+
"sql": [
4+
{
5+
"schema": "schema.sql",
6+
"queries": "query.sql",
7+
"engine": "postgresql",
8+
"codegen": [
9+
{
10+
"out": "gen",
11+
"plugin": "jsonb",
12+
"options": {
13+
"indent": " ",
14+
"filename": "codegen.json"
15+
}
16+
}
17+
]
18+
}
19+
],
20+
"plugins": [
21+
{
22+
"name": "jsonb",
23+
"process": {
24+
"cmd": "sqlc-gen-json"
25+
}
26+
}
27+
]
28+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
error validating sqlc.json: plugin: process-based plugins disabled via SQLCDEBUG=processplugins=0

internal/opts/debug.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,20 @@ import (
1313
// trace: setting trace=<path> will output a trace
1414

1515
type Debug struct {
16-
DumpAST bool
17-
DumpCatalog bool
18-
Trace string
16+
DumpAST bool
17+
DumpCatalog bool
18+
Trace string
19+
ProcessPlugins bool
1920
}
2021

2122
func DebugFromEnv() Debug {
22-
d := Debug{}
23-
val := os.Getenv("SQLCDEBUG")
23+
return DebugFromString(os.Getenv("SQLCDEBUG"))
24+
}
25+
26+
func DebugFromString(val string) Debug {
27+
d := Debug{
28+
ProcessPlugins: true,
29+
}
2430
if val == "" {
2531
return d
2632
}
@@ -38,6 +44,8 @@ func DebugFromEnv() Debug {
3844
} else {
3945
d.Trace = traceName
4046
}
47+
case pair == "processplugins=0":
48+
d.ProcessPlugins = false
4149
}
4250
}
4351
return d

0 commit comments

Comments
 (0)