-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
258 lines (231 loc) · 6.41 KB
/
cli.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package keeper
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"time"
)
func WaitForCommand(store Store) {
var command string
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print("> ")
if scanner.Scan() {
command = scanner.Text()
}
switch command {
case string(Add):
handleAdd(store, scanner)
case string(List):
handleList(store)
case string(Delete):
handleDelete(store, scanner)
case string(Update):
handleUpdate(store, scanner)
case string(Help):
handleHelp()
case string(Exit):
os.Exit(0)
default:
fmt.Printf("%s is not a valid command\n", command)
}
}
}
type commandType string
const (
Add commandType = "add"
List commandType = "list"
Delete commandType = "delete"
Update commandType = "update"
Help commandType = "help"
Exit commandType = "exit"
)
func printAndScan(msg string, scanner *bufio.Scanner) string {
fmt.Print(msg)
if scanner.Scan() {
return scanner.Text()
}
return ""
}
func parseDateInput(input string) (time.Time, error) {
if strings.HasPrefix(input, "???") {
//user want's unspecified date
return time.Time{}, nil
} else if len(input) > 0 {
// user provided a date, try to parse it
if d, err := time.Parse(DateFormat, input); err != nil {
return time.Time{}, err
} else {
// date has been parsed successfully, break from the loop
return d, nil
}
}
// user did not provide any date input, use todays date
return time.Now(), nil
}
func handleAdd(store Store, scanner *bufio.Scanner) {
book := BookEntry{}
setBookTitle(&book, scanner)
setBookAuthor(&book, scanner)
setBookDate("start", &book, scanner)
setBookDate("end", &book, scanner)
setBookState(&book, scanner)
if err := store.AddBookEntry(book); err != nil {
fmt.Printf("Failed to add a book to the collection\n%s\n", err)
}
}
func handleList(store Store) {
if err := store.PrintBookEntries(); err != nil {
fmt.Println(err)
}
}
func handleDelete(store Store, scanner *bufio.Scanner) {
idx := getBookIdx(store, scanner)
book, err := store.GetBookWithIndex(idx)
if err != nil {
fmt.Printf("A book with the index %d doesn't exists.\n", idx)
}
if err := store.DeleteBookEntry(itob(book.ID)); err != nil {
fmt.Printf("Failed to delete this book.\n%s\n", err)
}
}
func handleUpdate(store Store, scanner *bufio.Scanner) {
bookIdx := getBookIdx(store, scanner)
oldBookEntry, err := store.GetBookWithIndex(bookIdx)
if err != nil {
fmt.Println("Failed to retrieve the book")
return
}
newBookEntry := updateBookProperty(oldBookEntry, scanner)
if err := store.UpdateBookEntry(itob(oldBookEntry.ID), newBookEntry); err != nil {
fmt.Println("Failed to update this book.")
}
}
func getBookIdx(store Store, scanner *bufio.Scanner) int {
maxIdx := store.GetNumberOfBookEntries()
return askUntilTheConditionHasBeenMet("Please select the book index: ", scanner, func(s string) (interface{}, error) {
i, err := strconv.Atoi(s)
if err != nil {
return 0, fmt.Errorf("Cannot parse %s into a number. Please try again.\n", s)
} else {
if i > 0 && i <= maxIdx {
return i, nil
} else {
return 0, fmt.Errorf("%d is not a valid book index. Please try again.\n", i)
}
}
}).(int)
}
func updateBookProperty(oldBook BookEntry, scanner *bufio.Scanner) BookEntry {
msg := "Please select the property you want to update:1. Title\n2. Author\n3. Date Start\n4. Date End\n5. Book State\nChoice: "
updatedBook := oldBook
var _ = askUntilTheConditionHasBeenMet(msg, scanner, func(s string) (interface{}, error) {
switch s {
case "1":
setBookTitle(&updatedBook, scanner)
return nil, nil
case "2":
setBookAuthor(&updatedBook, scanner)
return nil, nil
case "3":
setBookDate("start", &updatedBook, scanner)
return nil, nil
case "4":
setBookDate("end", &updatedBook, scanner)
return nil, nil
case "5":
setBookState(&updatedBook, scanner)
return nil, nil
default:
return nil, fmt.Errorf("%s is not a valid book property. Please try again.\n", s)
}
})
return updatedBook
}
func setBookTitle(book *BookEntry, scanner *bufio.Scanner) {
newTitle := askUntilTheConditionHasBeenMet("Title: ", scanner, func(s string) (interface{}, error) {
if len(s) > 0 {
return s, nil
} else {
return "", fmt.Errorf("Cannot use an empty string as a book title. Please try again.\n")
}
}).(string)
book.Title = newTitle
}
func setBookAuthor(book *BookEntry, scanner *bufio.Scanner) {
setBookState := askUntilTheConditionHasBeenMet("Author: ", scanner, func(s string) (interface{}, error) {
if len(s) > 0 {
return s, nil
} else {
return "", fmt.Errorf("Cannot use an empty string as a book author. Please try again.\n")
}
}).(string)
book.Author = setBookState
}
func getBookState(scanner *bufio.Scanner) BookState {
msg := "Select a book state:\n1. Reading\n2. Finished\n3. Dropped\n4. Suspended\n5. Re reading\nChoice: "
st := askUntilTheConditionHasBeenMet(msg, scanner, func(s string) (interface{}, error) {
switch s {
case "1":
return Reading, nil
case "2":
return Finished, nil
case "3":
return Dropped, nil
case "4":
return Suspended, nil
case "5":
return ReRead, nil
default:
return nil, fmt.Errorf("%s is not a valid book state. Please select a number from 1 to 5.\n", s)
}
}).(BookState)
return st
}
func setBookDate(dateType string, book *BookEntry, scanner *bufio.Scanner) {
var msg string
if dateType == "start" {
msg = "Start Date: "
} else {
msg = "End Date: "
}
newDate := askUntilTheConditionHasBeenMet(msg, scanner, func(s string) (interface{}, error) {
d, err := parseDateInput(s)
if err != nil {
return "", fmt.Errorf("Couldn't parse date input. Please try again.\n")
} else {
return d, nil
}
}).(time.Time)
if dateType == "start" {
book.DateStart = newDate
} else {
book.DateEnd = newDate
}
}
func setBookState(book *BookEntry, scanner *bufio.Scanner) {
newState := getBookState(scanner)
book.State = newState
}
func askUntilTheConditionHasBeenMet(msg string, scanner *bufio.Scanner, callback func(s string) (interface{}, error)) interface{} {
for {
input := printAndScan(msg, scanner)
if r, err := callback(input); err == nil {
return r
} else {
fmt.Print(err)
}
}
}
func handleHelp() {
fmt.Print(
"Available commands:\n",
"list: list books in the database\n",
"add: adds a book to the database\n",
"delete: removes a book from the database\n",
"update: updates a selected book\n",
"help: prints this help\n",
"exit: exits from the program\n")
}