-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
67 lines (51 loc) · 1.2 KB
/
writer.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
package main
import (
"bufio"
"io/ioutil"
"log"
"os"
"gopkg.in/yaml.v2"
)
func tagsToString(tags []string) string {
var tagsString string
for i, element := range tags {
if i != len(tags)-1 {
tagsString = tagsString + "#" + element + " "
} else {
tagsString = tagsString + "#" + element
}
}
return tagsString
}
func removeFromFile(bookmarks []bookmark, fileLocation *string) {
err := ioutil.WriteFile(*fileLocation, []byte(""), 0644)
check(err)
for _, element := range bookmarks {
writeBookmark(element, fileLocation)
}
}
func writeBookmark(newBookmark bookmark, fileLocation *string) {
t := T{}
contentString := getFileContent(fileLocation)
err := yaml.Unmarshal([]byte(contentString), &t)
if err != nil {
log.Fatalf("error: %v", err)
}
yamlBookmark := Bookmark{
Title: newBookmark.title,
URL: newBookmark.url,
Tags: newBookmark.tags,
Comment: newBookmark.comment,
}
t.Bookmarks = append(t.Bookmarks, yamlBookmark)
d, err := yaml.Marshal(&t)
check(err)
newString := string(d)
file, err := os.Create(*fileLocation)
check(err)
defer file.Close()
writer := bufio.NewWriter(file)
defer writer.Flush()
_, err = writer.WriteString(newString)
check(err)
}