/
pipetdata.go
205 lines (170 loc) · 4.11 KB
/
pipetdata.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
198
199
200
201
202
203
204
205
// Package pipetdata impliments meat of pipet, data access functions - which are
// being called out from CLI
package pipetdata
import (
"bytes"
"fmt"
"github.com/pkg/errors"
"github.com/satori/go.uuid"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
var (
// EBadData error bad data on disk
EBadData = fmt.Errorf("bad data")
)
// DataStore is the main structure for snippet access
type DataStore struct {
documentDir string
}
// Metadata for snippet
type metadata struct {
UID string // only relevant to data store, pointer to file where snippet is stored.
Title string `yaml:"title"`
Tags []string `yaml:"tags,omitempty"`
}
// Snippet is the data type holding the actual snippet
type Snippet struct {
Meta metadata
Data string
}
// Marshal serializes snippet data into bytes. Format is
// ---
// yaml metadata front
// ---
// <text follows>
// This is very similiar to pandoc markdown except its just arbitary text for now.
func (s *Snippet) Marshal() ([]byte, error) {
template := `---
%s---
%s
`
meta, err := yaml.Marshal(s.Meta)
if err != nil {
return []byte{}, errors.Wrap(err, "yaml rendering failed")
}
rendered := fmt.Sprintf(template, meta, s.Data)
return []byte(rendered), nil
}
func splitData(buf []byte) (front, data []byte, err error) {
guard := []byte("---")
lg := len(guard)
if !bytes.HasPrefix(buf, guard) {
err = EBadData
return
}
buf = buf[lg:]
end := bytes.Index(buf, guard)
if end == -1 {
err = EBadData
return
}
front = buf[0:end]
// skip --- + '\n'
data = buf[end+lg+1:]
return
}
// Unmarshal takes in note data with metadata blocks in yaml and populates
// Snippet structure.
// https://jekyllrb.com/docs/frontmatter/
func (s *Snippet) Unmarshal(buf []byte) error {
front, data, err := splitData(buf)
var meta metadata
yaml.Unmarshal(front, &meta)
s.Meta = meta
s.Data = string(data)
return err
}
// NewDataStore creates a new datastore abastraction for storing notes. disk
// path is passed as documentDir
func NewDataStore(documentDir string) (*DataStore, error) {
fi, err := os.Stat(documentDir)
if err != nil {
//create directory
err = os.MkdirAll(documentDir, 0755)
if err != nil {
return nil, err
}
} else if !fi.IsDir() {
return nil, fmt.Errorf("path is not a directory: %s", documentDir)
}
return &DataStore{documentDir}, nil
}
// Exist checks with a snippet with the name exists.
func (d *DataStore) Exist(filename string) bool {
fullpath := filepath.Join(d.documentDir, filename)
_, err := os.Stat(fullpath)
return err == nil
}
func (d *DataStore) Fullpath(id string) string {
return filepath.Join(d.documentDir, id)
}
// New creates a new entry in snippets
func (d *DataStore) New(title string, tags ...string) (fn string, err error) {
id := uuid.NewV4().String()
uid := fmt.Sprintf("%s.txt", id)
ns := &Snippet{
Meta: metadata{uid, title, tags},
}
if d.Exist(uid) {
return "", errors.New("duplicate snippet")
}
data, err := ns.Marshal()
if err != nil {
return "", errors.Wrap(err, "marshalling failed")
}
filename := d.Fullpath(uid)
err = ioutil.WriteFile(filename, data, 0755)
return filename, err
}
// Read reads and parses a snippet document
func (d *DataStore) Read(id string) (sn *Snippet, err error) {
if !d.Exist(id) {
err = errors.New("no such document")
return
}
filename := d.Fullpath(id)
buf, err := ioutil.ReadFile(filename)
if err != nil {
err = errors.Wrap(err, "reading failed")
return
}
s := &Snippet{}
err = s.Unmarshal(buf)
return s, err
}
func (d *DataStore) List() (sns []*Snippet, err error) {
sns = []*Snippet{}
fli, err := ioutil.ReadDir(d.documentDir)
if err != nil {
return
}
for _, f := range fli {
if !f.IsDir() && strings.HasSuffix(f.Name(), ".txt") {
s, e := d.Read(f.Name())
if e != nil {
err = e
return
}
sns = append(sns, s)
}
}
if len(sns) == 0 {
err = errors.New("empty snippet store")
}
return
}
func (d *DataStore) Delete(id string) error {
if !d.Exist(id) {
return errors.New("no such document")
}
filename := d.Fullpath(id)
err := os.Remove(filename)
if err != nil {
return errors.Wrap(err, "delete failed")
}
return nil
}