forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
64 lines (49 loc) · 1.37 KB
/
check.go
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
// Copyright 2017 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package cmd
import (
"fmt"
"os"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/loader"
"github.com/spf13/cobra"
)
var checkParams = struct {
errLimit int
}{}
var errLimit int
var checkCommand = &cobra.Command{
Use: "check",
Short: "Check Rego source files",
Long: `Check Rego source files for parse and compilation errors.
If the 'check' command succeeds in parsing and compiling the source file(s), no output
is produced. If the parsing or compiling fails, 'check' will output the errors
and exit with a non-zero exit code.`,
Run: func(cmd *cobra.Command, args []string) {
os.Exit(checkModules(args))
},
}
func checkModules(args []string) int {
result, err := loader.AllRegos(args)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
modules := map[string]*ast.Module{}
for _, m := range result.Modules {
modules[m.Name] = m.Parsed
}
compiler := ast.NewCompiler().SetErrorLimit(checkParams.errLimit)
if compiler.Compile(modules); compiler.Failed() {
for _, err := range compiler.Errors {
fmt.Fprintln(os.Stderr, err)
}
return 1
}
return 0
}
func init() {
setMaxErrors(checkCommand.Flags(), &checkParams.errLimit)
RootCommand.AddCommand(checkCommand)
}