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