From 99288e4573f3d97d6065f6795511f4975e8bb2d2 Mon Sep 17 00:00:00 2001 From: Micael Malta Date: Mon, 27 May 2024 12:26:35 -0400 Subject: [PATCH] This fixes set-exit-status when using `./...` https://github.com/incu6us/goimports-reviser/issues/93 --- main.go | 9 ++++++++- reviser/dir.go | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) 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 69275b2..0fc2776 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)