-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (58 loc) · 1.32 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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
"gopkg.in/src-d/go-git.v4"
)
var (
org string
token string
path string
)
func main() {
flag.StringVar(&org, "org", "productsupcom", "GitHub org name")
flag.StringVar(&token, "token", "", "GitHub token")
flag.StringVar(&path, "path", "/tmp/backup", "Git local clone path")
flag.Parse()
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
opt := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
var allRepos []*github.Repository
fmt.Println("Querying the Github API")
for {
repos, resp, err := client.Repositories.ListByOrg(ctx, org, opt)
if err != nil {
log.Fatalln(err)
}
allRepos = append(allRepos, repos...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
for _, repo := range allRepos {
fmt.Println(*repo.SSHURL)
if *repo.Archived == true {
fmt.Printf("Excluding archived repository %s from backup\n", *repo.Name)
} else {
_, err := git.PlainClone(path+"/"+*repo.Name, false, &git.CloneOptions{
URL: *repo.SSHURL,
Progress: os.Stdout,
})
if err != nil {
fmt.Println(err)
}
}
}
}