-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathdebug.go
More file actions
67 lines (62 loc) · 1.86 KB
/
debug.go
File metadata and controls
67 lines (62 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package opts
import (
"os"
"strings"
)
// The SQLCDEBUG variable controls debugging variables within the runtime. It
// is a comma-separated list of name=val pairs setting these named variables:
//
// dumpast: setting dumpast=1 will print the AST of every SQL statement
// dumpcatalog: setting dumpcatalog=1 will print the parsed database schema
// trace: setting trace=<path> will output a trace
// processplugins: setting processplugins=0 will disable process-based plugins
// databases: setting databases=managed will disable connections to databases via URI
// dumpvetenv: setting dumpvetenv=1 will print the variables available to
// a vet rule during evaluation
// dumpexplain: setting dumpexplain=1 will print the JSON-formatted output
// from executing EXPLAIN ... on a query during vet rule evaluation
type Debug struct {
DumpAST bool
DumpCatalog bool
Trace string
ProcessPlugins bool
OnlyManagedDatabases bool
DumpVetEnv bool
DumpExplain bool
}
func DebugFromEnv() Debug {
return DebugFromString(os.Getenv("SQLCDEBUG"))
}
func DebugFromString(val string) Debug {
d := Debug{
ProcessPlugins: true,
}
if val == "" {
return d
}
for pair := range strings.SplitSeq(val, ",") {
pair = strings.TrimSpace(pair)
switch {
case pair == "dumpast=1":
d.DumpAST = true
case pair == "dumpcatalog=1":
d.DumpCatalog = true
case strings.HasPrefix(pair, "trace="):
traceName := strings.TrimPrefix(pair, "trace=")
if traceName == "1" {
d.Trace = "trace.out"
} else {
d.Trace = traceName
}
case pair == "processplugins=0":
d.ProcessPlugins = false
case pair == "databases=managed":
d.OnlyManagedDatabases = true
case pair == "dumpvetenv=1":
d.DumpVetEnv = true
case pair == "dumpexplain=1":
d.DumpExplain = true
}
}
return d
}