-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
58 lines (49 loc) · 1.47 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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"strings"
)
//var log = logging.MustGetLogger("git-grab")
func main() {
home := os.Getenv("HOME")
usage := `Usage:
git grab <repository>`
args := os.Args[1:] // get cmdline args without program
if len(args) < 1 {
fmt.Println(usage)
os.Exit(1)
}
repoPath := ParseRepo(args[0])
var cloneDir string = fmt.Sprintf("%s/Projects/%s", home, repoPath)
cmd := exec.Command("git", "clone", args[0], cloneDir)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}
// Parses the git repository path and determines the folder location on
// local file system
func ParseRepo(repo string) string {
var domain string = ""
var owner string = ""
var repoName string = ""
var noProtocol = strings.Split(repo, "://")
var withoutGit = strings.Split(noProtocol[len(noProtocol)-1], "@")
var basicRepo string = withoutGit[len(withoutGit)-1]
basicRepo = strings.TrimSuffix(basicRepo, ".git")
var ownerRepoSplit = strings.Split(basicRepo, "/")
var tokens = strings.Split(basicRepo, ".")
domain = tokens[len(tokens)-2]
owner = ownerRepoSplit[len(ownerRepoSplit)-2]
var colonSplit = strings.Split(owner, ":")
owner = colonSplit[len(colonSplit)-1]
owner = strings.Replace(owner, "~", "users/", 1) // for Stash, adding a subpath for user owned repos
owner = strings.ToLower(owner)
repoName = ownerRepoSplit[len(ownerRepoSplit)-1]
return fmt.Sprintf("%s/%s/%s", domain, owner, repoName)
}