From 2a8a58d9634f5f6f26a9bbb3aa923f166d1a7784 Mon Sep 17 00:00:00 2001 From: Ricardo Branco Date: Thu, 24 Oct 2024 19:07:39 +0200 Subject: [PATCH] Minor refactor --- main.go | 74 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/main.go b/main.go index c9c227a..5c58503 100644 --- a/main.go +++ b/main.go @@ -62,39 +62,39 @@ func getOutput(results *Checksums, noEscape bool) []*Output { return outputs } -func display(results *Checksums, noEscape bool) (unmatched int) { - if opts.check != "\x00" { - file := escapeFilename(results.file) - for i := range results.checksums { - var ok bool - if macKey != nil { - ok = hmac.Equal(results.checksums[i].sum, results.checksums[i].csum) - } else { - ok = bytes.Equal(results.checksums[i].sum, results.checksums[i].csum) - } - if ok { - if !opts.quiet && !opts.status { - if opts.verbose { - fmt.Printf("%s: %s OK\n", file, algorithms[results.checksums[i].hash].name) - } else { - fmt.Printf("%s: OK\n", file) - } +func printChecksums(results *Checksums, noEscape bool) { + if err := format.Execute(os.Stdout, getOutput(results, noEscape)); err != nil { + panic(err) + } +} + +func printCheckResults(results *Checksums, noEscape bool) (unmatched int) { + file := escapeFilename(results.file) + for i := range results.checksums { + var ok bool + if macKey != nil { + ok = hmac.Equal(results.checksums[i].sum, results.checksums[i].csum) + } else { + ok = bytes.Equal(results.checksums[i].sum, results.checksums[i].csum) + } + if ok { + if !opts.quiet && !opts.status { + if opts.verbose { + fmt.Printf("%s: %s OK\n", file, algorithms[results.checksums[i].hash].name) + } else { + fmt.Printf("%s: OK\n", file) } - } else { - unmatched++ - if !opts.status { - if opts.verbose { - fmt.Printf("%s: %s FAILED with %s\n", file, algorithms[results.checksums[i].hash].name, hex.EncodeToString(results.checksums[i].sum)) - } else { - fmt.Printf("%s: FAILED\n", file) - } + } + } else { + unmatched++ + if !opts.status { + if opts.verbose { + fmt.Printf("%s: %s FAILED with %s\n", file, algorithms[results.checksums[i].hash].name, hex.EncodeToString(results.checksums[i].sum)) + } else { + fmt.Printf("%s: FAILED\n", file) } } } - } else { - if err := format.Execute(os.Stdout, getOutput(results, noEscape)); err != nil { - log.Print(err) - } } return unmatched } @@ -281,13 +281,13 @@ func main() { defer f.Close() lines = inputFromFile(f, opts.zero) } else if flag.NArg() == 0 { - display(hashStdin(), true) + printChecksums(hashStdin(), true) os.Exit(0) } else if opts.recursive { lines = inputFromDir(flag.Args(), opts.followSymlinks) } else if opts.str { for _, s := range flag.Args() { - display(hashString(s), true) + printChecksums(hashString(s), true) } os.Exit(0) } else { @@ -319,10 +319,18 @@ func main() { } }() + if opts.check == "\x00" { + for checksum := range checksums { + printChecksums(checksum, false) + } + os.Exit(0) + } + + // Handle -c option + unmatched := 0 for checksum := range checksums { - n := display(checksum, false) - unmatched += n + unmatched += printCheckResults(checksum, false) } unreadableFiles := unreadable.Load()