/
main.go
89 lines (73 loc) · 1.73 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/url"
"os"
"strconv"
"strings"
"github.com/google/go-github/github"
"github.com/olekukonko/tablewriter"
)
var (
amount = flag.Int("amount", -1, "Set the maximum amount of issues to show")
repo = flag.String("repo", "", "Repository on http://www.github.com to list the issues from")
client = github.NewClient(nil)
)
func main() {
flag.Usage = func() {
fmt.Println("Usage of Issues table")
flag.PrintDefaults()
}
flag.Parse()
githubRepoURL := *repo
if githubRepoURL == "" {
flag.Usage()
os.Exit(1)
}
u, err := url.Parse(githubRepoURL)
if err != nil {
fmt.Printf("Invalid url! %s", err)
os.Exit(1)
}
path := strings.Split(u.EscapedPath(), "/")
if len(path) < 3 {
fmt.Println("Invalid github URL")
os.Exit(1)
}
opt := &github.IssueListByRepoOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
var issues []*github.Issue
for len(issues) <= *amount || *amount < 0 {
newIssues, resp, err := client.Issues.ListByRepo(context.Background(), path[1], path[2], opt)
if err != nil {
log.Fatalf("error fetching issues for %v/%v: %v", path[1], path[2], err)
}
issues = append(issues, newIssues...)
if resp.NextPage == 0 {
break
}
opt.ListOptions.Page = resp.NextPage
}
if len(issues) == 0 {
fmt.Printf("No issues found for this repository")
os.Exit(0)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"number", "created_at", "title"})
for index, issue := range issues {
if index == *amount {
break
}
column := make([]string, 3)
column[0] = strconv.Itoa(*issue.Number)
column[1] = issue.CreatedAt.String()
column[2] = *issue.Title
table.Append(column)
}
table.SetRowLine(true)
table.Render()
}