|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "runtime/trace" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/google/cel-go/cel" |
| 14 | + "github.com/spf13/cobra" |
| 15 | + |
| 16 | + "github.com/kyleconroy/sqlc/internal/config" |
| 17 | + "github.com/kyleconroy/sqlc/internal/debug" |
| 18 | + "github.com/kyleconroy/sqlc/internal/opts" |
| 19 | + "github.com/kyleconroy/sqlc/internal/plugin" |
| 20 | +) |
| 21 | + |
| 22 | +var ErrFailedChecks = errors.New("failed checks") |
| 23 | + |
| 24 | +func NewCmdVet() *cobra.Command { |
| 25 | + return &cobra.Command{ |
| 26 | + Use: "vet", |
| 27 | + Short: "Vet examines queries", |
| 28 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 29 | + defer trace.StartRegion(cmd.Context(), "vet").End() |
| 30 | + stderr := cmd.ErrOrStderr() |
| 31 | + dir, name := getConfigPath(stderr, cmd.Flag("file")) |
| 32 | + if err := Vet(cmd.Context(), ParseEnv(cmd), dir, name, stderr); err != nil { |
| 33 | + if !errors.Is(err, ErrFailedChecks) { |
| 34 | + fmt.Fprintf(stderr, "%s\n", err) |
| 35 | + } |
| 36 | + os.Exit(1) |
| 37 | + } |
| 38 | + return nil |
| 39 | + }, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func Vet(ctx context.Context, e Env, dir, filename string, stderr io.Writer) error { |
| 44 | + configPath, conf, err := readConfig(stderr, dir, filename) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + base := filepath.Base(configPath) |
| 50 | + if err := config.Validate(conf); err != nil { |
| 51 | + fmt.Fprintf(stderr, "error validating %s: %s\n", base, err) |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + if err := e.Validate(conf); err != nil { |
| 56 | + fmt.Fprintf(stderr, "error validating %s: %s\n", base, err) |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + env, err := cel.NewEnv( |
| 61 | + cel.StdLib(), |
| 62 | + cel.Types( |
| 63 | + &plugin.VetConfig{}, |
| 64 | + &plugin.VetQuery{}, |
| 65 | + ), |
| 66 | + cel.Variable("query", |
| 67 | + cel.ObjectType("plugin.VetQuery"), |
| 68 | + ), |
| 69 | + cel.Variable("config", |
| 70 | + cel.ObjectType("plugin.VetConfig"), |
| 71 | + ), |
| 72 | + ) |
| 73 | + if err != nil { |
| 74 | + return fmt.Errorf("new env; %s", err) |
| 75 | + } |
| 76 | + |
| 77 | + checks := map[string]cel.Program{} |
| 78 | + msgs := map[string]string{} |
| 79 | + |
| 80 | + for _, c := range conf.Rules { |
| 81 | + if c.Name == "" { |
| 82 | + return fmt.Errorf("checks require a name") |
| 83 | + } |
| 84 | + if _, found := checks[c.Name]; found { |
| 85 | + return fmt.Errorf("type-check error: a check with the name '%s' already exists", c.Name) |
| 86 | + } |
| 87 | + if c.Rule == "" { |
| 88 | + return fmt.Errorf("type-check error: %s is empty", c.Name) |
| 89 | + } |
| 90 | + ast, issues := env.Compile(c.Rule) |
| 91 | + if issues != nil && issues.Err() != nil { |
| 92 | + return fmt.Errorf("type-check error: %s %s", c.Name, issues.Err()) |
| 93 | + } |
| 94 | + prg, err := env.Program(ast) |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("program construction error: %s %s", c.Name, err) |
| 97 | + } |
| 98 | + checks[c.Name] = prg |
| 99 | + msgs[c.Name] = c.Msg |
| 100 | + } |
| 101 | + |
| 102 | + errored := true |
| 103 | + for _, sql := range conf.SQL { |
| 104 | + combo := config.Combine(*conf, sql) |
| 105 | + |
| 106 | + // TODO: This feels like a hack that will bite us later |
| 107 | + joined := make([]string, 0, len(sql.Schema)) |
| 108 | + for _, s := range sql.Schema { |
| 109 | + joined = append(joined, filepath.Join(dir, s)) |
| 110 | + } |
| 111 | + sql.Schema = joined |
| 112 | + |
| 113 | + joined = make([]string, 0, len(sql.Queries)) |
| 114 | + for _, q := range sql.Queries { |
| 115 | + joined = append(joined, filepath.Join(dir, q)) |
| 116 | + } |
| 117 | + sql.Queries = joined |
| 118 | + |
| 119 | + var name string |
| 120 | + parseOpts := opts.Parser{ |
| 121 | + Debug: debug.Debug, |
| 122 | + } |
| 123 | + |
| 124 | + result, failed := parse(ctx, name, dir, sql, combo, parseOpts, stderr) |
| 125 | + if failed { |
| 126 | + return nil |
| 127 | + } |
| 128 | + req := codeGenRequest(result, combo) |
| 129 | + cfg := vetConfig(req) |
| 130 | + for _, query := range req.Queries { |
| 131 | + q := vetQuery(query) |
| 132 | + for _, name := range sql.Rules { |
| 133 | + prg, ok := checks[name] |
| 134 | + if !ok { |
| 135 | + return fmt.Errorf("type-check error: a check with the name '%s' does not exist", name) |
| 136 | + } |
| 137 | + out, _, err := prg.Eval(map[string]any{ |
| 138 | + "query": q, |
| 139 | + "config": cfg, |
| 140 | + }) |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + tripped, ok := out.Value().(bool) |
| 145 | + if !ok { |
| 146 | + return fmt.Errorf("expression returned non-bool: %s", err) |
| 147 | + } |
| 148 | + if tripped { |
| 149 | + // TODO: Get line numbers in the output |
| 150 | + msg := msgs[name] |
| 151 | + if msg == "" { |
| 152 | + fmt.Fprintf(stderr, query.Filename+": %s: %s\n", q.Name, name, msg) |
| 153 | + } else { |
| 154 | + fmt.Fprintf(stderr, query.Filename+": %s: %s: %s\n", q.Name, name, msg) |
| 155 | + } |
| 156 | + errored = true |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + if errored { |
| 162 | + return ErrFailedChecks |
| 163 | + } |
| 164 | + return nil |
| 165 | +} |
| 166 | + |
| 167 | +func vetConfig(req *plugin.CodeGenRequest) *plugin.VetConfig { |
| 168 | + return &plugin.VetConfig{ |
| 169 | + Version: req.Settings.Version, |
| 170 | + Engine: req.Settings.Engine, |
| 171 | + Schema: req.Settings.Schema, |
| 172 | + Queries: req.Settings.Queries, |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +func vetQuery(q *plugin.Query) *plugin.VetQuery { |
| 177 | + var params []*plugin.VetParameter |
| 178 | + for _, p := range q.Params { |
| 179 | + params = append(params, &plugin.VetParameter{ |
| 180 | + Number: p.Number, |
| 181 | + }) |
| 182 | + } |
| 183 | + return &plugin.VetQuery{ |
| 184 | + Sql: q.Text, |
| 185 | + Name: q.Name, |
| 186 | + Cmd: strings.TrimPrefix(":", q.Cmd), |
| 187 | + Params: params, |
| 188 | + } |
| 189 | +} |
0 commit comments