-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
104 lines (81 loc) · 1.81 KB
/
github.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
package main
import (
"hawx.me/code/ggg/git"
"hawx.me/code/ggg/repos"
"code.google.com/p/goauth2/oauth"
"github.com/google/go-github/github"
"flag"
"fmt"
"log"
)
var (
dbPath = flag.String("db-path", "", "Path to ggg database")
gitDir = flag.String("git-dir", "", "Path to ggg git directory")
token = flag.String("token", "", "A personal access token")
)
const USAGE = `Usage: ggg-import-github [options]
Imports all repositories on your GitHub account to ggg.
--db-path # Path to ggg database
--git-dir # Path to ggg git directory
--token # A personal access token
`
func main() {
flag.Parse()
if *dbPath == "" || *gitDir == "" || *token == "" {
fmt.Println(USAGE)
return
}
t := &oauth.Transport{
Token: &oauth.Token{AccessToken: *token},
}
client := github.NewClient(t.Client())
db := repos.Open(*dbPath, *gitDir)
defer db.Close()
opt := &github.RepositoryListOptions{
Type: "owner",
}
for {
repos, resp, err := client.Repositories.List("", opt)
if err != nil {
log.Fatal(err)
}
for _, repo := range repos {
copyRepo(db, repo)
}
if resp.NextPage == 0 {
break
}
opt.ListOptions.Page = resp.NextPage
}
}
func copyRepo(db repos.Db, repo github.Repository) {
if db.Get(*repo.Name).Name != "" {
log.Println("skipping", *repo.Name)
return
}
homepage := ""
if repo.Homepage != nil {
homepage = *repo.Homepage
}
description := ""
if repo.Description != nil {
description = *repo.Description
}
private := false
if repo.Private != nil {
private = *repo.Private
}
branch := ""
if repo.DefaultBranch != nil {
branch = *repo.DefaultBranch
}
db.Create(
*repo.Name,
homepage,
description,
branch,
private)
created := db.Get(*repo.Name)
git.CopyRepo(created.Path, *repo.CloneURL)
log.Println("copied", *repo.Name)
}