diff --git a/main.go b/main.go index b376175..1a13be7 100644 --- a/main.go +++ b/main.go @@ -274,7 +274,14 @@ func main() { if err != nil { log.Fatalf("Failed to find unformatted files %s: %+v\n", originPath, err) } - fmt.Printf("%s\n", unformattedFiles.String()) + + if unformattedFiles != nil { + fmt.Printf("%s\n", unformattedFiles.String()) + if *setExitStatus { + os.Exit(1) + } + } + return } err := reviser.NewSourceDir(originProjectName, originPath, *isRecursive, excludes).Fix(options...) diff --git a/reviser/dir.go b/reviser/dir.go index fb7613f..2691482 100644 --- a/reviser/dir.go +++ b/reviser/dir.go @@ -121,6 +121,10 @@ func (d *SourceDir) Find(options ...SourceFileOption) (*UnformattedCollection, e return nil, fmt.Errorf("failed to walk dif: %w", err) } + if len(badFormattedCollection) == 0 { + return nil, nil + } + return newUnformattedCollection(badFormattedCollection), nil } @@ -176,6 +180,10 @@ func (c *UnformattedCollection) List() []string { } func (c *UnformattedCollection) String() string { + if c == nil { + return "" + } + var builder strings.Builder for i, file := range c.list { builder.WriteString(file)