-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
134 lines (115 loc) · 2.86 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
Convert CBZ/CBR/Dir into EPUB for e-reader devices (Kindle Devices, ...)
My goal is to make a simple, cross-platform, and fast tool to convert comics into EPUB.
EPUB is now support by Amazon through [SendToKindle](https://www.amazon.com/gp/sendtokindle/), by Email or by using the App. So I've made it simple to support the size limit constraint of those services.
*/
package main
import (
"encoding/json"
"os"
"runtime/debug"
"github.com/tcnksm/go-latest"
"github.com/celogeek/go-comic-converter/v2/internal/pkg/converter"
"github.com/celogeek/go-comic-converter/v2/internal/pkg/epub"
"github.com/celogeek/go-comic-converter/v2/internal/pkg/utils"
)
func main() {
cmd := converter.New()
if err := cmd.LoadConfig(); err != nil {
cmd.Fatal(err)
}
cmd.InitParse()
cmd.Parse()
switch {
case cmd.Options.Version:
version()
case cmd.Options.Save:
save(cmd)
case cmd.Options.Show:
show(cmd)
case cmd.Options.Reset:
reset(cmd)
default:
generate(cmd)
}
}
func version() {
bi, ok := debug.ReadBuildInfo()
if !ok {
utils.Fatalln("failed to fetch current version")
}
githubTag := &latest.GithubTag{
Owner: "celogeek",
Repository: "go-comic-converter",
}
v, err := githubTag.Fetch()
if err != nil {
utils.Fatalln("failed to fetch the latest version")
}
if len(v.Versions) < 1 {
utils.Fatalln("no versions found")
}
latestVersion := v.Versions[0]
utils.Printf(`go-comic-converter
Path : %s
Sum : %s
Version : %s
Available Version: %s
To install the latest version:
$ go install github.com/celogeek/go-comic-converter/v%d@%s
`,
bi.Main.Path,
bi.Main.Sum,
bi.Main.Version,
latestVersion.Original(),
latestVersion.Segments()[0],
latestVersion.Original(),
)
}
func save(cmd *converter.Converter) {
if err := cmd.Options.SaveConfig(); err != nil {
cmd.Fatal(err)
}
utils.Printf(
"%s%s\n\nSaving to %s\n",
cmd.Options.Header(),
cmd.Options.ShowConfig(),
cmd.Options.FileName(),
)
}
func show(cmd *converter.Converter) {
utils.Println(cmd.Options.Header(), cmd.Options.ShowConfig())
}
func reset(cmd *converter.Converter) {
if err := cmd.Options.ResetConfig(); err != nil {
cmd.Fatal(err)
}
utils.Printf(
"%s%s\n\nReset default to %s\n",
cmd.Options.Header(),
cmd.Options.ShowConfig(),
cmd.Options.FileName(),
)
}
func generate(cmd *converter.Converter) {
if err := cmd.Validate(); err != nil {
cmd.Fatal(err)
}
if profile := cmd.Options.GetProfile(); profile != nil {
cmd.Options.Image.View.Width = profile.Width
cmd.Options.Image.View.Height = profile.Height
}
if cmd.Options.Json {
_ = json.NewEncoder(os.Stdout).Encode(map[string]any{
"type": "options", "data": cmd.Options,
})
} else {
utils.Println(cmd.Options)
}
if err := epub.New(cmd.Options.EPUBOptions).Write(); err != nil {
utils.Fatalf("Error: %v\n", err)
}
if !cmd.Options.Dry {
cmd.Stats()
}
}