-
Notifications
You must be signed in to change notification settings - Fork 0
/
fallingstar.go
173 lines (157 loc) · 3.85 KB
/
fallingstar.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
package main
import (
"context"
"fmt"
"os"
"os/exec"
"time"
"github.com/google/go-github/github"
)
const binGit = "/bin/git"
const usrBinGit = "/usr/bin/git"
const usrLocalGit = "/usr/local/bin/git"
const perPage = 100
// Star holds a bunch of
type Star struct {
Page int
NextPage bool
User string
Limit int
Remaining int
LastPage int
Reset github.Timestamp
}
func main() {
var user string
if len(os.Args) >= 2 {
user = os.Args[1]
} else {
fmt.Fprintln(os.Stderr, "Must supply github username")
os.Exit(1)
}
s := &Star{
User: user,
Page: 1,
NextPage: true,
}
for s.NextPage {
r, err := s.getRepos(s.Page, perPage)
if err != nil {
Oopse(err)
}
fmt.Fprintln(os.Stderr, s.status())
loopStarred(r)
fmt.Printf("\nGithub api limit: %v. Github api remaining: %v. Github api reset %v\n",
s.Limit, s.Remaining, s.Reset)
}
}
func (s *Star) getRepos(page, perpage int) ([]*github.StarredRepository, error) {
client := github.NewClient(nil)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
starredRepos, resp, err := client.Activity.ListStarred(ctx, s.User, &github.ActivityListStarredOptions{
ListOptions: github.ListOptions{
PerPage: perpage,
Page: page,
},
})
if err != nil {
return nil, err
}
if resp.NextPage == 0 {
s.NextPage = false
} else {
s.NextPage = true
}
s.Page = resp.NextPage
s.Reset = resp.Reset
s.Limit = resp.Limit
s.Remaining = resp.Remaining
s.LastPage = resp.LastPage
return starredRepos, err
}
func (s *Star) status() string {
if s.Page == 0 {
return "On last page"
}
return fmt.Sprintf("On page %d of %d\n", s.Page-1, s.LastPage)
}
func loopStarred(starredRepos []*github.StarredRepository) {
for i := range starredRepos {
repo := starredRepos[i].Repository
fullName := *repo.FullName
_, err := os.Stat(fullName)
if err != nil {
fmt.Printf("Going to clone into %s via %s\n", fullName, *repo.CloneURL)
err = execGitCmd(fullName, *repo.CloneURL)
if err != nil {
fmt.Println("Failed to clone ", fullName, err)
}
time.Sleep(time.Second * 5)
} else {
fmt.Printf("Unable to clone into path %s. It already exists\n", fullName)
err = execGitCmd(fullName, "")
if err != nil {
fmt.Println("Failed to pull git repo ", err)
}
time.Sleep(time.Second * 1)
}
}
}
// buildGitCmd creates the git clone or git pull command. If url is empty
// string it creates a git pull instead of git clone
func gitCmd(path string, url string) (*exec.Cmd, error) {
gitPath := findGit()
if gitPath == "" {
return nil, fmt.Errorf("unable to find git on PATH or %s %s %s", binGit, usrBinGit, usrLocalGit)
}
var pwd string
var gitCmd []string
if url != "" {
gitCmd = []string{gitPath, "clone", url, path}
} else {
gitCmd = []string{gitPath, "pull", "--ff-only"}
pwd = path
}
command := exec.Command(gitPath, gitCmd[1:]...) // #nosec
command.Stdout = os.Stdout
command.Stderr = os.Stdout
// Should we make this the fully qualified path?
command.Dir = pwd
return command, nil
}
// execGitCmd is a wrapper around cmd.Start from gitCmd. It makes testing easier
func execGitCmd(path, url string) error {
cmd, err := gitCmd(path, url)
if err != nil {
return err
}
return cmd.Run()
}
func findGit() string {
gitPath, err := exec.LookPath("git")
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to find git on path. Looking at some other places")
for _, gitPath := range []string{binGit, usrBinGit, usrLocalGit} {
if exists(gitPath) {
return gitPath
}
}
fmt.Fprintln(os.Stderr, "Failed to find git entirely")
return ""
}
return gitPath
}
func exists(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func Oopse(err error) {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}