-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
190 lines (170 loc) · 5.22 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
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/epuboptions"
"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 || len(v.Versions) < 1 {
utils.Fatalln("failed to fetch the latest version")
}
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(),
)
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 cmd.Options.Json {
_ = json.NewEncoder(os.Stdout).Encode(map[string]any{
"type": "options", "data": cmd.Options,
})
} else {
utils.Println(cmd.Options)
}
profile := cmd.Options.GetProfile()
if err := epub.New(epuboptions.EPUBOptions{
Input: cmd.Options.Input,
Output: cmd.Options.Output,
LimitMb: cmd.Options.LimitMb,
Title: cmd.Options.Title,
TitlePage: cmd.Options.TitlePage,
Author: cmd.Options.Author,
StripFirstDirectoryFromToc: cmd.Options.StripFirstDirectoryFromToc,
SortPathMode: cmd.Options.SortPathMode,
Workers: cmd.Options.Workers,
Dry: cmd.Options.Dry,
DryVerbose: cmd.Options.DryVerbose,
Quiet: cmd.Options.Quiet,
Json: cmd.Options.Json,
Image: epuboptions.Image{
Crop: epuboptions.Crop{
Enabled: cmd.Options.Crop,
Left: cmd.Options.CropRatioLeft,
Up: cmd.Options.CropRatioUp,
Right: cmd.Options.CropRatioRight,
Bottom: cmd.Options.CropRatioBottom,
Limit: cmd.Options.CropLimit,
SkipIfLimitReached: cmd.Options.CropSkipIfLimitReached,
},
Quality: cmd.Options.Quality,
Brightness: cmd.Options.Brightness,
Contrast: cmd.Options.Contrast,
AutoContrast: cmd.Options.AutoContrast,
AutoRotate: cmd.Options.AutoRotate,
AutoSplitDoublePage: cmd.Options.AutoSplitDoublePage,
KeepDoublePageIfSplit: cmd.Options.KeepDoublePageIfSplit,
KeepSplitDoublePageAspect: cmd.Options.KeepSplitDoublePageAspect,
NoBlankImage: cmd.Options.NoBlankImage,
Manga: cmd.Options.Manga,
HasCover: cmd.Options.HasCover,
View: epuboptions.View{
Width: profile.Width,
Height: profile.Height,
AspectRatio: cmd.Options.AspectRatio,
PortraitOnly: cmd.Options.PortraitOnly,
Color: epuboptions.Color{
Foreground: cmd.Options.ForegroundColor,
Background: cmd.Options.BackgroundColor,
},
},
GrayScale: cmd.Options.Grayscale,
GrayScaleMode: cmd.Options.GrayscaleMode,
Resize: !cmd.Options.NoResize,
Format: cmd.Options.Format,
AppleBookCompatibility: cmd.Options.AppleBookCompatibility,
},
}).Write(); err != nil {
utils.Fatalf("Error: %v\n", err)
}
if !cmd.Options.Dry {
cmd.Stats()
}
}