|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "runtime/trace" |
| 11 | + |
| 12 | + "github.com/google/cel-go/cel" |
| 13 | + "github.com/spf13/cobra" |
| 14 | + |
| 15 | + "github.com/kyleconroy/sqlc/internal/config" |
| 16 | + "github.com/kyleconroy/sqlc/internal/debug" |
| 17 | + "github.com/kyleconroy/sqlc/internal/opts" |
| 18 | + "github.com/kyleconroy/sqlc/internal/plugin" |
| 19 | +) |
| 20 | + |
| 21 | +func NewCmdVet() *cobra.Command { |
| 22 | + return &cobra.Command{ |
| 23 | + Use: "vet", |
| 24 | + Short: "Vet examines queries", |
| 25 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 26 | + defer trace.StartRegion(cmd.Context(), "vet").End() |
| 27 | + stderr := cmd.ErrOrStderr() |
| 28 | + dir, name := getConfigPath(stderr, cmd.Flag("file")) |
| 29 | + if err := examine(cmd.Context(), ParseEnv(cmd), dir, name, stderr); err != nil { |
| 30 | + fmt.Fprintf(stderr, "%s\n", err) |
| 31 | + os.Exit(1) |
| 32 | + } |
| 33 | + return nil |
| 34 | + }, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func examine(ctx context.Context, e Env, dir, filename string, stderr io.Writer) error { |
| 39 | + configPath, conf, err := readConfig(stderr, dir, filename) |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + |
| 44 | + base := filepath.Base(configPath) |
| 45 | + if err := config.Validate(conf); err != nil { |
| 46 | + fmt.Fprintf(stderr, "error validating %s: %s\n", base, err) |
| 47 | + return err |
| 48 | + } |
| 49 | + |
| 50 | + if err := e.Validate(conf); err != nil { |
| 51 | + fmt.Fprintf(stderr, "error validating %s: %s\n", base, err) |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + env, err := cel.NewEnv( |
| 56 | + cel.StdLib(), |
| 57 | + cel.Types(&plugin.Query{}), |
| 58 | + cel.Variable("query", |
| 59 | + cel.ObjectType("plugin.Query"), |
| 60 | + ), |
| 61 | + ) |
| 62 | + if err != nil { |
| 63 | + return fmt.Errorf("new env; %s", err) |
| 64 | + } |
| 65 | + |
| 66 | + checks := map[string]cel.Program{} |
| 67 | + |
| 68 | + for _, c := range conf.Checks { |
| 69 | + // TODO: Verify check has a name |
| 70 | + // TODO: Verify that check names are unique |
| 71 | + ast, issues := env.Compile(c.Expr) |
| 72 | + if issues != nil && issues.Err() != nil { |
| 73 | + return fmt.Errorf("type-check error: %s %s", c.Name, issues.Err()) |
| 74 | + } |
| 75 | + prg, err := env.Program(ast) |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("program construction error: %s %s", c.Name, err) |
| 78 | + } |
| 79 | + checks[c.Name] = prg |
| 80 | + } |
| 81 | + |
| 82 | + errored := true |
| 83 | + for _, sql := range conf.SQL { |
| 84 | + combo := config.Combine(*conf, sql) |
| 85 | + |
| 86 | + // TODO: This feels like a hack that will bite us later |
| 87 | + joined := make([]string, 0, len(sql.Schema)) |
| 88 | + for _, s := range sql.Schema { |
| 89 | + joined = append(joined, filepath.Join(dir, s)) |
| 90 | + } |
| 91 | + sql.Schema = joined |
| 92 | + |
| 93 | + joined = make([]string, 0, len(sql.Queries)) |
| 94 | + for _, q := range sql.Queries { |
| 95 | + joined = append(joined, filepath.Join(dir, q)) |
| 96 | + } |
| 97 | + sql.Queries = joined |
| 98 | + |
| 99 | + var name string |
| 100 | + parseOpts := opts.Parser{ |
| 101 | + Debug: debug.Debug, |
| 102 | + } |
| 103 | + |
| 104 | + var errout bytes.Buffer |
| 105 | + result, failed := parse(ctx, name, dir, sql, combo, parseOpts, &errout) |
| 106 | + if failed { |
| 107 | + return nil |
| 108 | + } |
| 109 | + req := codeGenRequest(result, combo) |
| 110 | + for _, q := range req.Queries { |
| 111 | + for _, name := range sql.Checks { |
| 112 | + prg, ok := checks[name] |
| 113 | + if !ok { |
| 114 | + // TODO: Return a helpful error message |
| 115 | + continue |
| 116 | + } |
| 117 | + out, _, err := prg.Eval(map[string]any{ |
| 118 | + "query": q, |
| 119 | + }) |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + tripped, ok := out.Value().(bool) |
| 124 | + if !ok { |
| 125 | + return fmt.Errorf("expression returned non-bool: %s", err) |
| 126 | + } |
| 127 | + if tripped { |
| 128 | + // internal/cmd/vet.go:123:13: fmt.Errorf format %s has arg false of wrong type bool |
| 129 | + fmt.Fprintf(stderr, q.Filename+":17:1: query uses :exec\n") |
| 130 | + errored = true |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + if errored { |
| 136 | + return fmt.Errorf("errored") |
| 137 | + } |
| 138 | + return nil |
| 139 | +} |
0 commit comments