-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (49 loc) · 1.28 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
package main
import (
"fmt"
"log"
"net/url"
"os"
"os/exec"
"strings"
"github.com/pkg/browser"
)
func parsedURL(gitRemoteURL string) (*url.URL, error) {
return url.Parse(getFormattedGitRemoteURL(gitRemoteURL))
}
func main() {
u, err := parsedURL(getGitRemoteURL())
if err != nil {
log.Fatal(err)
}
args := os.Args
browser.OpenURL(getTargetURL(args, u))
}
func getTargetURL(args []string, u *url.URL) string {
if len(args) < 2 {
return strings.TrimSuffix("https://"+u.Hostname()+u.Path, "\n")
} else {
return getCommitHashURLByHostingService(u, args)
}
}
func getCommitHashURLByHostingService(u *url.URL, args []string) string {
commitHash := args[1]
if strings.Contains(u.Hostname(), "bitbucket") {
return strings.TrimSuffix("https://"+u.Hostname()+u.Path, "\n") + "/commits/" + commitHash
}
return strings.TrimSuffix("https://"+u.Hostname()+u.Path, "\n") + "/commit/" + commitHash
}
func getFormattedGitRemoteURL(gitRemoteURL string) string {
r1 := strings.Replace(gitRemoteURL, "[email protected]:", "ssh://[email protected]/", -1)
r2 := strings.Replace(r1, ".git", "", -1)
return r2
}
func getGitRemoteURL() string {
out, err := exec.Command("git", "remote", "get-url", "origin").Output()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
url := string(out)
return url
}