-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbooknotes.go
executable file
·95 lines (88 loc) · 2.73 KB
/
booknotes.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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/bgrohman/booknotes/config"
"github.com/bgrohman/booknotes/core"
"github.com/bgrohman/booknotes/printing"
)
func help() {
fmt.Println("Usage:")
fmt.Println("------")
fmt.Println("booknotes <command> [file]")
fmt.Println("")
fmt.Println("Commands:")
fmt.Println("---------")
fmt.Println("list Prints title, subtitle, author, and metadata for each book")
fmt.Println("full Same as \"list\" but includes the full notes, too")
fmt.Println("authors Prints all authors in alphabetical order")
fmt.Println("titles Prints all titles in alphabetical order")
fmt.Println("words Prints words and word counts")
fmt.Println("random Prints a random note")
fmt.Println("help Prints this help message")
fmt.Println("")
fmt.Println("Options:")
fmt.Println("--------")
fmt.Println("file Optional file path to process instead of all books")
fmt.Println("")
fmt.Println("Environment Variables:")
fmt.Println("----------------------")
fmt.Println("BOOKNOTES_DIRECTORY Path to directory containing book note files")
fmt.Println("BOOKNOTES_MAX_LINE_LENGTH Maximum number of columns when printing notes")
fmt.Println("")
}
func getFilePath(file string) string {
filePath, err := filepath.Abs(file)
if err == nil {
_, err = os.Stat(filePath)
if err != nil {
filePath = filepath.Join(config.GetConfig().Directory, file)
filePath, err = filepath.Abs(filePath)
if err == nil {
_, err = os.Stat(filePath)
if err != nil {
fmt.Println("Invalid file path:", file)
filePath = ""
}
} else {
fmt.Println("Invalid file path:", file)
filePath = ""
}
}
} else {
fmt.Println("Invalid file path:", file)
filePath = ""
}
return filePath
}
func main() {
args := os.Args[1:]
if len(args) > 0 {
file := ""
if len(args) >= 2 {
file = getFilePath(args[1])
}
switch args[0] {
case "list":
core.List(false, file)
case "full":
core.List(true, file)
case "authors":
core.PrintSortedProperties("author")
case "titles":
core.PrintSortedProperties("title")
case "words":
core.WordCount(file)
case "random":
printing.PrintBanner()
core.RandomNote(file)
case "help":
printing.PrintBanner()
help()
}
} else {
printing.PrintBanner()
help()
}
}