-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
331 lines (278 loc) · 8.28 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package sncli
import (
"os"
"time"
"github.com/briandowns/spinner"
"github.com/gookit/color"
"github.com/jonhadfield/gosn-v2/cache"
"github.com/jonhadfield/gosn-v2/common"
"github.com/jonhadfield/gosn-v2/items"
)
const (
timeLayout = "2006-01-02T15:04:05.000Z"
SNServerURL = "https://api.standardnotes.com"
SNPageSize = 600
SNAppName = "sn-cli"
MinPasswordLength = 8
)
type ItemReferenceYAML struct {
UUID string `yaml:"uuid"`
ContentType string `yaml:"content_type"`
ReferenceType string `yaml:"reference_type",omitempty`
}
type ItemReferenceJSON struct {
UUID string `json:"uuid"`
ContentType string `json:"content_type"`
ReferenceType string `json:"reference_type",omitempty`
}
type OrgStandardNotesSNDetailJSON struct {
ClientUpdatedAt string `json:"client_updated_at"`
PrefersPlainEditor bool `json:"prefersPlainEditor"`
Pinned bool `json:"pinned"`
}
type OrgStandardNotesSNComponentsDetailJSON map[string]interface{}
type OrgStandardNotesSNDetailYAML struct {
ClientUpdatedAt string `yaml:"client_updated_at"`
}
type AppDataContentYAML struct {
OrgStandardNotesSN OrgStandardNotesSNDetailYAML `yaml:"org.standardnotes.sn"`
OrgStandardNotesSNComponents items.OrgStandardNotesSNComponentsDetail `yaml:"org.standardnotes.sn.components,omitempty"`
}
type AppDataContentJSON struct {
OrgStandardNotesSN OrgStandardNotesSNDetailJSON `json:"org.standardnotes.sn"`
OrgStandardNotesSNComponents items.OrgStandardNotesSNComponentsDetail `json:"org.standardnotes.sn.components,omitempty"`
}
type TagContentYAML struct {
Title string `yaml:"title"`
ItemReferences []ItemReferenceYAML `yaml:"references"`
AppData AppDataContentYAML `yaml:"appData"`
}
type TagContentJSON struct {
Title string `json:"title"`
ItemReferences []ItemReferenceJSON `json:"references"`
AppData AppDataContentJSON `json:"appData"`
}
type SettingContentYAML struct {
Title string `yaml:"title"`
ItemReferences []ItemReferenceYAML `yaml:"references"`
AppData AppDataContentYAML `yaml:"appData"`
}
type SettingContentJSON struct {
Title string `json:"title"`
ItemReferences []ItemReferenceJSON `json:"references"`
AppData AppDataContentJSON `json:"appData"`
}
type NoteContentYAML struct {
Title string `yaml:"title"`
Text string `json:"text"`
ItemReferences []ItemReferenceYAML `yaml:"references"`
AppData AppDataContentYAML `yaml:"appData"`
EditorIdentifier string `yaml:"editorIdentifier"`
PreviewPlain string `yaml:"preview_plain"`
PreviewHtml string `yaml:"preview_html"`
Spellcheck bool `yaml:"spellcheck"`
Trashed *bool `yaml:"trashed,omitempty"`
}
type NoteContentJSON struct {
Title string `json:"title"`
Text string `json:"text"`
ItemReferences []ItemReferenceJSON `json:"references"`
AppData AppDataContentJSON `json:"appData"`
EditorIdentifier string `json:"editorIdentifier"`
PreviewPlain string `json:"preview_plain"`
PreviewHtml string `json:"preview_html"`
Spellcheck bool `json:"spellcheck"`
Trashed *bool `json:"trashed,omitempty"`
}
type TagJSON struct {
UUID string `json:"uuid"`
Content TagContentJSON `json:"content"`
ContentType string `json:"content_type"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type TagYAML struct {
UUID string `yaml:"uuid"`
Content TagContentYAML `yaml:"content"`
ContentType string `yaml:"content_type"`
CreatedAt string `yaml:"created_at"`
UpdatedAt string `yaml:"updated_at"`
}
type NoteJSON struct {
UUID string `json:"uuid"`
Content NoteContentJSON `json:"content"`
ContentType string `json:"content_type"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type SettingYAML struct {
UUID string `yaml:"uuid"`
Content SettingContentYAML `yaml:"content"`
ContentType string `yaml:"content_type"`
CreatedAt string `yaml:"created_at"`
UpdatedAt string `yaml:"updated_at"`
}
type SettingJSON struct {
UUID string `json:"uuid"`
Content SettingContentJSON `json:"content"`
ContentType string `json:"content_type"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type NoteYAML struct {
UUID string `yaml:"uuid"`
Content NoteContentYAML `yaml:"content"`
ContentType string `yaml:"content_type"`
CreatedAt string `yaml:"created_at"`
UpdatedAt string `yaml:"updated_at"`
}
type TagItemsConfig struct {
Session *cache.Session
FindTitle string
FindText string
FindTag string
NewTags []string
Replace bool
IgnoreCase bool
Debug bool
}
type GetItemsConfig struct {
Session *cache.Session
Filters items.ItemFilters
Output string
Debug bool
}
type AddTagsInput struct {
Session *cache.Session
Tags []string
Parent string
ParentUUID string
Debug bool
Replace bool
}
type AddTagsOutput struct {
Added, Existing []string
}
type GetTagConfig struct {
Session *cache.Session
Filters items.ItemFilters
Output string
Debug bool
}
type GetSettingsConfig struct {
Session *cache.Session
Filters items.ItemFilters
Output string
Debug bool
}
type GetNoteConfig struct {
Session *cache.Session
Filters items.ItemFilters
NoteTitles []string
TagTitles []string
TagUUIDs []string
PageSize int
BatchSize int
Debug bool
}
type DeleteTagConfig struct {
Session *cache.Session
Email string
TagTitles []string
TagUUIDs []string
Regex bool
Debug bool
}
type ListChecklistsInput struct {
Session *cache.Session
Debug bool
}
type ShowChecklistInput struct {
Session *cache.Session
Title string
UUID string
Debug bool
}
type AddNoteInput struct {
Session *cache.Session
Title string
Text string
FilePath string
Tags []string
Replace bool
Debug bool
}
type DeleteItemConfig struct {
Session *cache.Session
NoteTitles []string
NoteText string
ItemsUUIDs []string
Regex bool
Debug bool
}
type DeleteNoteConfig struct {
Session *cache.Session
NoteTitles []string
NoteText string
NoteUUIDs []string
Regex bool
Debug bool
}
type WipeConfig struct {
Session *cache.Session
UseStdOut bool
Debug bool
Everything bool
}
type StatsConfig struct {
Session cache.Session
}
func referenceExists(tag items.Tag, refID string) bool {
for _, ref := range tag.Content.References() {
if ref.UUID == refID {
return true
}
}
return false
}
func filterEncryptedItemsByTypes(ei items.EncryptedItems, types []string) (o items.EncryptedItems) {
for _, i := range ei {
if StringInSlice(i.ContentType, types, true) {
o = append(o, i)
}
}
return o
}
func filterItemsByTypes(ei items.Items, types []string) (o items.Items) {
for _, i := range ei {
if StringInSlice(i.GetContentType(), types, true) {
o = append(o, i)
}
}
return o
}
func filterCacheItemsByTypes(ei cache.Items, types []string) (o cache.Items) {
for _, i := range ei {
if StringInSlice(i.ContentType, types, true) {
o = append(o, i)
}
}
return o
}
var supportedContentTypes = []string{common.SNItemTypeNote, common.SNItemTypeTag, common.SNItemTypeComponent}
func (i *WipeConfig) Run() (int, error) {
i.Session.RemoveDB()
if !i.Session.Debug && i.UseStdOut {
prefix := color.HiWhite.Sprintf("wiping ")
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond, spinner.WithWriter(os.Stdout))
if i.UseStdOut {
s = spinner.New(spinner.CharSets[14], 100*time.Millisecond, spinner.WithWriter(os.Stderr))
}
s.Prefix = prefix
s.Start()
deleted, err := items.DeleteContent(i.Session.Session, i.Everything)
s.Stop()
return deleted, err
}
return items.DeleteContent(i.Session.Session, i.Everything)
}