-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
383 lines (312 loc) · 7.42 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
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
// Copyright 2020 Iglou.eu
// license that can be found in the LICENSE file
package main
import (
"crypto/sha256"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"git.iglou.eu/Laboratory/listea/icon"
"github.com/getlantern/systray"
"github.com/skratchdot/open-golang/open"
)
// AutoGenerated struct from standard v1 Gitea API
type AutoGenerated []struct {
ID int `json:"id"`
HTMLURL string `json:"html_url"`
Title string `json:"title"`
Body string `json:"body"`
Labels []struct {
Name string `json:"name"`
} `json:"labels"`
State string `json:"state"`
IsLocked bool `json:"is_locked"`
Comments int `json:"comments"`
UpdatedAt time.Time `json:"updated_at"`
ClosedAt time.Time `json:"closed_at"`
DueDate time.Time `json:"due_date"`
PullRequest struct {
Merged bool `json:"merged"`
} `json:"pull_request"`
Repository struct {
FullName string `json:"full_name"`
} `json:"repository"`
}
// APIResultList inline type def
type APIResultList struct {
entry []APIResult
}
// APIResult inline type def
type APIResult struct {
ID int
Title string
Body string
Comments int
Repository string
HTMLURL string
LabelsName string
state string
IsLocked bool
UpdatedAt time.Time
ClosedAt time.Time
DueDate time.Time
PRMerged bool
}
// ConfigAPI inline type def
type ConfigAPI struct {
APIURL string `json:"api_url"`
APIToken string `json:"api_token"`
List []List `json:"list"`
}
// QueryKey inline type def
type QueryKey struct {
Q string `json:"q"`
Type string `json:"type"`
State string `json:"state"`
Labels string `json:"labels"`
Milestones string `json:"milestones"`
}
// List inline type def
type List struct {
APIRequest string `json:"api_request"`
QueryKey QueryKey `json:"query_key"`
}
// APIRequest inline type def
type APIRequest struct {
URL []string
Hash []string
}
// MenuList for sub instance
type MenuList struct {
ItemTray []*systray.MenuItem
}
var apiRequest APIRequest
var menuItemList []MenuList
func init() {
var configDir string
switch runtime.GOOS {
case "windows":
configDir = filepath.Join(os.Getenv("APPDATA"), "listea")
case "darwin":
configDir = filepath.Join(os.Getenv("HOME"), "Library", "Application Support", "listea")
case "linux", "freebsd", "netbsd", "openbsd":
x := os.Getenv("XDG_CONFIG_HOME")
if x == "" {
configDir = filepath.Join(os.Getenv("HOME"), ".config", "listea")
} else {
configDir = filepath.Join(x, "listea")
}
default:
log.Fatal("Your operating system is not supported")
}
configFile := filepath.Join(configDir, "config.json")
if !fileExist(configDir) {
err := os.MkdirAll(configDir, 0755)
if err != nil {
log.Fatal(err)
}
}
if !fileExist(configFile) {
var itr ConfigAPI
itr.APIURL = "https://gitea.com/api/v1"
itr.List = make([]List, 1)
itr.List[0].QueryKey = QueryKey{"", "issues", "open", "", ""}
json, err := json.MarshalIndent(itr, "", " ")
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(configFile, json, 0750)
if err != nil {
log.Fatal(err)
}
}
cf, err := ioutil.ReadFile(configFile)
if err != nil {
log.Fatal(err)
}
var config ConfigAPI
err = json.Unmarshal(cf, &config)
if err != nil {
log.Fatal(err)
}
if config.APIURL == "" {
log.Fatal("No api Url on config file: ", configFile)
}
if config.APIToken == "" {
log.Fatal("No api Token on config file: ", configFile)
}
if len(config.List) < 1 || config.List[0].APIRequest == "" {
log.Fatal("Empty api request List or no Request configured: ", configFile)
}
apiRequest.URL = buildAPIRequest(config)
apiRequest.Hash = make([]string, len(apiRequest.URL))
menuItemList = make([]MenuList, len(apiRequest.URL))
go systray.Run(onReady, nil)
}
func main() {
for {
apiResult := make([]APIResultList, len(apiRequest.URL))
proceedAPIRequest(apiResult[:])
renderAPISystray(apiResult, menuItemList[:])
time.Sleep(1 * time.Minute)
systray.SetIcon(icon.Data)
}
}
func fileExist(f string) bool {
_, err := os.Stat(f)
if os.IsNotExist(err) {
return false
}
return true
}
func onReady() {
systray.SetIcon(icon.Data)
systray.SetTitle("Listea")
systray.SetTooltip("Task list viewer with a cup of tea")
}
func renderAPISystray(d []APIResultList, s []MenuList) {
if len(d) < 1 {
return
}
for i, v := range d {
if len(v.entry) < 1 {
continue
}
for j := range s[i].ItemTray {
s[i].ItemTray[j].Hide()
}
s[i] = menuItemAPI(v.entry)
}
}
func menuItemAPI(d []APIResult) MenuList {
var m MenuList
s := systray.AddMenuItem(fmt.Sprintf("📋 %s", d[0].Repository), "")
// For grey style
s.Disable()
// To be remove with other entry
m.ItemTray = append(m.ItemTray, s)
for _, v := range d {
var label string
if v.LabelsName != "" {
label = fmt.Sprintf("[%s] ", v.LabelsName)
}
t := s.AddSubMenuItem(fmt.Sprintf(" [%d] %s%s", v.Comments, label, v.Title), v.Body)
if v.state == "closed" || v.IsLocked || v.PRMerged {
t.Disable()
}
m.ItemTray = append(m.ItemTray, t)
go trayIsClicked(m.ItemTray[len(m.ItemTray)-1], v.HTMLURL)
}
return m
}
func trayIsClicked(t *systray.MenuItem, url string) {
for {
select {
case <-t.ClickedCh:
open.Run(url)
}
}
}
func proceedAPIRequest(o []APIResultList) {
for i, v := range apiRequest.URL {
get, err := http.Get(v)
if err != nil {
systray.SetIcon(icon.Err)
log.Println("Unable to get on : " + v)
log.Println(err)
continue
}
defer get.Body.Close()
if get.StatusCode != 200 {
systray.SetIcon(icon.Err)
log.Println("Can't access to API by:", strings.Split(v, "?")[0])
continue
}
body, err := ioutil.ReadAll(get.Body)
if err != nil {
systray.SetIcon(icon.Err)
log.Println("Unable read body : " + v)
log.Println(err)
continue
}
h := sha256.New()
h.Write([]byte(body))
hsum := fmt.Sprintf("%x", h.Sum(nil))
if apiRequest.Hash[i] != hsum {
if apiRequest.Hash[i] != "" {
systray.SetIcon(icon.New)
}
o[i] = proceedAPIResult(body)
apiRequest.Hash[i] = hsum
}
}
}
func proceedAPIResult(body []byte) APIResultList {
var res APIResultList
var api AutoGenerated
err := json.Unmarshal(body, &api)
if err != nil {
log.Fatal(err)
}
for _, v := range api {
var data APIResult
data.ID = v.ID
data.Title = v.Title
data.Body = v.Body
data.Comments = v.Comments
data.Repository = v.Repository.FullName
data.HTMLURL = v.HTMLURL
data.state = v.State
data.IsLocked = v.IsLocked
data.UpdatedAt = v.UpdatedAt
data.ClosedAt = v.ClosedAt
data.DueDate = v.DueDate
data.PRMerged = v.PullRequest.Merged
if len(v.Labels) > 0 {
data.LabelsName = v.Labels[0].Name
}
res.entry = append(res.entry, data)
}
return res
}
func buildAPIRequest(config ConfigAPI) []string {
var list []string
for _, v := range config.List {
list = append(list,
fmt.Sprintf(
"%s%s?%stoken=%s",
config.APIURL,
v.APIRequest,
buildAPIQuery(v.QueryKey),
config.APIToken,
),
)
}
return list
}
func buildAPIQuery(query QueryKey) string {
o := ""
if query.Q != "" {
o += fmt.Sprintf("q=%s&", query.Q)
}
if query.Type != "" {
o += fmt.Sprintf("type=%s&", query.Type)
}
if query.State != "" {
o += fmt.Sprintf("state=%s&", query.State)
}
if query.Labels != "" {
o += fmt.Sprintf("labels=%s&", query.Labels)
}
if query.Milestones != "" {
o += fmt.Sprintf("milestones=%s&", query.Milestones)
}
return o
}