From 2755aae487ba3d2146ab15dd9b2b206f0a5c9e50 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sat, 18 May 2024 14:54:49 +0200 Subject: [PATCH] combine print and exit 1 to fatal --- internal/pkg/converter/converter.go | 3 +-- internal/pkg/epubimageprocessor/loader.go | 9 +++------ internal/pkg/epubimageprocessor/processor.go | 7 ++----- internal/pkg/utils/utils.go | 10 ++++++++++ main.go | 9 +++------ 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/internal/pkg/converter/converter.go b/internal/pkg/converter/converter.go index 0f1a66e..1e9e28d 100644 --- a/internal/pkg/converter/converter.go +++ b/internal/pkg/converter/converter.go @@ -407,8 +407,7 @@ func (c *Converter) Validate() error { // Fatal Helper to show usage, err and exit 1 func (c *Converter) Fatal(err error) { c.Cmd.Usage() - utils.Printf("\nError: %s\n", err) - os.Exit(1) + utils.Fatalf("\nError: %s\n", err) } func (c *Converter) Stats() { diff --git a/internal/pkg/epubimageprocessor/loader.go b/internal/pkg/epubimageprocessor/loader.go index a317725..e0afd04 100644 --- a/internal/pkg/epubimageprocessor/loader.go +++ b/internal/pkg/epubimageprocessor/loader.go @@ -319,8 +319,7 @@ func (e EPUBImageProcessor) loadCbr() (totalImages int, output chan task, err er if isSolid && !e.Dry { r, rerr := rardecode.OpenReader(e.Input) if rerr != nil { - utils.Printf("\nerror processing image %s: %s\n", e.Input, rerr) - os.Exit(1) + utils.Fatalf("\nerror processing image %s: %s\n", e.Input, rerr) } defer func(r *rardecode.ReadCloser) { _ = r.Close() @@ -331,15 +330,13 @@ func (e EPUBImageProcessor) loadCbr() (totalImages int, output chan task, err er if rerr == io.EOF { break } - utils.Printf("\nerror processing image %s: %s\n", f.Name, rerr) - os.Exit(1) + utils.Fatalf("\nerror processing image %s: %s\n", f.Name, rerr) } if i, ok := indexedNames[f.Name]; ok { var b bytes.Buffer _, rerr = io.Copy(&b, r) if rerr != nil { - utils.Printf("\nerror processing image %s: %s\n", f.Name, rerr) - os.Exit(1) + utils.Fatalf("\nerror processing image %s: %s\n", f.Name, rerr) } jobs <- job{i, f.Name, func() (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(b.Bytes())), nil diff --git a/internal/pkg/epubimageprocessor/processor.go b/internal/pkg/epubimageprocessor/processor.go index 104d80c..8fc5897 100644 --- a/internal/pkg/epubimageprocessor/processor.go +++ b/internal/pkg/epubimageprocessor/processor.go @@ -5,7 +5,6 @@ import ( "image" "image/color" "image/draw" - "os" "sync" "github.com/disintegration/gift" @@ -84,8 +83,7 @@ func (e EPUBImageProcessor) Load() (images []epubimage.EPUBImage, err error) { e.EPUBOptions.Image.AutoSplitDoublePage && !e.EPUBOptions.Image.KeepDoublePageIfSplit) { if err = imgStorage.Add(img.EPUBImgPath(), img.Raw, e.Image.Quality); err != nil { _ = bar.Close() - utils.Printf("error with %s: %s", input.Name, err) - os.Exit(1) + utils.Fatalln("error with %s: %s", input.Name, err) } // do not keep raw image except for cover if img.Id > 0 { @@ -105,8 +103,7 @@ func (e EPUBImageProcessor) Load() (images []epubimage.EPUBImage, err error) { img = e.transformImage(input, i+1, b) if err = imgStorage.Add(img.EPUBImgPath(), img.Raw, e.Image.Quality); err != nil { _ = bar.Close() - utils.Printf("error with %s: %s", input.Name, err) - os.Exit(1) + utils.Fatalf("error with %s: %s", input.Name, err) } img.Raw = nil imageOutput <- img diff --git a/internal/pkg/utils/utils.go b/internal/pkg/utils/utils.go index da2d2b3..76607a1 100644 --- a/internal/pkg/utils/utils.go +++ b/internal/pkg/utils/utils.go @@ -10,10 +10,20 @@ func Printf(format string, a ...interface{}) { _, _ = fmt.Fprintf(os.Stderr, format, a...) } +func Fatalf(format string, args ...interface{}) { + Printf(format, args...) + os.Exit(1) +} + func Println(a ...interface{}) { _, _ = fmt.Fprintln(os.Stderr, a...) } +func Fatalln(a ...interface{}) { + Println(a...) + os.Exit(1) +} + func IntToString(i int) string { return strconv.FormatInt(int64(i), 10) } diff --git a/main.go b/main.go index 85e81a3..bd41f17 100644 --- a/main.go +++ b/main.go @@ -46,8 +46,7 @@ func main() { func version() { bi, ok := debug.ReadBuildInfo() if !ok { - utils.Println("failed to fetch current version") - os.Exit(1) + utils.Fatalln("failed to fetch current version") } githubTag := &latest.GithubTag{ @@ -56,8 +55,7 @@ func version() { } v, err := githubTag.Fetch() if err != nil || len(v.Versions) < 1 { - utils.Println("failed to fetch the latest version") - os.Exit(1) + utils.Fatalln("failed to fetch the latest version") } latestVersion := v.Versions[0] @@ -184,8 +182,7 @@ func generate(cmd *converter.Converter) { AppleBookCompatibility: cmd.Options.AppleBookCompatibility, }, }).Write(); err != nil { - utils.Printf("Error: %v\n", err) - os.Exit(1) + utils.Fatalf("Error: %v\n", err) } if !cmd.Options.Dry { cmd.Stats()