-
Notifications
You must be signed in to change notification settings - Fork 0
/
autodetect.go
48 lines (39 loc) · 1.04 KB
/
autodetect.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
package main
import (
"log"
"os"
"strings"
git "gopkg.in/src-d/go-git.v4"
)
//GetRemoteDetails interprets the remote url and parses to guess a user and repository name
func GetRemoteDetails() (string, string, string) {
cwd, err := os.Getwd()
repo, err := git.PlainOpen(cwd)
if err != nil {
log.Fatal(err)
}
remote, err := repo.Remote("origin")
if err != nil {
log.Fatal(err)
} else if remote == nil {
return "", "", ""
}
url := remote.Config().URLs[0]
url = strings.Replace(url, ".git", "", -1)
url = strings.Replace(url, "https://", "", -1)
var service string
if strings.Contains(url, "gitlab") {
service = "gitlab"
} else if strings.Contains(url, "github") {
service = "github"
}
repoName := url[strings.LastIndex(url, "/"):]
repoName = strings.Trim(repoName, "/")
var userName string
if strings.Contains(url, "@") {
userName = url[strings.Index(url, ":")+1 : strings.LastIndex(url, "/")]
} else {
userName = url[strings.Index(url, "/")+1 : strings.LastIndex(url, "/")]
}
return userName, repoName, service
}