-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
134 lines (124 loc) · 3.2 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
/*
Endive is a tool to keep your epub library in great shape.
It can rename and organize your library from the epub metadata, and can keep
track of retail and non-retail versions.
*/
package main
import (
"fmt"
"os"
"os/signal"
"strconv"
"syscall"
"github.com/ttacon/chalk"
en "github.com/barsanuphe/endive/endive"
)
func main() {
fmt.Println(chalk.Bold.TextStyle("\n# # # E N D I V E # # #\n"))
// create main Endive struct
e, err := NewEndive()
if err != nil {
e.UI.Error("Could not create Endive: " + err.Error())
// if error other than usage elsewhere, remove lock.
if err != en.ErrorCannotLockDB {
en.RemoveLock()
}
os.Exit(-1)
}
defer e.UI.CloseLog()
defer en.RemoveLock()
defer e.Library.Close()
// handle interrupt
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
<-c
e.UI.Error("Interrupt!")
e.UI.Error("Stopping everything, saving what can be.")
e.Library.Close()
en.RemoveLock()
e.UI.CloseLog()
os.Exit(1)
}()
cli := CLI{}
if err := cli.parseArgs(e, os.Args[1:]); err != nil {
fmt.Println(incorrectInput)
e.UI.Error(err.Error())
return
}
if cli.builtInCommand {
// nothing to do
return
}
// now dealing with commands
if cli.showConfig {
e.UI.Display(e.Config.String())
} else if cli.checkCollection {
if err := e.Library.Check(); err != nil {
e.UI.Error("Check found modified files since import! " + err.Error())
} else {
e.UI.Info("All epubs checked successfully.")
}
} else if cli.refreshCollection {
e.UI.Display("Refreshing library...")
if renamed, err := e.Refresh(); err == nil {
e.UI.Display("Refresh done, renamed " + strconv.Itoa(renamed) + " epubs.")
} else {
e.UI.Error("Could not refresh collection.")
}
} else if cli.rebuildIndex {
if err := e.Library.RebuildIndex(); err != nil {
e.UI.Error(err.Error())
}
} else if cli.checkIndex {
if err := e.Library.CheckIndex(); err != nil {
e.UI.Error(err.Error())
}
} else if cli.importEpubs {
if cli.listImport {
listImportableEpubs(e, cli.importRetail)
} else {
importEpubs(e, cli.epubs, cli.importRetail)
}
} else if cli.export {
if len(cli.searchTerms) == 0 {
exportCollection(e, cli.collection, cli.exportDirectory)
} else {
exportFilter(e, cli.searchTerms, cli.exportDirectory)
}
} else if cli.info != "" {
switch cli.info {
case infoGeneral:
showInfo(e, nil)
case infoBook:
showInfo(e, cli.books[0])
default:
e.UI.Display(en.TabulateMap(cli.collectionMap, cli.info, numberOfBooksHeader))
}
} else if cli.review {
reviewBook(e, cli.books[0], cli.rating, cli.reviewText)
} else if cli.edit {
if cli.field != "" {
editMetadata(e, cli.books, cli.field)
} else {
editMetadata(e, cli.books)
}
} else if cli.reset {
if cli.field != "" {
refreshMetadata(e, cli.books, cli.field)
} else {
refreshMetadata(e, cli.books)
}
} else if cli.set {
if cli.field != "" {
editMetadata(e, cli.books, cli.field, cli.value)
} else {
setProgress(e, cli.books, cli.progress)
}
} else if cli.search {
search(e, cli.searchTerms, cli.firstN, cli.lastN, cli.sortBy)
} else if cli.list {
displayBooks(e.UI, cli.collection, cli.firstN, cli.lastN, cli.sortBy)
}
}