diff --git a/kepub/kepub.go b/kepub/kepub.go index ec06409..4db38e1 100644 --- a/kepub/kepub.go +++ b/kepub/kepub.go @@ -5,6 +5,8 @@ import ( "io/ioutil" "os" "path/filepath" + "runtime" + "sync" "time" "github.com/beevik/etree" @@ -56,24 +58,49 @@ func Kepubify(src, dest string, printlog bool) error { bar.Start() } - for _, cf := range contentfiles { - buf, err := ioutil.ReadFile(cf) - if err != nil { - return fmt.Errorf("Could not open content file \"%s\" for reading: %s", cf, err) - } - str := string(buf) - err = process(&str) - if err != nil { - return fmt.Errorf("Error processing content file \"%s\": %s", cf, err) - } - err = ioutil.WriteFile(cf, []byte(str), 0644) - if err != nil { - return fmt.Errorf("Error writing content file \"%s\": %s", cf, err) - } - time.Sleep(time.Millisecond * 5) - if printlog { - bar.Increment() - } + runtime.GOMAXPROCS(runtime.NumCPU() + 1) + wg := sync.WaitGroup{} + cerr := make(chan error, 1) + for _, f := range contentfiles { + wg.Add(1) + go func(cf string) { + defer wg.Done() + buf, err := ioutil.ReadFile(cf) + if err != nil { + select { + case cerr <- fmt.Errorf("Could not open content file \"%s\" for reading: %s", cf, err): // Put err in the channel unless it is full + default: + } + return + } + str := string(buf) + err = process(&str) + if err != nil { + select { + case cerr <- fmt.Errorf("Error processing content file \"%s\": %s", cf, err): // Put err in the channel unless it is full + default: + } + return + } + err = ioutil.WriteFile(cf, []byte(str), 0644) + if err != nil { + select { + case cerr <- fmt.Errorf("Error writing content file \"%s\": %s", cf, err): // Put err in the channel unless it is full + default: + } + return + } + time.Sleep(time.Millisecond * 5) + if printlog { + bar.Increment() + } + }(f) + } + wg.Wait() + if len(cerr) > 0 { + bar.Finish() + fmt.Println() + return <-cerr } if printlog { diff --git a/kepubify.go b/kepubify.go index 2d0431b..e8640c3 100644 --- a/kepubify.go +++ b/kepubify.go @@ -108,7 +108,13 @@ func main() { app.Version = version app.ArgsUsage = "EPUB_INPUT_PATH [KEPUB_OUTPUT_PATH]" - app.Action = convert + app.Action = func(c *cli.Context) error { + err := convert(c) + if err != nil { + fmt.Println(err) + } + return err + } app.Run(os.Args) }