-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.go
148 lines (115 loc) · 2.59 KB
/
watch.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
package main
import (
"flag"
"fmt"
"os"
"path"
"sort"
)
var (
configFile string
debug bool
count int
tail bool
orgs = stringsl{}
users = stringsl{}
userOrgs = stringsl{}
repos = stringsl{}
)
func main() {
flag.StringVar(&configFile, "config", path.Join(os.Getenv("HOME"), ".config", "feedme"), "The location of the config file where github credentials are stored")
flag.IntVar(&count, "n", 30, "The amount of feed items to retrieve")
flag.BoolVar(&debug, "debug", false, "Enable debug logging")
flag.BoolVar(&tail, "f", false, "Poll the API to retrieve feed events as they happen")
flag.Var(&orgs, "org", "Public organization feed")
flag.Var(&userOrgs, "user-org", "Organization feed for the logged in user")
flag.Var(&repos, "repo", "Repository feed")
flag.Var(&users, "user", "User feed")
flag.Parse()
a := loadFileAuth()
if a == nil {
a = promptAuth()
}
if a == nil {
fmt.Println("Could not get an authorization token from Github")
os.Exit(1)
}
saveFileAuth(a)
cl := client{a.Token}
u, err := cl.getUser()
if err != nil {
fmt.Printf("User load error: %s\n", err)
os.Exit(1)
}
var (
ch = make(chan *pollMsg)
urls = []string{}
polling = map[string]int{}
)
for _, org := range orgs {
urls = append(urls, fmt.Sprintf("/orgs/%s/events", org))
}
for _, org := range userOrgs {
urls = append(urls, fmt.Sprintf("/users/%s/events/orgs/%s", u.Login, org))
}
for _, repo := range repos {
urls = append(urls, fmt.Sprintf("/repos/%s/events", repo))
}
for _, u := range users {
urls = append(urls, fmt.Sprintf("/users/%s/events", u))
}
if len(urls) == 0 {
urls = append(urls, fmt.Sprintf("/users/%s/received_events", u.Login))
}
for _, u := range urls {
if _, ok := polling[u]; ok {
continue
}
polling[u] = 0
cl.pollEvents(u, count, ch)
}
var (
first = false
events = events{}
)
for msg := range ch {
polling[msg.url]++
if !first {
first = true
for _, c := range polling {
if c == 0 {
first = false
break
}
}
}
if msg.err != nil {
warnf("Events load error: %s\n", msg.err)
continue
}
events = append(events, msg.events...)
if first {
sort.Sort(events)
for _, ev := range events {
printEvent(&ev)
}
events = nil
if !tail {
os.Exit(0)
}
}
}
}
func printEvent(ev *Event) {
fmt.Println("\n" + ev.Summary())
}
type events []Event
func (e events) Len() int {
return len(e)
}
func (e events) Less(i, j int) bool {
return e[i].CreatedAt.Before(e[j].CreatedAt)
}
func (e events) Swap(i, j int) {
e[i], e[j] = e[j], e[i]
}