From f236fc9a56a9e50ae829106476f4a407e3191437 Mon Sep 17 00:00:00 2001 From: "80111+shawnps@users.noreply.github.com" <80111+shawnps@users.noreply.github.com> Date: Sun, 31 Mar 2024 12:28:37 +0900 Subject: [PATCH] add back RevertFiles for CLI #397 --- check/check.go | 6 +++++- check/utils.go | 13 +++++++++++++ cmd/goreportcard-cli/main.go | 2 +- handlers/checks.go | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/check/check.go b/check/check.go index df3df81b..5bb6f43b 100644 --- a/check/check.go +++ b/check/check.go @@ -38,7 +38,7 @@ type ChecksResult struct { } // Run executes all checks on the given directory -func Run(dir string) (ChecksResult, error) { +func Run(dir string, cli bool) (ChecksResult, error) { filenames, skipped, err := GoFiles(dir) if err != nil { return ChecksResult{}, fmt.Errorf("could not get filenames: %v", err) @@ -52,6 +52,10 @@ func Run(dir string) (ChecksResult, error) { log.Println("Could not rename files:", err) } + if cli { + defer RevertFiles(skipped) + } + checks := []Check{ GoFmt{Dir: dir, Filenames: filenames}, GoVet{Dir: dir, Filenames: filenames}, diff --git a/check/utils.go b/check/utils.go index 6ce69914..72e8183a 100644 --- a/check/utils.go +++ b/check/utils.go @@ -82,6 +82,19 @@ func RenameFiles(names []string) (err error) { return err } +// RevertFiles removes the ".grc.bk" extension from files +func RevertFiles(names []string) (err error) { + for i := range names { + tmpErr := os.Rename(names[i]+".grc.bk", names[i]) + if tmpErr != nil { + // save this error, but still continue with other files + err = tmpErr + } + } + + return err +} + // lineCount returns the number of lines in a given file func lineCount(filepath string) (int, error) { out, err := exec.Command("wc", "-l", filepath).Output() diff --git a/cmd/goreportcard-cli/main.go b/cmd/goreportcard-cli/main.go index ca8e4e2d..05c91027 100644 --- a/cmd/goreportcard-cli/main.go +++ b/cmd/goreportcard-cli/main.go @@ -34,7 +34,7 @@ func dotPrintf(fullLen int, lfStr, rtFmtStr string, args ...interface{}) { func main() { flag.Parse() - result, err := check.Run(*dir) + result, err := check.Run(*dir, true) if err != nil { log.Fatalf("Fatal error checking %s: %s", *dir, err.Error()) } diff --git a/handlers/checks.go b/handlers/checks.go index 5755b20d..460d5df3 100644 --- a/handlers/checks.go +++ b/handlers/checks.go @@ -99,7 +99,7 @@ func newChecksResp(db *badger.DB, repo string, forceRefresh bool) (checksResp, e return checksResp{}, fmt.Errorf("could not download repo: %v", err) } - checkResult, err := check.Run(dirName(repo, ver)) + checkResult, err := check.Run(dirName(repo, ver), false) if err != nil { return checksResp{}, err }