-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathparse.go
150 lines (130 loc) · 3.75 KB
/
parse.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2018 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 (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/open-policy-agent/opa/cmd/internal/env"
pr "github.com/open-policy-agent/opa/internal/presentation"
"github.com/open-policy-agent/opa/v1/ast"
astJSON "github.com/open-policy-agent/opa/v1/ast/json"
"github.com/open-policy-agent/opa/v1/loader"
"github.com/open-policy-agent/opa/v1/util"
)
const (
parseFormatPretty = "pretty"
parseFormatJSON = "json"
)
type parseParams struct {
format *util.EnumFlag
jsonInclude string
v0Compatible bool
v1Compatible bool
}
func (p *parseParams) regoVersion() ast.RegoVersion {
// the '--v0--compatible' flag takes precedence over the '--v1-compatible' flag
if p.v0Compatible {
return ast.RegoV0
}
if p.v1Compatible {
return ast.RegoV1
}
return ast.DefaultRegoVersion
}
var configuredParseParams = parseParams{
format: util.NewEnumFlag(parseFormatPretty, []string{parseFormatPretty, parseFormatJSON}),
jsonInclude: "",
}
var parseCommand = &cobra.Command{
Use: "parse <path>",
Short: "Parse Rego source file",
Long: `Parse Rego source file and print AST.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("no source file specified")
}
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},
Run: func(_ *cobra.Command, args []string) {
os.Exit(parse(args, &configuredParseParams, os.Stdout, os.Stderr))
},
}
func parse(args []string, params *parseParams, stdout io.Writer, stderr io.Writer) int {
if len(args) == 0 {
return 0
}
exposeLocation := false
exposeComments := true
for _, opt := range strings.Split(params.jsonInclude, ",") {
value := true
if strings.HasPrefix(opt, "-") {
value = false
}
if strings.HasSuffix(opt, "locations") {
exposeLocation = value
}
if strings.HasSuffix(opt, "comments") {
exposeComments = value
}
}
parserOpts := ast.ParserOptions{
ProcessAnnotation: true,
RegoVersion: params.regoVersion(),
}
if exposeLocation {
parserOpts.JSONOptions = &astJSON.Options{
MarshalOptions: astJSON.MarshalOptions{
IncludeLocationText: true,
IncludeLocation: astJSON.NodeToggle{
Term: true,
Package: true,
Comment: true,
Import: true,
Rule: true,
Head: true,
Expr: true,
SomeDecl: true,
Every: true,
With: true,
Annotations: true,
AnnotationsRef: true,
},
},
}
}
result, err := loader.RegoWithOpts(args[0], parserOpts)
if err != nil {
_ = pr.JSON(stderr, pr.Output{Errors: pr.NewOutputErrors(err)})
return 1
}
if !exposeComments {
result.Parsed.Comments = nil
}
switch params.format.String() {
case parseFormatJSON:
bs, err := json.MarshalIndent(result.Parsed, "", " ")
if err != nil {
_ = pr.JSON(stderr, pr.Output{Errors: pr.NewOutputErrors(err)})
return 1
}
_, _ = fmt.Fprint(stdout, string(bs)+"\n")
default:
if err != nil {
_, _ = fmt.Fprintln(stderr, err)
return 1
}
ast.Pretty(stdout, result.Parsed)
}
return 0
}
func init() {
parseCommand.Flags().VarP(configuredParseParams.format, "format", "f", "set output format")
parseCommand.Flags().StringVarP(&configuredParseParams.jsonInclude, "json-include", "", "", "include or exclude optional elements. By default comments are included. Current options: locations, comments. E.g. --json-include locations,-comments will include locations and exclude comments.")
addV1CompatibleFlag(parseCommand.Flags(), &configuredParseParams.v1Compatible, false)
RootCommand.AddCommand(parseCommand)
}