mirror of
https://github.com/celogeek/go-comic-converter.git
synced 2025-06-25 22:59:56 +02:00
factor stderr printing
This commit is contained in:
parent
0dde6e02a4
commit
f4501753c5
@ -19,6 +19,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/converter/options"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/utils"
|
||||
)
|
||||
|
||||
type Converter struct {
|
||||
@ -44,17 +45,17 @@ func New() *Converter {
|
||||
var cmdOutput strings.Builder
|
||||
cmd.SetOutput(&cmdOutput)
|
||||
cmd.Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "Usage of %s:\n", filepath.Base(os.Args[0]))
|
||||
utils.Printf("Usage of %s:\n", filepath.Base(os.Args[0]))
|
||||
for _, o := range conv.order {
|
||||
switch v := o.(type) {
|
||||
case converterOrderSection:
|
||||
fmt.Fprintf(os.Stderr, "\n%s:\n", o.Value())
|
||||
utils.Printf("\n%s:\n", o.Value())
|
||||
case converterOrderName:
|
||||
fmt.Fprintln(os.Stderr, conv.Usage(v.isString, cmd.Lookup(v.Value())))
|
||||
utils.Println(conv.Usage(v.isString, cmd.Lookup(v.Value())))
|
||||
}
|
||||
}
|
||||
if cmdOutput.Len() > 0 {
|
||||
fmt.Fprintf(os.Stderr, "\nError: %s", cmdOutput.String())
|
||||
utils.Printf("\nError: %s", cmdOutput.String())
|
||||
}
|
||||
}
|
||||
|
||||
@ -401,7 +402,7 @@ func (c *Converter) Validate() error {
|
||||
// Fatal Helper to show usage, err and exit 1
|
||||
func (c *Converter) Fatal(err error) {
|
||||
c.Cmd.Usage()
|
||||
fmt.Fprintf(os.Stderr, "\nError: %s\n", err)
|
||||
utils.Printf("\nError: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@ -420,8 +421,7 @@ func (c *Converter) Stats() {
|
||||
},
|
||||
})
|
||||
} else {
|
||||
fmt.Fprintf(
|
||||
os.Stderr,
|
||||
utils.Printf(
|
||||
"Completed in %s, Memory usage %d Mb\n",
|
||||
elapse,
|
||||
mem.Sys/1024/1024,
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"archive/zip"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
@ -13,6 +12,8 @@ import (
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
|
||||
epubimage "github.com/celogeek/go-comic-converter/v2/internal/epub/image"
|
||||
epubimageprocessor "github.com/celogeek/go-comic-converter/v2/internal/epub/imageprocessor"
|
||||
epuboptions "github.com/celogeek/go-comic-converter/v2/internal/epub/options"
|
||||
@ -20,7 +21,7 @@ import (
|
||||
epubtemplates "github.com/celogeek/go-comic-converter/v2/internal/epub/templates"
|
||||
epubtree "github.com/celogeek/go-comic-converter/v2/internal/epub/tree"
|
||||
epubzip "github.com/celogeek/go-comic-converter/v2/internal/epub/zip"
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/utils"
|
||||
)
|
||||
|
||||
type EPub struct {
|
||||
@ -418,12 +419,12 @@ func (e *EPub) Write() error {
|
||||
|
||||
if e.Dry {
|
||||
p := epubParts[0]
|
||||
fmt.Fprintf(os.Stderr, "TOC:\n - %s\n%s\n", e.Title, e.getTree(p.Images, true))
|
||||
utils.Printf("TOC:\n - %s\n%s\n", e.Title, e.getTree(p.Images, true))
|
||||
if e.DryVerbose {
|
||||
if e.Image.HasCover {
|
||||
fmt.Fprintf(os.Stderr, "Cover:\n%s\n", e.getTree([]*epubimage.Image{p.Cover}, false))
|
||||
utils.Printf("Cover:\n%s\n", e.getTree([]*epubimage.Image{p.Cover}, false))
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "Files:\n%s\n", e.getTree(p.Images, false))
|
||||
utils.Printf("Files:\n%s\n", e.getTree(p.Images, false))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -469,7 +470,7 @@ func (e *EPub) Write() error {
|
||||
}
|
||||
bar.Close()
|
||||
if !e.Json {
|
||||
fmt.Fprintln(os.Stderr)
|
||||
utils.Println()
|
||||
}
|
||||
|
||||
// display corrupted images
|
||||
@ -477,17 +478,17 @@ func (e *EPub) Write() error {
|
||||
for pId, part := range epubParts {
|
||||
if pId == 0 && e.Image.HasCover && part.Cover.Error != nil {
|
||||
hasError = true
|
||||
fmt.Fprintf(os.Stderr, "Error on image %s: %v\n", filepath.Join(part.Cover.Path, part.Cover.Name), part.Cover.Error)
|
||||
utils.Printf("Error on image %s: %v\n", filepath.Join(part.Cover.Path, part.Cover.Name), part.Cover.Error)
|
||||
}
|
||||
for _, img := range part.Images {
|
||||
if img.Part == 0 && img.Error != nil {
|
||||
hasError = true
|
||||
fmt.Fprintf(os.Stderr, "Error on image %s: %v\n", filepath.Join(img.Path, img.Name), img.Error)
|
||||
utils.Printf("Error on image %s: %v\n", filepath.Join(img.Path, img.Name), img.Error)
|
||||
}
|
||||
}
|
||||
}
|
||||
if hasError {
|
||||
fmt.Fprintln(os.Stderr)
|
||||
utils.Println()
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -16,6 +16,7 @@ import (
|
||||
epuboptions "github.com/celogeek/go-comic-converter/v2/internal/epub/options"
|
||||
epubprogress "github.com/celogeek/go-comic-converter/v2/internal/epub/progress"
|
||||
epubzip "github.com/celogeek/go-comic-converter/v2/internal/epub/zip"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/utils"
|
||||
)
|
||||
|
||||
type EPUBImageProcessor struct {
|
||||
@ -84,7 +85,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
|
||||
e.Options.Image.AutoSplitDoublePage && !e.Options.Image.KeepDoublePageIfSplit) {
|
||||
if err = imgStorage.Add(img.EPUBImgPath(), img.Raw, e.Image.Quality); err != nil {
|
||||
bar.Close()
|
||||
fmt.Fprintf(os.Stderr, "error with %s: %s", input.Name, err)
|
||||
utils.Printf("error with %s: %s", input.Name, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
// do not keep raw image except for cover
|
||||
@ -105,7 +106,7 @@ func (e *EPUBImageProcessor) Load() (images []*epubimage.Image, err error) {
|
||||
img = e.transformImage(input, i+1, b)
|
||||
if err = imgStorage.Add(img.EPUBImgPath(), img.Raw, e.Image.Quality); err != nil {
|
||||
bar.Close()
|
||||
fmt.Fprintf(os.Stderr, "error with %s: %s", input.Name, err)
|
||||
utils.Printf("error with %s: %s", input.Name, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
img.Raw = nil
|
||||
|
@ -20,12 +20,14 @@ import (
|
||||
"golang.org/x/image/font/gofont/gomonobold"
|
||||
_ "golang.org/x/image/webp"
|
||||
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/sortpath"
|
||||
"github.com/fogleman/gg"
|
||||
"github.com/golang/freetype/truetype"
|
||||
"github.com/nwaples/rardecode/v2"
|
||||
pdfimage "github.com/raff/pdfreader/image"
|
||||
"github.com/raff/pdfreader/pdfread"
|
||||
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/sortpath"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/utils"
|
||||
)
|
||||
|
||||
type task struct {
|
||||
@ -315,7 +317,7 @@ func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *task, err
|
||||
if isSolid && !e.Dry {
|
||||
r, rerr := rardecode.OpenReader(e.Input)
|
||||
if rerr != nil {
|
||||
fmt.Fprintf(os.Stderr, "\nerror processing image %s: %s\n", e.Input, rerr)
|
||||
utils.Printf("\nerror processing image %s: %s\n", e.Input, rerr)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer r.Close()
|
||||
@ -325,14 +327,14 @@ func (e *EPUBImageProcessor) loadCbr() (totalImages int, output chan *task, err
|
||||
if rerr == io.EOF {
|
||||
break
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "\nerror processing image %s: %s\n", f.Name, rerr)
|
||||
utils.Printf("\nerror processing image %s: %s\n", f.Name, rerr)
|
||||
os.Exit(1)
|
||||
}
|
||||
if i, ok := indexedNames[f.Name]; ok {
|
||||
var b bytes.Buffer
|
||||
_, rerr = io.Copy(&b, r)
|
||||
if rerr != nil {
|
||||
fmt.Fprintf(os.Stderr, "\nerror processing image %s: %s\n", f.Name, rerr)
|
||||
utils.Printf("\nerror processing image %s: %s\n", f.Name, rerr)
|
||||
os.Exit(1)
|
||||
}
|
||||
jobs <- &job{i, f.Name, func() (io.ReadCloser, error) {
|
||||
|
@ -7,6 +7,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/schollz/progressbar/v3"
|
||||
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/utils"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
@ -38,7 +40,7 @@ func New(o Options) EpubProgress {
|
||||
progressbar.OptionSetWriter(os.Stderr),
|
||||
progressbar.OptionThrottle(65*time.Millisecond),
|
||||
progressbar.OptionOnCompletion(func() {
|
||||
fmt.Fprint(os.Stderr, "\n")
|
||||
utils.Println()
|
||||
}),
|
||||
progressbar.OptionSetDescription(fmt.Sprintf(fmtDesc, o.CurrentJob, o.TotalJob, o.Description)),
|
||||
progressbar.OptionSetWidth(60),
|
||||
|
14
internal/utils/utils.go
Normal file
14
internal/utils/utils.go
Normal file
@ -0,0 +1,14 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Printf(format string, a ...interface{}) {
|
||||
_, _ = fmt.Fprintf(os.Stderr, format, a...)
|
||||
}
|
||||
|
||||
func Println(a ...interface{}) {
|
||||
_, _ = fmt.Fprintln(os.Stderr, a...)
|
||||
}
|
20
main.go
20
main.go
@ -9,7 +9,6 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
|
||||
@ -18,6 +17,7 @@ import (
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/converter"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/epub"
|
||||
epuboptions "github.com/celogeek/go-comic-converter/v2/internal/epub/options"
|
||||
"github.com/celogeek/go-comic-converter/v2/internal/utils"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -31,7 +31,7 @@ func main() {
|
||||
if cmd.Options.Version {
|
||||
bi, ok := debug.ReadBuildInfo()
|
||||
if !ok {
|
||||
fmt.Fprintln(os.Stderr, "failed to fetch current version")
|
||||
utils.Println("failed to fetch current version")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@ -41,12 +41,12 @@ func main() {
|
||||
}
|
||||
v, err := githubTag.Fetch()
|
||||
if err != nil || len(v.Versions) < 1 {
|
||||
fmt.Fprintln(os.Stderr, "failed to fetch the latest version")
|
||||
utils.Println("failed to fetch the latest version")
|
||||
os.Exit(1)
|
||||
}
|
||||
latestVersion := v.Versions[0]
|
||||
|
||||
fmt.Fprintf(os.Stderr, `go-comic-converter
|
||||
utils.Printf(`go-comic-converter
|
||||
Path : %s
|
||||
Sum : %s
|
||||
Version : %s
|
||||
@ -67,8 +67,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
|
||||
|
||||
if cmd.Options.Save {
|
||||
cmd.Options.SaveConfig()
|
||||
fmt.Fprintf(
|
||||
os.Stderr,
|
||||
utils.Printf(
|
||||
"%s%s\n\nSaving to %s\n",
|
||||
cmd.Options.Header(),
|
||||
cmd.Options.ShowConfig(),
|
||||
@ -78,14 +77,13 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
|
||||
}
|
||||
|
||||
if cmd.Options.Show {
|
||||
fmt.Fprintln(os.Stderr, cmd.Options.Header(), cmd.Options.ShowConfig())
|
||||
utils.Println(cmd.Options.Header(), cmd.Options.ShowConfig())
|
||||
return
|
||||
}
|
||||
|
||||
if cmd.Options.Reset {
|
||||
cmd.Options.ResetConfig()
|
||||
fmt.Fprintf(
|
||||
os.Stderr,
|
||||
utils.Printf(
|
||||
"%s%s\n\nReset default to %s\n",
|
||||
cmd.Options.Header(),
|
||||
cmd.Options.ShowConfig(),
|
||||
@ -103,7 +101,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
|
||||
"type": "options", "data": cmd.Options,
|
||||
})
|
||||
} else {
|
||||
fmt.Fprintln(os.Stderr, cmd.Options)
|
||||
utils.Println(cmd.Options)
|
||||
}
|
||||
|
||||
profile := cmd.Options.GetProfile()
|
||||
@ -159,7 +157,7 @@ $ go install github.com/celogeek/go-comic-converter/v%d@%s
|
||||
AppleBookCompatibility: cmd.Options.AppleBookCompatibility,
|
||||
},
|
||||
}).Write(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
utils.Printf("Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if !cmd.Options.Dry {
|
||||
|
Loading…
x
Reference in New Issue
Block a user