-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
51 lines (47 loc) · 1.07 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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"syscall"
)
var urlRegexp = regexp.MustCompile(`^(?:git@|https://)(github\.com|gitlab\.com)[:/]([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+).git$`)
func die(msg string, a ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", a...)
os.Exit(1)
}
func main() {
args := os.Args
if len(args) < 2 {
die("usage: %s <repository>", filepath.Base(os.Args[0]))
os.Exit(1)
}
repo := args[1]
slug := urlRegexp.FindStringSubmatch(repo)
if slug == nil {
die("invalid url")
os.Exit(1)
}
gitpath := os.Getenv("GITPATH")
if gitpath == "" {
die("GITPATH is not set")
}
host := slug[1]
user := slug[2]
project := slug[3]
target := filepath.Join(gitpath, host, user, project)
err := os.MkdirAll(target, 0777)
if err != nil {
die("error creating directory %s: %v", target, err)
}
git, err := exec.LookPath("git")
if err != nil {
die("couldn't find git executable: %v", err)
}
err = syscall.Exec(git, []string{"git", "clone", repo, target}, os.Environ())
if err != nil {
die("failed to exec git clone: %v", err)
}
}