-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
175 lines (145 loc) · 4.57 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
172
173
174
175
package main
import (
"github.com/alitto/pond"
"os"
"path/filepath"
"sync"
)
func main() {
pullRepositories()
}
func pullRepositories() {
// load all the config options
config := NewConfig()
// create a logger to use throughout the application
logger := NewLogger(config.LogFile)
defer logger.Sync()
logger.Info("Starting gitsync")
if config.IsDryRun {
logger.Info("Dry run")
}
// create a bitbucket client to interact with the bitbucket api
client, err := NewBitbucketClient(config.User, config.Key, config.Secret, logger)
if err != nil {
logger.Panic(err)
}
workspaces, err := client.WorkspaceList()
if err != nil {
logger.Panic(err)
}
// slice to store list of failed repos
failed := make([]Repository, 0)
// create a mutex, so we can guard access to the
// failed slice from within goroutines
var mu sync.Mutex
// create a worker pool, so we can only copy up to 7
// repositories at a time
pool := pond.New(7, 1000)
// loop through all the workspaces available to our account.
// we are going to pull every repo in each workspace, so we are
// going to be looking through all of them one by one.
for _, workspace := range workspaces {
repositories, err := client.RepositoryList(&workspace)
if err != nil {
logger.Panic(err)
}
// loop through each repository in the workspace and pull it
for _, repository := range repositories {
r := repository
// add goroutine to the pool
pool.Submit(func() {
logger.Info("Copy:", r.Full_name)
if !config.IsDryRun {
// create a path to copy the repository and a backup path
repoDir := filepath.Join(config.OutputDir, r.Full_name)
backupDir := repoDir + "-backup"
// check if either of these paths exist
repoExists, err := exists(repoDir)
if err != nil {
logger.Panic(err)
}
backupExists, err := exists(repoDir)
if err != nil {
logger.Panic(err)
}
// at this point we can be in one of the following states:
// 1. No directory and no '-backup' directory - no special action required
// 2. No directory and a '-backup' directory - no special action required
// 3. A directory and no '-backup' directory - special action needed
// 4. A directory and a '-backup' directory - special action needed
// 3. A directory and no '-backup' directory
if repoExists && !backupExists {
// create a backup
if err := archive(repoDir, backupDir); err != nil {
logger.Panic(err)
}
}
// 4. A directory and a '-backup' directory
if repoExists && backupExists {
// 1. delete the old backup
if err := os.RemoveAll(backupDir); err != nil {
logger.Panic(err)
}
// 2. create a new backup
if err := archive(repoDir, backupDir); err != nil {
logger.Panic(err)
}
}
// store the path we need to remove following the copy operation.
// by default this will be the '-backup' version because we only want
// to keep the latest version, but if the copy fails we want to clean
// up the new version and keep the -backup one
cleanupDir := backupDir
// create a directory for the repository. This will create the
// following directory structure: <WorkingDir>\<workspaceSlug>\<repositorySlug>
if err := os.MkdirAll(repoDir, os.ModePerm); err != nil {
logger.Panic(err)
}
// copy the repository. If the copy fails record it and
// move onto the next one, so we pull as many as we can.
if out, err := client.Copy(repoDir, &r); err != nil {
logger.Warn("Copy failed\t", r.Full_name, " (", out, ")")
// the copy failed so the new directory needs to be cleaned up
cleanupDir = repoDir
// add repository to failed in a concurrent safe way
mu.Lock()
failed = append(failed, r)
mu.Unlock()
}
// cleanup the relevant path
if err := os.RemoveAll(cleanupDir); err != nil {
logger.Panic(err)
}
}
})
}
}
// wait for all goroutines to complete
pool.StopAndWait()
// if there are any failed pulls list the names of
// the repos that failed
logger.Info("Failed to pull: ", len(failed))
for _, repository := range failed {
logger.Info(">> ", repository.Full_name)
}
if config.IsDryRun {
logger.Info("Dry run")
}
logger.Info("Completed gitsync")
}
func archive(path, pathOld string) error {
if err := os.Rename(path, pathOld); err != nil {
return err
}
return nil
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}