Skip to content

Commit

Permalink
Attempt 2
Browse files Browse the repository at this point in the history
Signed-off-by: Charlie Egan <charlie@styra.com>
  • Loading branch information
charlieegan3 committed Jun 10, 2024
1 parent 4d4113b commit c6e056d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 28 deletions.
37 changes: 10 additions & 27 deletions cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,8 @@ func init() {

rep, err := lint(args, params)
if err != nil {
if !errors.Is(err, &alreadyReportedError{}) {
log.SetOutput(os.Stderr)
log.Println(err)
}
log.SetOutput(os.Stderr)
log.Println(err)

return exit(1)
}
Expand Down Expand Up @@ -313,7 +311,7 @@ func lint(args []string, params *lintCommandParams) (report.Report, error) {

result, err := regal.Lint(ctx)
if err != nil {
return report.Report{}, reportErrors(params.format, outputWriter, err)
return report.Report{}, formatError(params.format, fmt.Errorf("error(s) encountered while linting: %w", err))
}

rep, err := getReporter(params.format, outputWriter)
Expand Down Expand Up @@ -361,33 +359,18 @@ func getWriterForOutputFile(filename string) (io.Writer, error) {
return f, nil
}

// alreadyReportedError is an error type that is used to signal that an error has already been reported
// and should not shown again, however it should still trigger the error exit code.
type alreadyReportedError struct{}

func (*alreadyReportedError) Error() string {
return "error already reported"
}

func reportErrors(format string, outputWriter io.Writer, err error) error {
func formatError(format string, err error) error {
// currently, JSON and SARIF will get the same generic JSON error format
if format == formatJSON || format == formatSarif {
enc := json.NewEncoder(outputWriter)
enc.SetIndent("", " ")

if err = enc.Encode(map[string]interface{}{
bs, err := json.MarshalIndent(map[string]interface{}{
"errors": []string{err.Error()},
}); err != nil {
return fmt.Errorf("failed to encode error in %s: %w", format, err)
}, "", " ")
if err != nil {
return fmt.Errorf("failed to format errors for output: %w", err)
}

return &alreadyReportedError{}
}

_, err = outputWriter.Write([]byte(fmt.Sprintf("error(s) encountered while linting: %s\n", err)))
if err != nil {
return fmt.Errorf("failed to write error to output: %w", err)
return fmt.Errorf("%s", string(bs))
}

return &alreadyReportedError{}
return err
}
2 changes: 1 addition & 1 deletion e2e/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestLintNonExistentDir(t *testing.T) {
if exp, act := "error(s) encountered while linting: errors encountered when reading files to lint: "+
"failed to filter paths:\nstat "+td+filepath.FromSlash("/what/ever")+": no such file or directory\n",
stderr.String(); exp != act {
t.Errorf("expected stderr %q, got %q", exp, act)
t.Errorf("expected stderr\n%q,\ngot\n%q", exp, act)
}
}

Expand Down

0 comments on commit c6e056d

Please sign in to comment.