From 52ca3d81cdfd57e0665ebfcf2c2bf9b4a3a3bb4c Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Tue, 21 Nov 2017 21:20:10 +0900 Subject: [PATCH] Don't use fail function in findMarkupFiles() --- main.go | 23 +++++++++++++++++++---- utils.go | 11 ++++++----- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 3729774..d16a1d2 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,9 @@ package main -import "os" +import ( + "os" + "sync" +) const filesCapacity = 1024 @@ -8,13 +11,23 @@ func main() { args, err := getArguments(nil) if err != nil { - printToStderr(err.Error()) - os.Exit(1) + fail(err) } fc := make(chan string, filesCapacity) + ec := make(chan error, 64) + wg := sync.WaitGroup{} + + go findMarkupFiles(args.filenames, args.recursive, fc, ec) - go findMarkupFiles(args.filenames, args.recursive, fc) + wg.Add(1) + go func() { + for e := range ec { + fail(e) + } + + wg.Done() + }() rc := make(chan fileResult, filesCapacity) s := newSemaphore(args.concurrency) @@ -33,6 +46,8 @@ func main() { } } + wg.Wait() + if !ok { os.Exit(1) } diff --git a/utils.go b/utils.go index 4e1f8b3..35d6428 100644 --- a/utils.go +++ b/utils.go @@ -55,27 +55,28 @@ func listDirectory(d string, fc chan<- string) error { }) } -func findMarkupFiles(fs []string, recursive bool, fc chan<- string) { +func findMarkupFiles(fs []string, recursive bool, fc chan<- string, ec chan<- error) { for _, f := range fs { i, err := os.Stat(f) if err != nil { - fail(err) + ec <- err + continue } if i.IsDir() && recursive { err := listDirectory(f, fc) if err != nil { - fail(err) + ec <- err } - } else if i.IsDir() { - fail(fmt.Errorf("%v is not a file", f)) + ec <- fmt.Errorf("%v is not a file", f) } else { fc <- f } } close(fc) + close(ec) }