-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
123 lines (101 loc) · 2.71 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
package main
import (
"flag"
"fmt"
"os"
"strconv"
"github.com/atotto/clipboard"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
fileLocation := flag.String("f", ".mydrawer", "location of bookmark file")
addBookmarkBool := flag.Bool("a", false, "add a new bookmark")
searchBookmarkID := flag.String("i", "", "get the bookmark with id")
deleteBookmarkID := flag.String("d", "", "delete bookmark with this id")
tagToSearch := flag.String("t", "", "get bookmarks with this tag")
copyBookmark := flag.String("c", "", "[bookmark id] copy URL for bookmark with this id")
flag.Parse()
fmt.Println("file:" + *fileLocation)
if *addBookmarkBool {
addBookmark(fileLocation)
return
}
if *searchBookmarkID != "" {
searchBookmarkIDInt, err := strconv.Atoi(*searchBookmarkID)
check(err)
getBookmarkByID(searchBookmarkIDInt, fileLocation)
return
}
if *deleteBookmarkID != "" {
deleteBookmarkIDInt, err := strconv.Atoi(*deleteBookmarkID)
check(err)
deleteBookmarkByID(deleteBookmarkIDInt, fileLocation)
return
}
if *tagToSearch != "" {
searchBookmarksByTag(tagToSearch, fileLocation)
return
}
if *copyBookmark != "" {
copyBookmarkIDInt, err := strconv.Atoi(*copyBookmark)
check(err)
copyBookmarkByID(copyBookmarkIDInt, fileLocation)
return
}
createFileOrListBookmarks(fileLocation)
}
func copyBookmarkByID(id int, fileLocation *string) {
var bookmarkToCopy bookmark
bookmarks := parseBookmarksFile(fileLocation)
for _, element := range bookmarks {
if element.id == id {
bookmarkToCopy = element
}
}
clipboard.WriteAll(bookmarkToCopy.url)
fmt.Println("Copied bookmark:")
fmt.Printf(printBookmark(bookmarkToCopy))
return
}
func deleteBookmarkByID(id int, fileLocation *string) {
var bookmarkToDelete bookmark
bookmarks := parseBookmarksFile(fileLocation)
for i, element := range bookmarks {
if element.id == id {
bookmarkToDelete = element
bookmarks = append(bookmarks[:i], bookmarks[i+1:]...)
}
}
deleteBookmark(bookmarkToDelete, bookmarks, fileLocation)
}
func getBookmarkByID(id int, fileLocation *string) {
bookmarks := parseBookmarksFile(fileLocation)
for _, element := range bookmarks {
if element.id == id {
fmt.Printf(printBookmark(element))
return
}
}
}
func createFileOrListBookmarks(fileLocation *string) {
if _, err := os.Stat(*fileLocation); os.IsNotExist(err) {
_, err := os.Create(*fileLocation)
check(err)
addBookmark(fileLocation)
}
listBookmarks(fileLocation)
}
func searchBookmarksByTag(tag *string, fileLocation *string) {
bookmarks := parseBookmarksFile(fileLocation)
for _, bookmark := range bookmarks {
for _, element := range bookmark.tags {
if element == *tag {
fmt.Printf(printBookmark(bookmark))
}
}
}
}