-
Notifications
You must be signed in to change notification settings - Fork 41
/
main.go
482 lines (381 loc) · 10 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
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
package main
import (
"bufio"
"fmt"
"os"
"os/user"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
"gopkg.in/ini.v1"
)
func yOrN(question string, alwaysYes bool) (bool, error) {
if alwaysYes {
return true, nil
}
reader := bufio.NewReader(os.Stdin)
fmt.Printf("%s [y/n] ", question)
input, err := reader.ReadString('\n')
text := strings.TrimSpace(input)
for err == nil && text != "y" && text != "n" {
fmt.Printf("%s [y/n] ", question)
text, err = reader.ReadString('\n')
text = strings.TrimSpace(text)
}
if err != nil || text == "n" {
return false, err
}
return true, err
}
func listSubcommand(project Project, filter func(todo Todo) bool) error {
todosToList := []*Todo{}
err := project.WalkTodosOfDir(".", func(todo Todo) error {
if filter(todo) {
todosToList = append(todosToList, &todo)
}
return nil
})
if err != nil {
return err
}
sort.Slice(todosToList, func(i, j int) bool {
return todosToList[i].Urgency > todosToList[j].Urgency
})
for _, todo := range todosToList {
fmt.Println(todo.LogString())
}
return nil
}
func reportSubcommand(project Project, creds IssueAPI, repo string, prependBody string, alwaysYes bool) error {
todosToReport := []*Todo{}
err := project.WalkTodosOfDir(".", func(todo Todo) error {
if todo.ID != nil {
return nil
}
fmt.Printf("%v\n", todo.LogString())
fmt.Printf("Issue Title: %s\n", todo.Title)
for _, bodyLine := range todo.Body {
fmt.Printf(" %s\n", bodyLine)
}
yes, err := yOrN("Do you want to report this? ", alwaysYes)
if err != nil {
return err
}
if yes {
todosToReport = append(todosToReport, &todo)
}
return nil
})
for _, todo := range todosToReport {
reportedTodo, err := todo.Report(creds, repo,
prependBody+"\n\n"+strings.Join(todo.Body, "\n\n"))
if err != nil {
return err
}
fmt.Printf("[REPORTED] %v\n", reportedTodo.LogString())
err = reportedTodo.Update()
if err != nil {
return err
}
err = reportedTodo.GitCommit("Add")
if err != nil {
return err
}
}
return err
}
func purgeSubcommand(project Project, creds IssueAPI, repo string, alwaysYes bool) error {
todosToRemove := []*Todo{}
err := project.WalkTodosOfDir(".", func(todo Todo) error {
if todo.ID == nil {
return nil
}
status, err := todo.RetrieveStatus(creds, repo)
if err != nil {
return err
}
if status != "closed" {
fmt.Printf("[OPEN] %v\n", todo.LogString())
return nil
}
fmt.Printf("[CLOSED] %v\n", todo.LogString())
fmt.Printf("Issue link: https://%s/%s/issues/%s\n",
creds.getHost(), repo, (*todo.ID)[1:])
yes, err := yOrN("This issue is closed. Do you want to remove the TODO?", alwaysYes)
if err != nil {
return err
}
if yes {
todosToRemove = append(todosToRemove, &todo)
}
return err
})
if err != nil {
return err
}
sort.Slice(todosToRemove, func(i, j int) bool {
if todosToRemove[i].Filename == todosToRemove[j].Filename {
return todosToRemove[i].Line > todosToRemove[j].Line
}
return todosToRemove[i].Filename < todosToRemove[j].Filename
})
for _, todo := range todosToRemove {
err = todo.Remove()
if err != nil {
return err
}
fmt.Printf("[REMOVED] %v\n", todo)
err = todo.GitCommit("Remove")
if err != nil {
return err
}
}
return err
}
func usage() {
// FIXME(#9): implement a map for options instead of println'ing them all there
fmt.Printf("snitch [opt]\n" +
"\tlist [--unreported] [--reported] [--y] [--remote]: lists all todos of a dir recursively\n" +
"\treport [--prepend-body <issue-body>] [--y] [--remote]: reports all todos of a dir recursively \n\t\tas GitHub issues\n" +
"\tpurge [--remote]: removes all of the reported TODOs that refer to closed issues\n")
}
func locateDotGit(dir string) (string, error) {
absDir, err := filepath.Abs(dir)
rooted := ""
if err != nil {
return "", err
}
for absDir != rooted {
dotGit := path.Join(absDir, ".git")
if stat, err := os.Stat(dotGit); !os.IsNotExist(err) && stat.IsDir() {
return dotGit, nil
}
rooted = absDir
absDir = filepath.Dir(absDir)
}
return "", fmt.Errorf("Couldn't find .git. Maybe you are not inside of a git repo")
}
func getURLAliases() (map[string]string, error) {
usr, err := user.Current()
if err != nil {
return map[string]string{}, nil
}
path := path.Join(usr.HomeDir, ".gitconfig")
cfg, err := ini.Load(path)
if err != nil {
return map[string]string{}, nil
}
sections := cfg.Sections()
aliases := map[string]string{}
for _, elem := range sections {
sectionName := elem.Name()
regex := regexp.MustCompile("url \"([-\\w]+@[github.com|gitlab.com][^\"]+)\"")
urlSections := regex.FindAllStringSubmatch(sectionName, -1)
for _, elem := range urlSections {
section := cfg.Section(elem[0])
alias, err := section.GetKey("insteadOf")
if err != nil {
return map[string]string{}, nil
}
aliases[alias.Value()] = elem[1]
}
}
return aliases, nil
}
func getRemote(params map[string]string) string {
project := getProject(".")
if len(params["remote"]) > 0 {
return params["remote"]
} else if len(project.Remote) > 0 {
return project.Remote
}
return "origin"
}
func getRepo(directory string, remote string) (string, IssueAPI, error) {
credentials := getCredentials()
if len(credentials) == 0 {
return "", nil, fmt.Errorf("No credentials have been found. Read https://github.com/tsoding/snitch#credentials")
}
dotGit, err := locateDotGit(directory)
if err != nil {
return "", nil, err
}
configPath := path.Join(dotGit, "config")
cfg, err := ini.Load(configPath)
if err != nil {
return "", nil, err
}
origin := cfg.Section("remote \"" + remote + "\"")
if origin == nil {
return "", nil, fmt.Errorf("The git repo doesn't have any origin remote. " +
"Please use `git remote add' command to add one.")
}
url := origin.Key("url")
if url == nil {
return "", nil, fmt.Errorf("The origin remote doesn't have any URL's " +
"associated with it.")
}
aliases, err := getURLAliases()
if err != nil {
return "", nil, err
}
urlString := url.String()
for key, value := range aliases {
if strings.Contains(urlString, key) {
urlString = strings.Replace(urlString, key, value, 1)
break
}
}
for _, creds := range credentials {
s := creds.getHost() + "[:/]([-\\.\\w]+)\\/([-\\.\\w]+)"
hostRegex := regexp.MustCompile(s)
groups := hostRegex.FindStringSubmatch(strings.TrimSuffix(urlString, ".git"))
if groups != nil {
return groups[1] + "/" + groups[2], creds, nil
}
}
if urlString == "" {
urlString = fmt.Sprintf("Remote: '%v'", remote)
}
return "", nil, fmt.Errorf("%s does not match any of the hosts", urlString)
}
func parseParams(args []string) (map[string]string, error) {
currentParam := ""
result := map[string]string{}
for _, arg := range args {
if strings.HasPrefix(arg, "--") { // Long Flag
if len(currentParam) != 0 {
result[currentParam] = ""
}
currentParam = arg[2:]
} else if strings.HasPrefix(arg, "-") { // Short Flags
if len(currentParam) != 0 {
result[currentParam] = ""
}
currentParam = arg[1:]
} else { // Value
if len(currentParam) == 0 {
return nil, fmt.Errorf("Value %v is not associated with any flag", arg)
}
result[currentParam] = arg
currentParam = ""
}
}
if len(currentParam) != 0 {
result[currentParam] = ""
}
return result, nil
}
func checkParams(params map[string]string, allowedParams []string) error {
for param := range params {
allowed := false
for _, allowedParam := range allowedParams {
if param == allowedParam {
allowed = true
break
}
}
if !allowed {
return fmt.Errorf("Unknown flag `%s'", param)
}
}
return nil
}
func locateProject(directory string) (string, error) {
dotGit, err := locateDotGit(directory)
if err != nil {
return "", err
}
return filepath.Dir(dotGit), nil
}
func exitOnError(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func getCredentials() []IssueAPI {
creds := []IssueAPI{}
if github, err := getGithubCredentials(); err == nil {
creds = append(creds, github)
}
creds = getGitlabCredentials(creds)
creds = getGiteaCredentials(creds)
return creds
}
func getProject(directory string) *Project {
projectPath, err := locateProject(directory)
exitOnError(err)
project, err := NewProject(projectPath)
exitOnError(err)
return project
}
func main() {
project := getProject(".")
if len(os.Args) > 1 {
switch os.Args[1] {
case "list":
params, err := parseParams(os.Args[2:])
exitOnError(err)
err = checkParams(params, []string{"unreported", "reported", "remote"})
exitOnError(err)
_, unreported := params["unreported"]
_, reported := params["reported"]
err = listSubcommand(*project, func(todo Todo) bool {
filter := reported == unreported
if unreported {
filter = filter || todo.ID == nil
}
if reported {
filter = filter || todo.ID != nil
}
return filter
})
exitOnError(err)
case "report":
params, err := parseParams(os.Args[2:])
exitOnError(err)
err = checkParams(params, []string{"prepend-body", "y", "remote"})
if err != nil {
fmt.Fprintln(os.Stderr, err)
usage()
os.Exit(1)
}
prependBody, ok := params["prepend-body"]
if !ok {
prependBody = ""
}
_, alwaysYes := params["y"]
repo, creds, err := getRepo(".", getRemote(params))
exitOnError(err)
fmt.Printf("Detected project: https://%s/%s\n", creds.getHost(), repo)
if err = reportSubcommand(*project, creds, repo, prependBody, alwaysYes); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
case "purge":
params, err := parseParams(os.Args[2:])
exitOnError(err)
err = checkParams(params, []string{"y", "remote"})
if err != nil {
fmt.Fprintln(os.Stderr, err)
usage()
os.Exit(1)
}
repo, creds, err := getRepo(".", getRemote(params))
exitOnError(err)
_, alwaysYes := params["y"]
if err = purgeSubcommand(*project, creds, repo, alwaysYes); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
default:
fmt.Fprintf(os.Stderr, "`%s` unknown command\n", os.Args[1])
os.Exit(1)
}
} else {
usage()
}
}