forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfmt.go
More file actions
320 lines (269 loc) · 8.79 KB
/
fmt.go
File metadata and controls
320 lines (269 loc) · 8.79 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// 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 (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/spf13/cobra"
"github.com/open-policy-agent/opa/cmd/internal/env"
fileurl "github.com/open-policy-agent/opa/internal/file/url"
"github.com/open-policy-agent/opa/v1/ast"
"github.com/open-policy-agent/opa/v1/format"
)
type fmtCommandParams struct {
overwrite bool
list bool
diff bool
fail bool
regoV1 bool
v0Compatible bool
v1Compatible bool
checkResult bool
dropV0Imports bool
capabilitiesFlag *capabilitiesFlag
}
func newFmtCommandParams() *fmtCommandParams {
return &fmtCommandParams{
capabilitiesFlag: newCapabilitiesFlag(),
}
}
func (p *fmtCommandParams) capabilities() *ast.Capabilities {
if p.capabilitiesFlag != nil && p.capabilitiesFlag.C != nil {
return p.capabilitiesFlag.C
}
return ast.CapabilitiesForThisVersion(ast.CapabilitiesRegoVersion(p.regoVersion()))
}
func (p *fmtCommandParams) regoVersion() ast.RegoVersion {
// The '--rego-v1' flag takes precedence over the '--v1-compatible' flag.
if p.regoV1 {
return ast.RegoV0CompatV1
}
// 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
}
func opaFmt(args []string, fmtParams *fmtCommandParams) int {
if len(args) == 0 {
if err := formatStdin(fmtParams, os.Stdin, os.Stdout); err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
return 0
}
for _, filename := range args {
var err error
filename, err = fileurl.Clean(filename)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
err = filepath.Walk(filename, func(path string, info os.FileInfo, err error) error {
return formatFile(fmtParams, os.Stdout, path, info, err)
})
if err != nil {
switch err := err.(type) {
case fmtError:
fmt.Fprintln(os.Stderr, err.msg)
return err.code
default:
fmt.Fprintln(os.Stderr, err.Error())
return 1
}
}
}
return 0
}
func formatFile(params *fmtCommandParams, out io.Writer, filename string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if filepath.Ext(filename) != ".rego" {
return nil
}
contents, err := os.ReadFile(filename)
if err != nil {
return newError("failed to open file: %v", err)
}
opts := format.Opts{
RegoVersion: params.regoVersion(),
DropV0Imports: params.dropV0Imports,
Capabilities: params.capabilities(),
}
if params.regoV1 {
opts.ParserOptions = &ast.ParserOptions{RegoVersion: ast.RegoV0}
}
if params.v0Compatible {
// v0 takes precedence over v1
opts.ParserOptions = &ast.ParserOptions{RegoVersion: ast.RegoV0}
} else if params.v1Compatible {
opts.ParserOptions = &ast.ParserOptions{RegoVersion: ast.RegoV1}
}
formatted, err := format.SourceWithOpts(filename, contents, opts)
if err != nil {
return newError("failed to format Rego source file: %v", err)
}
if params.checkResult {
popts := ast.ParserOptions{RegoVersion: params.regoVersion()}
_, err := ast.ParseModuleWithOpts("formatted", string(formatted), popts)
if err != nil {
return newError("%s was successfully formatted, but the result is invalid: %v\n\nTo inspect the formatted Rego, you can turn off this check with --check-result=false.", filename, err)
}
}
changed := !bytes.Equal(contents, formatted)
if params.fail && !params.list && !params.diff {
if changed {
return newError("unexpected diff")
}
}
if params.list {
if changed {
fmt.Fprintln(out, filename)
if params.fail {
return newError("unexpected diff")
}
}
return nil
}
if params.diff {
if changed {
diffString := doDiff(contents, formatted)
if _, err := fmt.Fprintln(out, diffString); err != nil {
return newError("failed to print contents: %v", err)
}
if params.fail {
return newError("unexpected diff")
}
}
return nil
}
if params.overwrite {
outfile, err := os.OpenFile(filename, os.O_WRONLY|os.O_TRUNC, info.Mode().Perm())
if err != nil {
return newError("failed to open file for writing: %v", err)
}
defer outfile.Close()
out = outfile
}
_, err = out.Write(formatted)
if err != nil {
return newError("failed writing formatted contents: %v", err)
}
return nil
}
func formatStdin(params *fmtCommandParams, r io.Reader, w io.Writer) error {
contents, err := io.ReadAll(r)
if err != nil {
return err
}
opts := format.Opts{
RegoVersion: params.regoVersion(),
Capabilities: params.capabilities(),
}
if params.regoV1 {
opts.ParserOptions = &ast.ParserOptions{RegoVersion: ast.RegoV0}
}
if params.v0Compatible {
// v0 takes precedence over v1
opts.ParserOptions = &ast.ParserOptions{RegoVersion: ast.RegoV0}
} else if params.v1Compatible {
opts.ParserOptions = &ast.ParserOptions{RegoVersion: ast.RegoV1}
}
formatted, err := format.SourceWithOpts("stdin", contents, opts)
if err != nil {
return err
}
_, err = w.Write(formatted)
return err
}
func doDiff(a, b []byte) (diffString string) { // "a" is old, "b" is new
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(string(a), string(b), false)
return dmp.DiffPrettyText(diffs)
}
type fmtError struct {
msg string
code int
}
func (e fmtError) Error() string {
return fmt.Sprintf("%s (%d)", e.msg, e.code)
}
func newError(msg string, a ...any) fmtError {
return fmtError{
msg: fmt.Sprintf(msg, a...),
code: 2,
}
}
func init() {
fmtParams := newFmtCommandParams()
var formatCommand = &cobra.Command{
Use: "fmt [path [...]]",
Short: "Format Rego source files",
Long: `Format Rego source files.
The 'fmt' command takes a Rego source file and outputs a reformatted version. If no file path
is provided - this tool will use stdin.
The format of the output is not defined specifically; whatever this tool outputs
is considered correct format (with the exception of bugs).
If the '-w' option is supplied, the 'fmt' command will overwrite the source file
instead of printing to stdout.
If the '-d' option is supplied, the 'fmt' command will output a diff between the
original and formatted source.
If the '-l' option is supplied, the 'fmt' command will output the names of files
that would change if formatted. The '-l' option will suppress any other output
to stdout from the 'fmt' command.
If the '--fail' option is supplied, the 'fmt' command will return a non zero exit
code if a file would be reformatted.
The 'fmt' command can be run in several compatibility modes for consuming and outputting
different Rego versions:
* ` + "`" + `opa fmt` + "`" + `:
* v1 Rego is formatted to v1
* ` + "`" + `rego.v1` + "`" + `/` + "`" + `future.keywords` + "`" + ` imports are NOT removed
* ` + "`" + `rego.v1` + "`" + `/` + "`" + `future.keywords` + "`" + ` imports are NOT added if missing
* v0 rego is rejected
* ` + "`" + `opa fmt --v0-compatible` + "`" + `:
* v0 Rego is formatted to v0
* v1 Rego is rejected
* ` + "`" + `opa fmt --v0-v1` + "`" + `:
* v0 Rego is formatted to be compatible with v0 AND v1
* v1 Rego is rejected
* ` + "`" + `opa fmt --v0-v1 --v1-compatible` + "`" + `:
* v1 Rego is formatted to be compatible with v0 AND v1
* v0 Rego is rejected
`,
PreRunE: func(cmd *cobra.Command, _ []string) error {
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true
cmd.SilenceUsage = true
exit := opaFmt(args, fmtParams)
if exit != 0 {
return newExitError(exit)
}
return nil
},
}
formatCommand.Flags().BoolVarP(&fmtParams.overwrite, "write", "w", false, "overwrite the original source file")
formatCommand.Flags().BoolVarP(&fmtParams.list, "list", "l", false, "list all files who would change when formatted")
formatCommand.Flags().BoolVarP(&fmtParams.diff, "diff", "d", false, "only display a diff of the changes")
formatCommand.Flags().BoolVar(&fmtParams.fail, "fail", false, "non zero exit code on reformat")
addRegoV0V1FlagWithDescription(formatCommand.Flags(), &fmtParams.regoV1, false, "format module(s) to be compatible with both Rego v0 and v1")
addV0CompatibleFlag(formatCommand.Flags(), &fmtParams.v0Compatible, false)
addV1CompatibleFlag(formatCommand.Flags(), &fmtParams.v1Compatible, false)
formatCommand.Flags().BoolVar(&fmtParams.checkResult, "check-result", true, "assert that the formatted code is valid and can be successfully parsed")
formatCommand.Flags().BoolVar(&fmtParams.dropV0Imports, "drop-v0-imports", false, "drop v0 imports from the formatted code, such as 'rego.v1' and 'future.keywords'")
addCapabilitiesFlag(formatCommand.Flags(), fmtParams.capabilitiesFlag)
RootCommand.AddCommand(formatCommand)
}