Skip to content

Commit

Permalink
Minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Oct 24, 2024
1 parent b42fa33 commit e60572b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
6 changes: 2 additions & 4 deletions hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ func hashF(f io.ReadCloser, checksums []*Checksum) []*Checksum {
// build the multiwriter for all the pipes
mw := io.MultiWriter(writers...)
// copy the data into the multiwriter
if _, err := io.Copy(mw, f); err != nil {
return err
}
return nil
_, err := io.Copy(mw, f)
return err
})
if err := g.Wait(); err != nil {
log.Print(err)
Expand Down
74 changes: 41 additions & 33 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit e60572b

Please sign in to comment.