-
Notifications
You must be signed in to change notification settings - Fork 1
/
user_lib.go
654 lines (569 loc) · 17.1 KB
/
user_lib.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"reflect"
"sort"
"strings"
"text/template"
"time"
"github.com/blevesearch/bleve/search"
"github.com/briandowns/spinner"
"github.com/google/go-github/github"
"github.com/schollz/progressbar/v2"
"golang.org/x/oauth2"
)
// global background context
var ctx = context.Background()
// libConfig - Global library configuration
var libConfig = fmt.Sprintf("%s/config.json", getLibraryDirectory())
// Global library configuration
var libPath = fmt.Sprintf("%s/library.json", getLibraryDirectory())
var libTagsPath = fmt.Sprintf("%s/tags.json", getLibraryDirectory())
type configuration struct {
AuthToken string `json:"token"`
Login string `json:"login"`
UpdatedAt time.Time `json:"updated_at"`
Editor string `json:"editor"`
}
type gistSort []*github.Gist
func (e gistSort) Len() int {
return len(e)
}
func (e gistSort) Less(i, j int) bool {
return e[i].UpdatedAt.Before(e[j].GetUpdatedAt())
}
func (e gistSort) Swap(i, j int) {
e[i], e[j] = e[j], e[i]
}
// Snippet - Used to store gist data
type Snippet struct {
// The ID is actually the github Node ID which is unique to the given commit
// IDX is A convenience numeric ID for listing individual snippets
ID string `json:"ID"`
GistID string `json:"GistID"`
IDX int `json:"IDX"`
Owner string `json:"Owner"`
Description string `json:"Description"`
Public string `json:"Public"`
Starred string `json:"Starred"`
Files map[github.GistFilename]github.GistFile `json:"Files"`
NFiles int `json:"NFiles"`
NLines int `json:"NLines"`
Language []string `json:"Language"`
Filename []string `json:"Filename"`
Tags []string `json:"Tags"`
Comments int `json:"Comments"`
CreatedAt time.Time `json:"CreatedAt"`
UpdatedAt time.Time `json:"UpdatedAt"`
URL string `json:"URL"`
}
// Generate list of IDs for gists
func idMap(gistSet []*github.Gist) map[string]*github.Gist {
m := make(map[string]*github.Gist)
var key string
for _, gist := range gistSet {
key = getGistRecID(gist)
m[key] = gist
}
return m
}
func getLibraryDirectory() string {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
return fmt.Sprintf(filepath.Join(usr.HomeDir, ".gg"))
}
func deleteLibrary() {
os.RemoveAll(getLibraryDirectory())
}
func initializeLibrary(AuthToken string, rebuild bool) bool {
if rebuild {
deleteLibrary()
// Reload index
dbIdx = *openDb()
}
client, _ := authenticate(AuthToken)
user, resp, err := client.Users.Get(ctx, "")
if err != nil {
ThrowError(fmt.Sprintf("Error authenticating: %s", resp), 1)
}
var config = configuration{
AuthToken: string(AuthToken),
Login: string(user.GetLogin()),
UpdatedAt: time.Now(),
Editor: "",
}
saveConfig(config)
return true
}
func saveConfig(config configuration) {
_ = os.Mkdir(getLibraryDirectory(), 0755)
out, err := json.Marshal(config)
check(err)
err = ioutil.WriteFile(libConfig, out, 0644)
check(err)
}
func getConfig() (configuration, error) {
// Check that config exists
var blankConfig configuration
if _, err := os.Stat(libConfig); os.IsNotExist(err) {
return blankConfig, errors.New("No config found. Run 'gg sync --token <github token>'")
}
jsonFile, err := os.Open(libConfig)
if err != nil {
return blankConfig, errors.New("JSON Parse Error. Run 'gg sync --rebuild'")
}
defer jsonFile.Close()
configData, _ := ioutil.ReadAll(jsonFile)
var config configuration
json.Unmarshal(configData, &config)
return config, nil
}
// authenticate - Setup user authentication with github token
func authenticate(authToken string) (*github.Client, string) {
var login string
if authToken == "" {
config, err := getConfig()
if err != nil {
ThrowError(err.Error(), 1)
}
authToken = config.AuthToken
login = config.Login
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: authToken},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
client.UserAgent = "github.com/danielecook/gg"
return client, login
}
func newGist(fileSet map[string]string, description string, public bool) {
client, _ := authenticate("")
var gist github.Gist
files := map[github.GistFilename]github.GistFile{}
for fname, item := range fileSet {
fset := new(string)
*fset = fname
files[github.GistFilename(fname)] = github.GistFile{
Content: &item,
Filename: fset,
}
}
gist.Description = &description
gist.Files = files
gist.Public = &public
resultGist, _, err := client.Gists.Create(ctx, &gist)
if err != nil {
ThrowError(fmt.Sprintf("Error: %s", err), 1)
}
// Add record to database
gistDbRec := gistDbRecord(resultGist, nextIdx(), []string{})
dbIdx.Index(gistDbRec.ID, gistDbRec)
// Print URL on success
boldUnderline.Println(*resultGist.HTMLURL)
}
func runGistEdit(params Snippet) {
// Opens template and allows user to edit; and validates.
gistFiles := params.Files
// Use first filename ext
var ext string
for _, item := range gistFiles {
ext = filepath.Ext(*item.Filename)
break
}
tmpfile, err := ioutil.TempFile("", fmt.Sprintf("gist.*.%s", ext))
if err != nil {
log.Fatal(err)
}
defer os.Remove(tmpfile.Name()) // clean up
t, err := template.New("tname").Funcs(template.FuncMap{
"Deref": func(i *string) string { return *i },
"fname_line": func(fname string) string {
return fmt.Sprintf("%s%s", fname, strings.Repeat("-", (100-len(fname))))
},
}).Parse(string(gistTemplate))
check(err)
buf := new(bytes.Buffer)
t.Execute(buf, params)
// Write the header of the file
err = ioutil.WriteFile(tmpfile.Name(), buf.Bytes(), 0644)
if err != nil {
log.Fatal(err)
}
var cmd *exec.Cmd
config, _ := getConfig()
var editor = config.Editor
if editor == "subl" {
cmd = exec.Command("subl", "--wait", fmt.Sprintf("%s", tmpfile.Name()))
} else if editor == "nano" {
cmd = exec.Command("nano", "-t", tmpfile.Name())
} else if editor == "vim" {
cmd = exec.Command("vim", tmpfile.Name())
} else if editor == "micro" {
cmd = exec.Command("micro", tmpfile.Name())
}
cmd.Stdin = os.Stdout
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
func editGist(gistID int) {
// TODO [$5fdcfd44ecafc60007b0920a]: Split out template portion/editing for creating new gists...
client, username := authenticate("")
dbGist := lookupGist(gistID)
// Check that username == user
if username != dbGist.Fields["Owner"].(string) {
ThrowError("You can't edit another users gists!", 1)
}
gistFiles := parseGistFilesStruct(dbGist)
dbStarred := dbGist.Fields["Starred"].(string) == "T"
var params = Snippet{
Description: dbGist.Fields["Description"].(string),
Starred: dbGist.Fields["Starred"].(string),
Public: dbGist.Fields["Public"].(string),
Files: gistFiles,
}
// Use first filename ext
var ext string
for _, item := range gistFiles {
ext = filepath.Ext(*item.Filename)
break
}
tmpfile, err := ioutil.TempFile("", fmt.Sprintf("gist.*%s", ext))
if err != nil {
log.Fatal(err)
}
defer os.Remove(tmpfile.Name()) // clean up
t, err := template.New("tname").Funcs(template.FuncMap{
"Deref": func(i *string) string { return *i },
"fname_line": func(fname string) string {
return fmt.Sprintf("%s%s", fname, strings.Repeat("-", (100-len(fname))))
},
}).Parse(string(gistTemplate))
check(err)
buf := new(bytes.Buffer)
t.Execute(buf, params)
// Write the header of the file
err = ioutil.WriteFile(tmpfile.Name(), buf.Bytes(), 0644)
if err != nil {
log.Fatal(err)
}
var cmd *exec.Cmd
config, _ := getConfig()
var editor = config.Editor
if editor == "subl" {
cmd = exec.Command("subl", "--wait", fmt.Sprintf("%s", tmpfile.Name()))
} else if editor == "nano" {
cmd = exec.Command("nano", "-t", tmpfile.Name())
} else if editor == "vim" {
cmd = exec.Command("vim", tmpfile.Name())
} else if editor == "micro" {
cmd = exec.Command("micro", tmpfile.Name())
}
cmd.Stdin = os.Stdout
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
edit, err := ioutil.ReadFile(tmpfile.Name())
if err != nil {
ThrowError("Error reading output", 1)
}
var starred bool
var eGist github.Gist
eGist, starred, err = parseGistTemplate(string(edit))
// If filenames were removed from the original, set them to NULL to delete.
for fname := range gistFiles {
if (eGist.Files[fname] == github.GistFile{}) {
var nullFile *string = nil
eGist.Files[fname] = github.GistFile{
Filename: nullFile,
}
}
}
if err != nil {
// Reload template here with comment
}
greenText.Printf("Saving %v [%v]\n", int(dbGist.Fields["IDX"].(float64)), dbGist.Fields["GistID"].(string))
resultGist, response, err := client.Gists.Edit(ctx, dbGist.Fields["GistID"].(string), &eGist)
if err != nil {
fmt.Printf("Error: %v", err)
ThrowError(response.String(), 1)
}
// If star status has changed, update
var starErr error
var starIds []string
if dbStarred != starred {
if starred {
_, starErr = client.Gists.Star(ctx, resultGist.GetID())
} else {
_, starErr = client.Gists.Unstar(ctx, resultGist.GetID())
}
if starErr != nil {
ThrowError("Error updating star", 1)
}
starIds = []string{getGistRecID(resultGist)}
}
// Delete the old record, and insert the new record below.
// Retain the same 'IDX' as before.
batch := dbIdx.NewBatch()
editGistDbRec := gistDbRecord(resultGist, int(dbGist.Fields["IDX"].(float64)), starIds)
batch.Index(editGistDbRec.ID, editGistDbRec)
batch.Delete(dbGist.ID)
dbIdx.Batch(batch)
boldUnderline.Println(*resultGist.HTMLURL)
}
func rmGist(gistID int) {
client, _ := authenticate("")
gist := lookupGist(gistID)
_, err := client.Gists.Delete(ctx, gist.Fields["GistID"].(string))
if err != nil {
ThrowError(fmt.Sprintf("Error: %s", err), 1)
}
// Print URL on success
msg := fmt.Sprintf("Removed %s\n", gist.Fields["GistID"].(string))
// Remove from search index
dbIdx.Delete(gist.ID)
successMsg(msg)
}
func getGistRecID(gist *github.Gist) string {
return fmt.Sprintf("%v::%v", gist.GetID(), gist.GetUpdatedAt())
}
func gistDbRecord(gist *github.Gist, idx int, starIDs []string) Snippet {
gistRecID := getGistRecID(gist)
ch := make(chan *string, 50)
items := make(map[github.GistFilename]github.GistFile)
// Check if gist has already been loaded
// if not, download files.
filenames := []string{}
languages := []string{}
nlines := 0
// Check whether document exists
if _, err := dbIdx.Document(gistRecID); err == nil {
for k := range gist.Files {
var updated = gist.Files[k]
// If RawURL is nil, the gist was generated
// locally and does not need to be retrieved.
if gist.Files[k].RawURL != nil {
var url = string(*gist.Files[k].RawURL)
go fetchContent(url, ch)
updated.Content = <-ch
}
nlines += len(strings.Split(*updated.Content, "\n"))
items[k] = updated
if gist.Files[k].Filename != nil {
filenames = append(filenames, *gist.Files[k].Filename)
}
if gist.Files[k].Language != nil {
languages = append(languages, *gist.Files[k].Language)
}
}
}
tags := parseTags(gist.GetDescription())
var sn = Snippet{
ID: getGistRecID(gist),
GistID: gist.GetID(),
IDX: idx,
Owner: string(gist.GetOwner().GetLogin()),
Description: gist.GetDescription(),
Public: trueFalse(gist.GetPublic()),
Files: items,
Language: languages,
Filename: filenames,
Starred: trueFalse(contains(starIDs, gistRecID)),
NFiles: len(items),
NLines: nlines,
Tags: tags,
Comments: gist.GetComments(),
CreatedAt: gist.GetCreatedAt(),
UpdatedAt: gist.GetUpdatedAt(),
URL: gist.GetHTMLURL(),
}
return sn
}
func updateLibrary() {
client, username := authenticate("")
// Add a spinner
s := spinner.New(spinner.CharSets[14], 100*time.Millisecond) // Build our new spinner
s.Writer = os.Stderr
s.Start() // Start the spinner
/*
List User Gists
*/
var allGists []*github.Gist
var starredGists []*github.Gist
page := 1
opt := &github.GistListOptions{
ListOptions: github.ListOptions{Page: 0, PerPage: 100},
}
for {
gists, resp, err := client.Gists.List(ctx, "", opt)
check(err)
allGists = append(allGists, gists...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
errlog.Printf("Listing [total=%v] [page=%v]\n", len(allGists), page)
page++
}
since, _ := time.Parse("Mon Jan 2 15:04:05 -0700 MST 2006", "Mon Jan 2 15:04:05 -0700 MST 1987")
opt = &github.GistListOptions{
ListOptions: github.ListOptions{Page: 0, PerPage: 100},
Since: since,
}
gistMap := idMap(allGists)
// Get starred gists
for {
gists, resp, err := client.Gists.ListStarred(ctx, opt)
check(err)
starredGists = append(starredGists, gists...)
// Append starred gists not present in the library
for _, gist := range starredGists {
key := fmt.Sprintf("%v::%v", gist.GetID(), gist.GetUpdatedAt())
if gistMap[key] == nil {
allGists = append(allGists, gist)
}
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
errlog.Printf("Fetching starred [total=%v] [page=%v]\n", len(allGists), page)
page++
}
starIDs := make([]string, len(starredGists))
for idx, gist := range starredGists {
starIDs[idx] = getGistRecID(gist)
}
errlog.Printf("Listing complete [total=%v]\n", len(allGists))
s.Stop()
sort.Sort(gistSort(allGists))
// Parse library
var Library []*Snippet
Library = make([]*Snippet, len(allGists))
boldMsg(fmt.Sprintf("Loading Gists for %s\n", username))
// Dump previous DB to determine whether
// gists need to be updated.
existingGists := dumpDb()
existingGistIds := make([]string, len(existingGists.Hits))
for idx, gist := range existingGists.Hits {
existingGistIds[idx] = gist.ID
}
// Initialize progress bar
bar := progressbar.New(len(allGists))
currentGistIds := make([]string, len(allGists))
idStart := nextIdx()
offset := 0
for i, gist := range allGists {
// Store gist in db
var gistDbRec Snippet
if contains(existingGistIds, getGistRecID(gist)) == false {
// Calculate nextIdx so IDs are static unless
// a rebuild is performed.
gistDbRec = gistDbRecord(gist, idStart+offset, starIDs)
offset++
}
currentGistIds[i] = getGistRecID(gist)
Library[i] = &gistDbRec
bar.Add(1)
}
boldMsg("\nIndexing Gists\n")
batch := dbIdx.NewBatch()
// Delete gist IDs that no longer exist
for _, existingID := range existingGistIds {
if contains(currentGistIds, existingID) == false {
batch.Delete(existingID)
}
}
for _, gist := range Library {
// Store gist in db if it is not there already
if contains(existingGistIds, gist.ID) == false {
batch.Index(gist.ID, gist)
}
}
// Execute database updates
dbIdx.Batch(batch)
/*
Store JSON
*/
// Library
out, err := json.Marshal(Library)
check(err)
err = ioutil.WriteFile(libPath, out, 0644)
check(err)
// Update date/time of config
config, _ := getConfig()
config.UpdatedAt = time.Now()
saveConfig(config)
docCount, err := dbIdx.DocCount()
fmt.Println()
successMsg(fmt.Sprintf("Loaded %v gist%s\n", docCount, ifelse(docCount == 1, "", "s")))
}
func libExists() bool {
if _, err := os.Stat(libConfig); os.IsNotExist(err) {
return false
}
return true
}
func parseGistFiles(gist *search.DocumentMatch) map[string]map[string]string {
// Parse bleve index which flattens results
keys := reflect.ValueOf(gist.Fields).MapKeys()
strkeys := make([]string, len(keys))
for i := 0; i < len(keys); i++ {
strkeys[i] = keys[i].String()
}
var fsplit []string
var fileset = map[string]map[string]string{}
for idx := range strkeys {
fsplit = strings.Split(strkeys[idx], ".")
if fsplit[0] == "Files" {
field := fsplit[len(fsplit)-1]
filename := strings.Join(fsplit[1:len(fsplit)-1], ".")
value := gist.Fields[strkeys[idx]]
if fileset[filename] == nil {
fileset[filename] = map[string]string{}
}
fileset[filename][field] = fmt.Sprintf("%v", value)
}
}
return fileset
}
func parseGistFilesStruct(gist *search.DocumentMatch) map[github.GistFilename]github.GistFile {
// Converts gistfiles struct for use in Snippet
files := parseGistFiles(gist)
var result map[github.GistFilename]github.GistFile
result = make(map[github.GistFilename]github.GistFile, len(files))
for _, item := range files {
var content = item["content"]
var fname = item["filename"]
var fset = github.GistFilename(fname)
result[fset] = github.GistFile{
Content: &content,
Filename: &fname,
}
}
return result
}
func GistToText(gist *search.DocumentMatch) string {
// Concatenates gist files into a single line of text
// for coying
output := ""
gistSet := parseGistFilesStruct(gist)
for _, item := range gistSet {
output += *item.Content + "\n"
}
return output
}