-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
418 lines (407 loc) · 12.6 KB
/
main.go
File metadata and controls
418 lines (407 loc) · 12.6 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package main
import (
"encoding/json"
"fmt"
"github.com/tidwall/buntdb"
"github.com/urfave/cli/v2"
"gitlab.com/go-classroom/todo/util"
"log"
"os"
"strconv"
)
type Entry struct {
Id string
Title string
Description string
Category string
Status string
}
func main() {
db, err := buntdb.Open("data.db")
if err != nil {
log.Fatal(err)
}
app := &cli.App{
Commands: []*cli.Command{
{
Name: "new",
Aliases: []string{"n"},
Usage: "Add a new entry to the list",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "category",
Aliases: []string{"c"},
Value: "other",
Usage: "Enter a category of the entry: fun, work or personal",
},
},
Action: func(cCtx *cli.Context) error {
//Checking if the user input is correct
if cCtx.String("c") != "fun" && cCtx.String("c") != "personal" && cCtx.String("c") != "work" && cCtx.String("c") != "other" {
fmt.Println("Please enter a correct category for the entry")
os.Exit(0)
}
//Using custom made key that auto increments according to the key of the last entry
new_key := 0
var titles []string
db.View(func(tx *buntdb.Tx) error {
fetched_entry := Entry{}
tx.Ascend("", func(key, value string) bool {
if err := json.Unmarshal([]byte(value), &fetched_entry); err != nil {
panic(err)
}
titles = append(titles, fetched_entry.Title)
new_key, _ = strconv.Atoi(key)
return true
})
return nil
})
//If not incremented it will be the same on as the one of the last entry
new_key++
var new_title, new_description string
fmt.Println("Enter the title of your entry:")
util.Scanner(&new_title)
//Check if the title provided already exists
for _, v := range titles {
if new_title == v {
fmt.Println("Title already exists")
os.Exit(0)
}
}
fmt.Println("Enter a description:")
util.Scanner(&new_description)
//Setting the status "active" as a default for new entries
db.Update(func(tx *buntdb.Tx) error {
tx.Set(fmt.Sprintf("%d", new_key), fmt.Sprintf(
`{"id": "%s", "title": "%s", "description" : "%s", "status": "active", "category": "%s"}`,
fmt.Sprintf("%d", new_key), new_title, new_description, cCtx.String("c")), nil)
return nil
})
defer db.Close()
return nil
},
},
{
Name: "delete",
Aliases: []string{"del"},
Usage: "Delete an entry from the list",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Value: "",
Usage: "Enter the id of the entry to be delete",
},
},
Action: func(cCtx *cli.Context) error {
//If the id flag is empty the process should terminate
if cCtx.String("id") == "" {
fmt.Println("Please enter the title of the entry to be delete")
os.Exit(0)
}
//Fetching all the entries to match the id given with the correct entry
var entries []Entry
db.View(func(tx *buntdb.Tx) error {
fetched_entry := Entry{}
tx.Ascend("", func(key, value string) bool {
if err := json.Unmarshal([]byte(value), &fetched_entry); err != nil {
panic(err)
}
entries = append(entries, fetched_entry)
return true
})
return nil
})
var del_key string
for _, v := range entries {
if cCtx.String("id") == v.Id {
del_key = v.Id
}
}
db.Update(func(tx *buntdb.Tx) error {
_, err := tx.Delete(del_key)
if err != nil {
fmt.Printf("Entry with id \"%s\" was not found\n", cCtx.String("id"))
} else {
fmt.Printf("Entry with id \"%s\" has been succesfully deleted\n", del_key)
}
return nil
})
defer db.Close()
return nil
},
},
{
Name: "edit",
Aliases: []string{"e"},
Usage: "Edit the title, description and/or category",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Value: "",
Usage: "Id of the entry to be deleted",
},
&cli.StringFlag{
Name: "field",
Aliases: []string{"f"},
Value: "all",
Usage: "Specify which field to edit: title, description or category",
},
},
Action: func(cCtx *cli.Context) error {
//If the id flag is empty the process should terminate
if cCtx.String("id") == "" {
fmt.Println("Please enter the id of the entry to be delete")
os.Exit(0)
}
//If the field flag was not correctly given the process should terminate
if cCtx.String("f") != "title" && cCtx.String("f") != "description" && cCtx.String("f") != "category" && cCtx.String("f") != "all" {
fmt.Println("Please enter a correct field to edit")
os.Exit(0)
}
//Fetching all the entries to match the id given with the entry to be edited
var entries []Entry
db.View(func(tx *buntdb.Tx) error {
fetched_entry := Entry{}
tx.Ascend("", func(key, value string) bool {
if err := json.Unmarshal([]byte(value), &fetched_entry); err != nil {
panic(err)
}
entries = append(entries, fetched_entry)
return true
})
return nil
})
var edit_entry Entry
id_found := false
for _, v := range entries {
if cCtx.String("id") == v.Id {
edit_entry.Title = v.Title
edit_entry.Description = v.Description
edit_entry.Category = v.Category
edit_entry.Status = v.Status
id_found = true
}
}
if id_found == false {
fmt.Println("The id given doesn't exist")
os.Exit(0)
}
switch cCtx.String("f") {
case "title":
var new_title string
fmt.Println("Enter the new title: ")
util.Scanner(&new_title)
edit_entry.Title = new_title
case "description":
var new_desc string
fmt.Println("Enter the new description: ")
util.Scanner(&new_desc)
edit_entry.Description = new_desc
case "category":
var new_cat string
fmt.Println("Enter the new category: ")
util.Scanner(&new_cat)
//Using an infinite loop to ensure the user input is correct
for {
if new_cat != "work" && new_cat != "fun" && new_cat != "personal" {
fmt.Println("Please enter the correct category(use only lowercase letters): ")
util.Scanner(&new_cat)
} else {
edit_entry.Category = new_cat
break
}
}
case "all":
var new_title, new_desc, new_cat string
fmt.Println("Enter the new title: ")
util.Scanner(&new_title)
edit_entry.Title = new_title
fmt.Println("Enter the new description: ")
util.Scanner(&new_desc)
edit_entry.Description = new_desc
fmt.Println("Enter the new category: ")
util.Scanner(&new_cat)
for {
if new_cat != "work" && new_cat != "fun" && new_cat != "personal" {
fmt.Println("Please enter the correct category: ")
util.Scanner(&new_cat)
} else {
edit_entry.Category = new_cat
break
}
}
default:
fmt.Println("Please enter a correct field to edit")
}
db.Update(func(tx *buntdb.Tx) error {
tx.Set(cCtx.String("id"), fmt.Sprintf(
`{"id": "%s", "title": "%s", "description" : "%s", "status": "%s", "category": "%s"}`,
cCtx.String("id"), edit_entry.Title, edit_entry.Description, edit_entry.Status, edit_entry.Category), nil)
return nil
})
defer db.Close()
return nil
},
},
{
Name: "list",
Aliases: []string{"ls"},
Usage: "List all the entries",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "status",
Aliases: []string{"s"},
Value: "all",
Usage: "Filter the returning list through the status given: abandoned, active, done",
},
&cli.StringFlag{
Name: "category",
Aliases: []string{"c"},
Value: "all",
Usage: "Filter the returning list through the category given: fun, work, personal",
},
&cli.StringFlag{
Name: "id",
Value: "all",
Usage: "Return information about a specific entry",
},
},
Action: func(cCtx *cli.Context) error {
var entries []Entry
db.View(func(tx *buntdb.Tx) error {
fetched_entry := Entry{}
tx.Ascend("", func(key, value string) bool {
if err := json.Unmarshal([]byte(value), &fetched_entry); err != nil {
panic(err)
}
entries = append(entries, fetched_entry)
return true
})
return nil
})
//Checking if the user used a title flag
if cCtx.String("id") == "all" {
//Using 2 new variables to potentially filter twice through the list of entries, once for category flag and once for status flag
var entries1 []Entry
var entries2 []Entry
//Checking if the user put the correct input
if cCtx.String("c") != "fun" && cCtx.String("c") != "work" && cCtx.String("c") != "all" && cCtx.String("c") != "personal" {
panic("Wrong category parameter given")
}
if cCtx.String("s") != "active" && cCtx.String("s") != "done" && cCtx.String("s") != "all" && cCtx.String("s") != "abandoned" {
panic("Wrong status parameter given")
}
//Filtering through all the entries using the category flag
if cCtx.String("c") == "fun" || cCtx.String("c") == "personal" || cCtx.String("c") == "work" {
for _, v := range entries {
if v.Category == cCtx.String("c") {
entries1 = append(entries1, v)
}
}
} else {
//If no category flag was provided then the list remains the same
entries1 = entries
}
//Filtering through all the entries using the status flag
if cCtx.String("s") == "abandoned" || cCtx.String("s") == "done" || cCtx.String("s") == "active" {
for _, v := range entries1 {
if v.Status == cCtx.String("s") {
entries2 = append(entries2, v)
}
}
} else {
entries2 = entries1
}
fmt.Printf("Here is a list of your entries: \n")
for _, v := range entries2 {
fmt.Printf("%s: %s\n", v.Id, v.Title)
}
} else {
id_found := false
for _, v := range entries {
if cCtx.String("id") == v.Id {
id_found = true
fmt.Printf("Title: %s\nDescription: %s\nCategory: %s\nStatus: %s\n", v.Title, v.Description, v.Category, v.Status)
break
}
}
if id_found == false {
fmt.Printf("Entry with id \"%s\" was not found\n", cCtx.String("id"))
}
}
return nil
},
},
{
Name: "update",
Aliases: []string{"u"},
Usage: "Update the status of an entry",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Value: "",
Usage: "Enter the id of the entry",
},
&cli.StringFlag{
Name: "status",
Aliases: []string{"s"},
Value: "",
Usage: "Enter the new status of the entry: done, active, abandoned",
},
},
Action: func(cCtx *cli.Context) error {
//If the id is empty the process should terminate
if cCtx.String("id") == "" {
fmt.Println("Please enter the id of the entry")
os.Exit(0)
}
//If the status given is empty or not correct the process should terminate
if cCtx.String("s") != "done" && cCtx.String("s") != "abandoned" && cCtx.String("s") != "active" {
fmt.Println("Please enter a correct status")
os.Exit(0)
}
//Fetching all the entries to match the id given with the entry to be updated
var entries []Entry
db.View(func(tx *buntdb.Tx) error {
fetched_entry := Entry{}
tx.Ascend("", func(key, value string) bool {
if err := json.Unmarshal([]byte(value), &fetched_entry); err != nil {
panic(err)
}
entries = append(entries, fetched_entry)
return true
})
return nil
})
id_found := false
var entry_title, entry_desc, entry_cat string
for _, v := range entries {
if cCtx.String("id") == v.Id {
entry_title = v.Title
entry_desc = v.Description
entry_cat = v.Category
id_found = true
break
}
}
//If the id given was not found the process should terminate
if id_found == false {
fmt.Printf("Entry with id \"%s\" was not found\n", cCtx.String("id"))
os.Exit(0)
}
db.Update(func(tx *buntdb.Tx) error {
tx.Set(cCtx.String("id"), fmt.Sprintf(
`{"id": "%s", "title": "%s", "description" : "%s", "status": "%s", "category": "%s"}`,
cCtx.String("id"), entry_title, entry_desc, cCtx.String("s"), entry_cat), nil)
return nil
})
defer db.Close()
return nil
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}