| 64 | } |
| 65 | |
| 66 | func Vet(ctx context.Context, dir, filename string, opts *Options) error { |
| 67 | e := opts.Env |
| 68 | stderr := opts.Stderr |
| 69 | configPath, conf, err := readConfig(stderr, dir, filename) |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | |
| 74 | base := filepath.Base(configPath) |
| 75 | if err := config.Validate(conf); err != nil { |
| 76 | fmt.Fprintf(stderr, "error validating %s: %s\n", base, err) |
| 77 | return err |
| 78 | } |
| 79 | |
| 80 | if err := e.Validate(conf); err != nil { |
| 81 | fmt.Fprintf(stderr, "error validating %s: %s\n", base, err) |
| 82 | return err |
| 83 | } |
| 84 | |
| 85 | env, err := cel.NewEnv( |
| 86 | cel.StdLib(), |
| 87 | ext.Strings(ext.StringsVersion(1)), |
| 88 | cel.Types( |
| 89 | &vet.Config{}, |
| 90 | &vet.Query{}, |
| 91 | &vet.PostgreSQL{}, |
| 92 | &vet.MySQL{}, |
| 93 | ), |
| 94 | cel.Variable("query", |
| 95 | cel.ObjectType("vet.Query"), |
| 96 | ), |
| 97 | cel.Variable("config", |
| 98 | cel.ObjectType("vet.Config"), |
| 99 | ), |
| 100 | cel.Variable("postgresql", |
| 101 | cel.ObjectType("vet.PostgreSQL"), |
| 102 | ), |
| 103 | cel.Variable("mysql", |
| 104 | cel.ObjectType("vet.MySQL"), |
| 105 | ), |
| 106 | ) |
| 107 | if err != nil { |
| 108 | return fmt.Errorf("new CEL env error: %s", err) |
| 109 | } |
| 110 | |
| 111 | rules := map[string]rule{ |
| 112 | constants.QueryRuleDbPrepare: {NeedsPrepare: true}, |
| 113 | } |
| 114 | |
| 115 | for _, c := range conf.Rules { |
| 116 | if c.Name == "" { |
| 117 | return fmt.Errorf("rules require a name") |
| 118 | } |
| 119 | if _, found := rules[c.Name]; found { |
| 120 | return fmt.Errorf("type-check error: a rule with the name '%s' already exists", c.Name) |
| 121 | } |
| 122 | if c.Rule == "" { |
| 123 | return fmt.Errorf("type-check error: %s is empty", c.Name) |