/
bookmark.go
92 lines (73 loc) · 1.85 KB
/
bookmark.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
package main
import (
"bufio"
"fmt"
"os"
)
type bookmark struct {
id int
url string
title string
comment string
tags []string
}
func deleteBookmark(bookmarkToDelete bookmark, bookmarks []bookmark, fileLocation *string) {
reader := bufio.NewReader(os.Stdin)
fmt.Printf(printBookmark(bookmarkToDelete))
fmt.Printf("Delete bookmark (y/N)? ")
response, err := reader.ReadString('\n')
check(err)
if response == "y\n" {
removeFromFile(bookmarks, fileLocation)
}
return
}
func addBookmark(fileLocation *string) {
var bookmarkID int
reader := bufio.NewReader(os.Stdin)
fmt.Print("Add a new bookmark (Y/n)? ")
response, err := reader.ReadString('\n')
check(err)
if response == "n\n" {
return
}
fmt.Print("Add a link: ")
url, err := reader.ReadString('\n')
check(err)
url = url[:len(url)-1]
fmt.Print("Add a title for that link: ")
title, err := reader.ReadString('\n')
check(err)
title = title[:len(title)-1]
fmt.Print("Add a comment: ")
comment, err := reader.ReadString('\n')
check(err)
comment = comment[:len(comment)-1]
var tags []string
tags = getTags(reader, &tags)
contentString := getFileContent(fileLocation)
if contentString != "" {
parsedBookmarks := parseBookmarksFile(fileLocation)
bookmarkID = getLastID(parsedBookmarks) + 1
} else {
bookmarkID = 0
}
newBookmark := bookmark{id: bookmarkID, url: url, title: title, comment: comment, tags: tags}
writeBookmark(newBookmark, fileLocation)
}
func getTags(reader *bufio.Reader, tags *[]string) []string {
fmt.Print("Add a tag (empty to stop): ")
tag, err := reader.ReadString('\n')
check(err)
if tag == "\n" {
return *tags
}
tag = tag[:len(tag)-1]
mytags := *tags
*tags = append(mytags, tag)
return getTags(reader, tags)
}
func getLastID(parsedBookmarks []bookmark) int {
lastID := parsedBookmarks[len(parsedBookmarks)-1].id
return lastID
}