-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
171 lines (144 loc) · 4.06 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
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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
"sync"
"syscall"
"time"
"repo-backup/utils"
"path/filepath"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
"golang.org/x/term"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go/aws/awserr"
)
var wg sync.WaitGroup
func clone(url string, directory string, access_token string) {
defer wg.Done()
utils.Info("git clone %s %s", url, directory)
r, err := git.PlainClone(directory, false, &git.CloneOptions{
Auth: &http.BasicAuth{
Username: "blah",
Password: access_token,
},
URL: url,
Progress: os.Stdout,
})
utils.CheckIfError(err)
// ... retrieving the branch being pointed by HEAD
if r == nil {
return
} else {
ref, err := r.Head()
utils.CheckIfError(err)
// ... retrieving the commit object
commit, err := r.CommitObject(ref.Hash())
utils.CheckIfError(err)
fmt.Println(commit)
}
}
func listRepositories(access_token string) []*github.Repository {
opt := &github.RepositoryListOptions{
ListOptions: github.ListOptions{PerPage: 40},
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: access_token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
// list all repositories for the authenticated user
repos, _, err := client.Repositories.List(ctx, "", opt)
if err != nil {
fmt.Println(err.Error())
}
return repos
}
func returnCreds(region string) aws.Config {
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region))
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
return cfg
}
func uploadFileToS3(s3BucketName string, fileName string) *s3.PutObjectOutput {
svc := s3.NewFromConfig(returnCreds("us-east-1"))
input := &s3.PutObjectInput{
Body: strings.NewReader(fileName),
Bucket: aws.String(s3BucketName),
Key: aws.String(fileName),
Tagging: aws.String("Purpose=Created from repo-backup tool"),
}
result, err := svc.PutObject(context.TODO(), input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
fmt.Println(aerr.Error())
}
} else {
fmt.Println(err.Error())
}
} else {
fmt.Printf("Object %s uploaded", fileName)
}
return result
}
func main() {
var directory string
var outFileName string
var filter string
var s3bucket string
var s3bucketKey string
var timestamp int32 = int32(time.Now().Unix())
var timestampString string = strconv.FormatInt(int64(timestamp), 10)
flag.StringVar(&directory, "d", "", "Specify path to destination to download repos")
flag.StringVar(&filter, "f", "", "Specify filter to match just a certain amount of repos")
flag.StringVar(&outFileName, "o", "", "Specify the resulting name of the zip file")
flag.StringVar(&s3bucket, "s3", "", "Specify the S3 bucket to store the zipped repos")
flag.StringVar(&s3bucketKey, "s3key", "", "Specify the S3 bucket path to store the zipped repos")
flag.Parse()
fmt.Print("Personal Access Token: ")
fmt.Println()
bytepw, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
os.Exit(1)
}
token := string(bytepw)
repos := listRepositories(token)
directory = directory + timestampString
outFileName = outFileName + timestampString + ".zip"
for i := range repos {
if len(filter) == 0 {
go clone(repos[i].GetCloneURL(), filepath.Join(directory, *repos[i].FullName), token)
wg.Add(1)
} else {
if strings.Contains(repos[i].GetName(), filter) {
go clone(repos[i].GetCloneURL(), filepath.Join(directory, *repos[i].FullName), token)
wg.Add(1)
}
}
}
wg.Wait()
fmt.Println("zipping repos at destination path....")
if err := utils.ZipSource(directory, outFileName); err != nil {
log.Fatal(err.Error())
}
if len(s3bucket) != 0 {
if len(s3bucketKey) != 0 {
uploadFileToS3(s3bucket, filepath.Join(s3bucketKey, outFileName))
} else {
uploadFileToS3(s3bucket, outFileName)
}
}
}