-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.go
137 lines (119 loc) · 3.05 KB
/
entry.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
package main
import (
"context"
"fmt"
"github.com/google/go-github/github"
"github.com/o98k-ok/lazy/v2/alfred"
"github.com/o98k-ok/lazy/v2/cache/file"
"golang.org/x/oauth2"
"os"
"strings"
"time"
)
const (
CacheJsonFile = "./cache.json"
DefaultTTL = "8h"
FullPageSize = 20
IncrPageSize = 5
TokenKey = "GithubToken"
TTLKey = "TTL"
)
// ListRepos from GitHub apis sorted by starred time desc
// cache history data, compare and update
func ListRepos(cached *alfred.Items, token string) (*alfred.Items, error) {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
client := github.NewClient(oauth2.NewClient(ctx, ts))
var pageSize = FullPageSize
cachedItems := make(map[string]*alfred.Item)
if cached != nil && cached.Len() != 0 {
pageSize = IncrPageSize
for _, item := range cached.Items {
cachedItems[item.Title] = item
}
}
option := github.ListOptions{
Page: 1,
PerPage: pageSize,
}
var finish bool
for {
optns := &github.ActivityListStarredOptions{
Sort: "created",
Direction: "desc",
ListOptions: option,
}
repos, _, err := client.Activity.ListStarred(ctx, "", optns)
if err != nil {
return nil, err
}
for _, repo := range repos {
desc := ""
if repo.Repository.Description != nil {
desc = *repo.Repository.Description
}
// last starred repo has been cache, so finish call GitHub api
if _, finish = cachedItems[*repo.Repository.FullName]; finish {
break
}
cachedItems[*repo.Repository.FullName] = alfred.NewItem(*repo.Repository.FullName, desc, *repo.Repository.CloneURL)
}
if len(repos) < option.PerPage || finish {
break
}
option.Page += 1
}
res := alfred.NewItems()
for _, item := range cachedItems {
res.Append(item)
}
return res, nil
}
func main() {
variables, err := alfred.FlowVariables()
if err != nil {
alfred.ErrItems("Read variables error", err)
return
}
var duration time.Duration
duration, err = time.ParseDuration(variables[TTLKey])
if err != nil {
duration, _ = time.ParseDuration(DefaultTTL)
}
token := variables[TokenKey]
cli := alfred.NewApp("list github star repos")
cli.Bind("list", func(keys []string) {
var keyword string
if len(keys) > 0 {
keyword = keys[0]
}
f, err := os.OpenFile(CacheJsonFile, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
alfred.ErrItems("open cache file error", err)
return
}
defer f.Close()
gitStars := file.NewFileCache[*alfred.Items](f, duration)
old, err := gitStars.Load(func(cached *alfred.Items, _ time.Time) (*alfred.Items, error) { return ListRepos(cached, token) })
if err != nil {
alfred.ErrItems("load github star list error", err)
return
}
var match alfred.Items
for _, item := range old.Items {
if !strings.Contains(strings.ToLower(item.Title), strings.ToLower(keyword)) &&
!strings.Contains(item.SubTitle, keyword) {
continue
}
match.Append(item)
}
fmt.Println(match.Encode())
})
err = cli.Run(os.Args)
if err != nil {
alfred.ErrItems("run failed", err)
return
}
}