Skip to content

Commit

Permalink
Minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Oct 27, 2024
1 parent a95141f commit 6ba6ae5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
16 changes: 8 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import flag "github.com/spf13/pflag"

const version string = "3.5.2"

func getOutput(results *Checksums, noEscape bool) []*Output {
func getOutput(results *Checksums, noEscape bool, opts Options) []*Output {
var backslash string
outputs := make([]*Output, 0, len(results.checksums)+1)
file := results.file
Expand Down Expand Up @@ -62,13 +62,13 @@ func getOutput(results *Checksums, noEscape bool) []*Output {
return outputs
}

func printChecksums(results *Checksums, noEscape bool) {
if err := format.Execute(os.Stdout, getOutput(results, noEscape)); err != nil {
func printChecksums(results *Checksums, noEscape bool, opts Options) {
if err := format.Execute(os.Stdout, getOutput(results, noEscape, opts)); err != nil {
panic(err)
}
}

func printCheckResults(results *Checksums, noEscape bool) (unmatched int) {
func printCheckResults(results *Checksums) (unmatched int) {
file := escapeFilename(results.file)
for i := range results.checksums {
var ok bool
Expand Down Expand Up @@ -281,13 +281,13 @@ func main() {
defer f.Close()
lines = inputFromFile(f, opts.zero)
} else if flag.NArg() == 0 {
printChecksums(hashStdin(), true)
printChecksums(hashStdin(), true, opts)
os.Exit(0)
} else if opts.recursive {
lines = inputFromDir(flag.Args(), opts.followSymlinks)
} else if opts.str {
for _, s := range flag.Args() {
printChecksums(hashString(s), true)
printChecksums(hashString(s), true, opts)
}
os.Exit(0)
} else {
Expand Down Expand Up @@ -321,7 +321,7 @@ func main() {

if opts.check == "\x00" {
for checksum := range checksums {
printChecksums(checksum, false)
printChecksums(checksum, false, opts)
}
os.Exit(0)
}
Expand All @@ -330,7 +330,7 @@ func main() {

unmatched := 0
for checksum := range checksums {
unmatched += printCheckResults(checksum, false)
unmatched += printCheckResults(checksum)
}

unreadableFiles := unreadable.Load()
Expand Down
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func Test_getOutput(t *testing.T) {
log.Fatal(err)
}
b := new(strings.Builder)
format.Execute(b, getOutput(results, false))
format.Execute(b, getOutput(results, false, opts))
if b.String() != want {
t.Errorf("got %v; want %v", b.String(), want)
}
Expand All @@ -54,7 +54,7 @@ func Test_getOutput(t *testing.T) {
log.Fatal(err)
}
b := new(strings.Builder)
format.Execute(b, getOutput(results, false))
format.Execute(b, getOutput(results, false, opts))
if b.String() != want {
t.Errorf("got %v; want %v", b.String(), want)
}
Expand Down
4 changes: 3 additions & 1 deletion typesvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var (
gnuFormat string = "{{range .}}{{.Sum}} {{.File}}\n{{end}}"
)

var opts struct {
type Options struct {
all bool
base64 bool
check string
Expand All @@ -93,6 +93,8 @@ var opts struct {
zero bool
}

var opts Options

// Used by the -c option

type ErrorAction int
Expand Down

0 comments on commit 6ba6ae5

Please sign in to comment.