-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
77 lines (58 loc) · 1.33 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
package main
import (
"bufio"
"context"
"fmt"
"os"
"time"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
func main() {
token := os.Getenv("GH_AUTH_TOKEN")
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) != 0 {
fmt.Fprintln(os.Stderr, "No users detected. Hint: cat users.txt | gh-repos")
os.Exit(1)
}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
user := scanner.Text()
// Get repos for each user
opt := &github.RepositoryListOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
for {
repos, resp, err := client.Repositories.List(ctx, user, opt)
if resp.StatusCode == 404 {
fmt.Println("User not found")
break
}
if err != nil {
fmt.Println(err)
if resp.Remaining == 0 {
fmt.Printf("Rate limit exceeded, waitting for %+v minutes\n", resp.Rate.Reset.Sub(time.Now()).Minutes())
time.Sleep(resp.Rate.Reset.Sub(time.Now()))
}
continue
}
// Print repos
for _, repo := range repos {
if *repo.Fork {
continue
}
fmt.Println(user + "/" + repo.GetName())
}
opt.Page = resp.NextPage
if resp.NextPage == 0 {
break
}
}
}
}