-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
246 lines (201 loc) · 4.59 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"strings"
"time"
sf "github.com/VictorNine/sfgo"
"github.com/fsnotify/fsnotify"
)
type Note struct {
UUID string `json:"-"`
Text string `json:"text"`
Title string `json:"title"`
References []Reference `json:"references"`
AppData map[string]map[string]string `json:"appData"`
}
type Reference struct {
UUID string `json:"uuid"`
ContentType string `json:"content_type"`
}
var files map[string]string // Map filename to UUID
func syncLoop(sess *sf.Session, db *database) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
syncTimer := time.NewTicker(30 * time.Second)
defer syncTimer.Stop()
err = watcher.Add("notes")
if err != nil {
log.Fatal(err)
}
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Println("event:", event)
if event.Op&fsnotify.Write == fsnotify.Write {
filename := "./" + strings.Replace(event.Name, "\\", "/", -1)
uuid, ok := files[filename]
if ok {
fileToNote(uuid, filename, sess, db)
}
log.Println("modified file:", event.Name)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
case <-syncTimer.C:
err := sync(sess, db)
if err != nil {
log.Fatal(err)
}
}
}
}
// Decrypt and unmarshal an item
func itemToNote(sess *sf.Session, item *sf.Item) (Note, error) {
pt, err := sess.Decrypt(item)
if err != nil {
return Note{}, err
}
note := Note{}
err = json.Unmarshal(pt, ¬e)
if err != nil {
return Note{}, err
}
note.UUID = item.UUID
return note, nil
}
func sync(sess *sf.Session, db *database) error {
unItems, err := db.getUnsyncedItems()
if err != nil {
return err
}
for _, it := range unItems {
log.Println(it.UUID + " ready to be synced")
err := sess.UpdateItem(it)
if err != nil {
return err
}
}
// Sync
items, err := sess.Sync()
if err != nil {
return err
}
// TODO: Check if there were conflicts before deleting (implement in lib)
for _, it := range unItems {
err := db.deletetUnsyncedItem(it.UUID)
if err != nil {
return err
}
}
notesSynced := 0 // Counter for notes synced
for _, item := range items.RetrievedItems {
if item.ContentType != "Note" {
continue
}
notesSynced++
// Delete the item from DB if marked for deletion
if item.Deleted {
err := db.deleteItem(item.UUID)
if err != nil {
return err
}
continue
}
db.saveItem(&item)
_, err := createFile(&item, sess)
if err != nil {
log.Fatal(err)
}
}
if notesSynced > 0 {
log.Printf("%v new notes synced to database\n", notesSynced)
} else {
log.Println("Database is up to date")
}
for _, item := range items.SavedItems {
if item.ContentType != "Note" {
continue
}
db.saveItem(&item)
}
db.setSyncToken(*sess.SyncToken)
return nil
}
func main() {
listNotes := flag.Bool("list", false, "Retrieve a list of notes")
syncAndExit := flag.Bool("sync", false, "Sync database and exit")
email := flag.String("email", "", "email")
password := flag.String("password", "", "password")
flag.Parse()
if *email == "" || *password == "" {
fmt.Println("No login information use -email and -password")
return
}
files = make(map[string]string)
db, err := InitDB()
if err != nil {
log.Fatal(err)
}
defer db.Close()
sess := sf.NewSession(
"https://sync.standardnotes.org",
*email,
)
syncToken := db.getSyncToken()
if len(syncToken) > 1 {
sess.SyncToken = &syncToken
}
err = sess.Signin(*password)
if err != nil {
log.Fatal(err)
}
log.Println("Login successful!")
err = sync(sess, db)
if err != nil {
log.Fatal(err)
}
if *syncAndExit {
return
}
// Get a list of items from the database
items, err := db.getItems()
if err != nil {
log.Fatal(err)
}
// List notes
if *listNotes {
for _, item := range items {
note, err := itemToNote(sess, &item)
if err != nil {
log.Fatal(err)
}
fmt.Println(note.UUID + " - " + note.Title)
}
return // Exit
}
err = createDir("notes")
if err != nil {
log.Fatal(err)
}
// Create txt files from notes
for _, item := range items {
_, err := createFile(&item, sess)
if err != nil {
log.Fatal(err)
}
}
// Start sync loop
syncLoop(sess, db)
}