-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (85 loc) · 1.63 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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
_ "embed"
"github.com/gonejack/textbundle-to-epub/cmd"
"github.com/spf13/cobra"
)
var (
//go:embed cover.png
defaultCover []byte
cover *string
title *string
author *string
output *string
verbose = false
prog = &cobra.Command{
Use: "textbundle-to-epub [-o output] [--title title] [--cover cover] *.textbundle",
Short: "Command line tool for converting textbundles to epub.",
Run: func(c *cobra.Command, args []string) {
err := run(c, args)
if err != nil {
log.Fatal(err)
}
},
}
)
func init() {
log.SetOutput(os.Stdout)
prog.Flags().SortFlags = false
prog.PersistentFlags().SortFlags = false
cover = prog.PersistentFlags().StringP(
"cover",
"",
"",
"epub cover image",
)
title = prog.PersistentFlags().StringP(
"title",
"",
"TextBundles",
"epub title",
)
author = prog.PersistentFlags().StringP(
"author",
"",
"TextBundle to Epub",
"epub author",
)
output = prog.PersistentFlags().StringP(
"output",
"o",
"output.epub",
"output filename",
)
prog.PersistentFlags().BoolVarP(
&verbose,
"verbose",
"v",
false,
"verbose",
)
}
func run(c *cobra.Command, args []string) error {
_, err := os.Stat(*output)
if !os.IsNotExist(err) {
return fmt.Errorf("output file %s already exist", *output)
}
exec := cmd.TextBundleToEpub{
DefaultCover: defaultCover,
Cover: *cover,
Title: *title,
Author: *author,
Verbose: verbose,
}
if len(args) == 0 || args[0] == "*.textbundle" {
args, _ = filepath.Glob("*.textbundle")
}
return exec.Run(args, *output)
}
func main() {
_ = prog.Execute()
}