-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixer: First draft implementation of --fix
WIP while I work on some other fixes to prove out the interfaces. Signed-off-by: Charlie Egan <[email protected]>
- Loading branch information
1 parent
c31886e
commit 0460b60
Showing
9 changed files
with
457 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package fixer | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/open-policy-agent/opa/ast" | ||
"github.com/open-policy-agent/opa/format" | ||
|
||
"github.com/styrainc/regal/pkg/fixer/fixes" | ||
"github.com/styrainc/regal/pkg/report" | ||
) | ||
|
||
func Fix(rep *report.Report, readers map[string]io.Reader, _ fixes.Options) (map[string][]byte, error) { | ||
fixableViolations := map[string]struct{}{ | ||
"opa-fmt": {}, | ||
"use-rego-v1": {}, | ||
} | ||
|
||
filesToFix, err := computeFilesToFix(rep, readers, fixableViolations) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to determine files to fix: %w", err) | ||
} | ||
|
||
fixResults := make(map[string][]byte) | ||
|
||
var fixedViolations []int | ||
|
||
for file, content := range filesToFix { | ||
for i, violation := range rep.Violations { | ||
_, ok := fixableViolations[violation.Title] | ||
if !ok { | ||
continue | ||
} | ||
|
||
fixed := true | ||
|
||
switch violation.Title { | ||
case "opa-fmt": | ||
fixed, fixedContent, err := fixes.Fmt(content, &fixes.FmtOptions{ | ||
Filename: file, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to fix %s: %w", file, err) | ||
} | ||
|
||
if fixed { | ||
fixResults[file] = fixedContent | ||
} | ||
case "use-rego-v1": | ||
fixed, fixedContent, err := fixes.Fmt(content, &fixes.FmtOptions{ | ||
Filename: file, | ||
OPAFmtOpts: format.Opts{ | ||
RegoVersion: ast.RegoV0CompatV1, | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to fix %s: %w", file, err) | ||
} | ||
|
||
if fixed { | ||
fixResults[file] = fixedContent | ||
} | ||
default: | ||
fixed = false | ||
} | ||
|
||
if fixed { | ||
fixedViolations = append(fixedViolations, i) | ||
} | ||
} | ||
} | ||
|
||
for i := len(fixedViolations) - 1; i >= 0; i-- { | ||
rep.Violations = append(rep.Violations[:fixedViolations[i]], rep.Violations[fixedViolations[i]+1:]...) | ||
} | ||
|
||
rep.Summary.NumViolations = len(rep.Violations) | ||
|
||
return fixResults, nil | ||
} | ||
|
||
func computeFilesToFix( | ||
rep *report.Report, | ||
readers map[string]io.Reader, | ||
fixableViolations map[string]struct{}, | ||
) (map[string][]byte, error) { | ||
filesToFix := make(map[string][]byte) | ||
|
||
// determine which files need to be fixed | ||
for _, violation := range rep.Violations { | ||
file := violation.Location.File | ||
|
||
// skip files already marked for fixing | ||
if _, ok := filesToFix[file]; ok { | ||
continue | ||
} | ||
|
||
// skip violations that are not fixable | ||
if _, ok := fixableViolations[violation.Title]; !ok { | ||
continue | ||
} | ||
|
||
if _, ok := readers[file]; !ok { | ||
return nil, fmt.Errorf("no reader for fixable file %s", file) | ||
} | ||
|
||
bs, err := io.ReadAll(readers[file]) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read file %s: %w", file, err) | ||
} | ||
|
||
filesToFix[violation.Location.File] = bs | ||
} | ||
|
||
return filesToFix, nil | ||
} |
Oops, something went wrong.