forked from headzoo/surf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookmarks.go
197 lines (172 loc) · 5.08 KB
/
bookmarks.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
package jar
import (
"encoding/json"
"github.com/headzoo/surf/errors"
"github.com/headzoo/surf/util"
"io/ioutil"
"os"
)
// initialBookmarksCapacity is the initial capacity for the bookmarks map.
const initialBookmarksCapacity = 20
// BookmarksMap stores bookmarks.
type BookmarksMap map[string]string
// BookmarksJar is a container for storage and retrieval of bookmarks.
type BookmarksJar interface {
// Save saves a bookmark with the given name.
Save(name, url string) error
// Read returns the URL for the bookmark with the given name.
Read(name string) (string, error)
// Remove deletes the bookmark with the given name.
Remove(name string) bool
// Has returns a boolean value indicating whether a bookmark exists with the given name.
Has(name string) bool
// All returns all of the bookmarks as a BookmarksMap.
All() BookmarksMap
}
// MemoryBookmarks is an in-memory implementation of BookmarksJar.
type MemoryBookmarks struct {
bookmarks BookmarksMap
}
// NewMemoryBookmarks creates and returns a new *BookmarkMemoryJar type.
func NewMemoryBookmarks() *MemoryBookmarks {
return &MemoryBookmarks{
bookmarks: make(BookmarksMap, initialBookmarksCapacity),
}
}
// Save saves a bookmark with the given name.
//
// Returns an error when a bookmark with the given name already exists. Use the
// Has() or Remove() methods first to avoid errors.
func (b *MemoryBookmarks) Save(name, url string) error {
if b.Has(name) {
return errors.New(
"Bookmark with the name '%s' already exists.", name)
}
b.bookmarks[name] = url
return nil
}
// Read returns the URL for the bookmark with the given name.
//
// Returns an error when a bookmark does not exist with the given name. Use the
// Has() method first to avoid errors.
func (b *MemoryBookmarks) Read(name string) (string, error) {
if !b.Has(name) {
return "", errors.New(
"A bookmark does not exist with the name '%s'.", name)
}
return b.bookmarks[name], nil
}
// Remove deletes the bookmark with the given name.
//
// Returns a boolean value indicating whether a bookmark existed with the given
// name and was removed. This method may be safely called even when a bookmark
// with the given name does not exist.
func (b *MemoryBookmarks) Remove(name string) bool {
if b.Has(name) {
delete(b.bookmarks, name)
return true
}
return false
}
// Has returns a boolean value indicating whether a bookmark exists with the given name.
func (b *MemoryBookmarks) Has(name string) bool {
_, ok := b.bookmarks[name]
return ok
}
// All returns all of the bookmarks as a BookmarksMap.
func (b *MemoryBookmarks) All() BookmarksMap {
return b.bookmarks
}
// FileBookmarks is an implementation of BookmarksJar that saves to a file.
//
// The bookmarks are saved as a JSON string.
type FileBookmarks struct {
bookmarks BookmarksMap
file string
}
// NewFileBookmarks creates and returns a new *FileBookmarks type.
func NewFileBookmarks(file string) (*FileBookmarks, error) {
var bookmarks BookmarksMap = nil
if !util.FileExists(file) {
bookmarks = make(BookmarksMap, initialBookmarksCapacity)
} else {
fin, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
err = json.Unmarshal(fin, &bookmarks)
if err != nil {
return nil, err
}
}
return &FileBookmarks{
bookmarks: bookmarks,
file: file,
}, nil
}
// Save saves a bookmark with the given name.
//
// Returns an error when a bookmark with the given name already exists. Use the
// Has() or Remove() methods first to avoid errors.
func (b *FileBookmarks) Save(name, url string) error {
if b.Has(name) {
return errors.New(
"Bookmark with the name '%s' already exists.", name)
}
b.bookmarks[name] = url
return b.writeToFile()
}
// Read returns the URL for the bookmark with the given name.
//
// Returns an error when a bookmark does not exist with the given name. Use the
// Has() method first to avoid errors.
func (b *FileBookmarks) Read(name string) (string, error) {
if !b.Has(name) {
return "", errors.New(
"A bookmark does not exist with the name '%s'.", name)
}
return b.bookmarks[name], nil
}
// Remove deletes the bookmark with the given name.
//
// Returns a boolean value indicating whether a bookmark existed with the given
// name and was removed. This method may be safely called even when a bookmark
// with the given name does not exist.
func (b *FileBookmarks) Remove(name string) bool {
if b.Has(name) {
delete(b.bookmarks, name)
err := b.writeToFile()
if err == nil {
return true
}
}
return false
}
// Has returns a boolean value indicating whether a bookmark exists with the given name.
func (b *FileBookmarks) Has(name string) bool {
_, ok := b.bookmarks[name]
return ok
}
// All returns all of the bookmarks as a BookmarksMap.
func (b *FileBookmarks) All() BookmarksMap {
return b.bookmarks
}
// writeToFile writes the bookmarks to the file.
func (b *FileBookmarks) writeToFile() (err error) {
j, err := json.Marshal(b.bookmarks)
if err != nil {
return err
}
fout, err := os.Create(b.file)
if err != nil {
return err
}
defer func() {
err = fout.Close()
}()
_, err = fout.Write(j)
if err != nil {
return err
}
return err
}