-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (96 loc) · 2.38 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"time"
)
func main() {
var args []string
if len(os.Args) > 1 {
args = os.Args[1:]
} else {
fmt.Println(`Usage of ./gitlab-migrate:
setup Initial setup
login Validate credentials
<pid> <destination> Migrate <pid> to <destination>`)
os.Exit(1)
}
switch args[0] {
case "login":
login()
os.Exit(0)
case "setup":
setup()
os.Exit(0)
}
if len(args) >= 3 {
log.Println("Too many arguments")
} else if len(args) == 2 {
migrate(args[0], args[1])
os.Exit(0)
} else if len(args) == 1 {
migrate(args[0], "")
}
}
type scheduleResp struct {
Message string `json:"message"`
}
const notFoundError = "404 Project Not Found"
// migrate performs the migration of the project from the source gitlab
// server to the destination server, for the given user or at the
// specified namespace.
func migrate(pid, dst string) {
c := fetchCredentials()
log.Println("Scheduling export")
resp, err := scheduleExport(c.ExportURI, c.ExportToken, pid)
if err != nil {
log.Fatal(err)
}
bs, err := ioutil.ReadAll(resp.Body)
var m scheduleResp
json.Unmarshal(bs, &m)
if m.Message == notFoundError {
log.Println("Project with PID", pid, "not found.")
os.Exit(1)
}
var r *statusResp
var filename string
// consider not looping forever
for {
// retrieve the export status
r, _, err = exportStatus(c.ExportURI, c.ExportToken, pid)
if err != nil {
log.Fatal(err)
}
log.Println("Export status:", r.ExportStatus)
if r.ExportStatus == "finished" {
t := time.Now()
filename = "./" + t.Format("01-02-2006") + "-" + r.Path + ".tar.gz"
log.Println("Downloading", filename)
// Download the project
_, err := exportDownload(c.ExportURI, c.ExportToken, pid, filename)
if err != nil {
log.Fatal(err)
}
break
}
time.Sleep(10 * time.Second)
}
// if the dst is not empty, create the namespace
// otherwise create the project in the token's user's projects.
if dst != "" {
// create the groups
log.Println("Creating groups")
gid := newGroup(c.ImportURI, c.ImportToken, dst)
// import the project
log.Println("Importing project")
_ = importFile(c.ImportURI, c.ImportToken, gid, r.Path, filename)
} else {
log.Println("Importing project")
_ = importFile(c.ImportURI, c.ImportToken, dst, r.Path, filename)
}
log.Println("Import complete")
}