-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbackup.go
More file actions
77 lines (66 loc) · 1.75 KB
/
backup.go
File metadata and controls
77 lines (66 loc) · 1.75 KB
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
package main
import (
"log"
"os"
"strings"
"github.com/Frontware/GitLabBack/config"
"github.com/Frontware/GitLabBack/git"
"github.com/Frontware/GitLabBack/gitlab"
)
// oauthHTTPURL returns url for open authorization
func oauthHTTPURL(httpURL, token string) string {
if strings.HasPrefix(httpURL, "https") {
return httpURL[0:8] + "oauth2:" + token + "@" + httpURL[8:]
}
return httpURL[0:7] + "oauth2:" + token + "@" + httpURL[7:]
}
func backup(conf *config.Config) {
// init GitLab client
c := gitlab.New(conf)
// Request groups
groups, err := c.ListGroups()
if err != nil {
log.Fatal(err.Error())
}
for _, group := range groups {
// Request projects for a group
projects, err := c.ListProjects(group.ID)
if err != nil {
log.Print(err.Error())
}
for _, project := range projects {
var repoURL string
if conf.SSH {
repoURL = project.SSHURLToRepo
} else {
if len(conf.Token) > 0 {
repoURL = oauthHTTPURL(project.HTTPURLToRepo, conf.Token)
} else {
repoURL = project.HTTPURLToRepo
}
}
myRepo := git.Repo{
Repo: repoURL,
Dir: conf.BackupDir + project.PathWithNamespace,
Name: project.Name,
FullName: project.NameWithNamespace,
}
if _, err := os.Stat(myRepo.Dir); err == nil {
log.Printf("Pulling %s", myRepo.FullName)
if _, err := myRepo.Pull(); err == nil {
// Pull successfully
continue
}
log.Printf("Error while pulling %s", myRepo.FullName)
log.Printf("Removing %s", myRepo.FullName)
if err := os.RemoveAll(myRepo.Dir); err != nil {
log.Print(err.Error())
}
}
log.Printf("Cloning %s", myRepo.FullName)
if _, err := myRepo.Clone(); err != nil {
log.Printf("Error while cloning %s", myRepo.FullName)
}
}
}
}