-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (52 loc) · 1.36 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
package main
import (
"flag"
"fmt"
"regexp"
"ebooker/data"
"ebooker/downloader"
"ebooker/maker"
)
// Define flags
var input = flag.String("input", "-", "The input textproto to process.")
var dir = flag.String("dir", ".", "The directory to store output in.")
var download = flag.Bool("download", false, "Whether to download the content or assume it already exists.")
var generate = flag.Bool("generate", false, "Whether to generate the ebook.")
var textprotoRe = regexp.MustCompile(`\.textproto$`)
func main() {
flag.Parse()
fmt.Println("ebooker starting...")
book, err := getBook()
if err != nil {
fmt.Printf("Error reading book data: %v\n", err)
return
}
fmt.Printf("Processing book %s (by %s)\n", book.Title, book.Author)
if *generate {
// Generate output
output := textprotoRe.ReplaceAllString(*input, ".epub")
err := maker.Make(book, output)
if err != nil {
fmt.Printf("Error generating book data: %v\n", err)
return
}
} else {
err = book.Write(*dir)
if err != nil {
fmt.Printf("Error writng book data to %s: %v\n", *dir, err)
return
}
fmt.Printf("Wrote data for %s to %s\n", book.Title, *dir)
}
}
func getBook() (*data.Book, error) {
if *download {
book, err := downloader.DownloadBook(*input)
if err != nil {
return nil, err
}
return book, nil
}
// Assume the book data is in dir.
return data.ReadBook(*dir)
}