-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
127 lines (109 loc) · 2.36 KB
/
util.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"bytes"
"fmt"
"io"
"net/url"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
)
func projectFromURL(path string) string {
if strings.HasSuffix(path, ".git") {
path = path[:len(path)-4]
}
if !strings.HasPrefix(path, "http://") &&
!strings.HasPrefix(path, "https://") &&
!strings.HasPrefix(path, "git://") {
path = "http://" + path
}
if index := strings.LastIndex(path, ":"); index != strings.Index(path, "://") && index > 0 {
path = replaceAt(path, index, "/")
}
u, _ := url.Parse(path)
return u.Host + u.Path
}
func pkgFromPath(path string) string {
index := strings.LastIndex(path, "/")
return path[index+1:]
}
func getAbsPath(path string) string {
if strings.HasPrefix(path, "~") {
usr, _ := user.Current()
path = strings.Replace(path, "~", usr.HomeDir, 1)
}
abspath, _ := filepath.Abs(path)
return abspath
}
func replaceAt(s string, at int, with string) string {
return s[:at] + with + s[at+1:]
}
func pipeFromExec(dst io.Writer, cmd string, args ...string) error {
c := exec.Command(cmd, args...)
stdout, err := c.StdoutPipe()
if err != nil {
return err
}
go io.Copy(os.Stdout, stdout)
err = c.Start()
if err != nil {
return err
}
err = c.Wait()
if err != nil {
return err
}
return nil
}
func concat(strs ...string) string {
var final string
for _, str := range strs {
final += str
}
return final
}
func concatWithSpace(strs ...string) string {
var final string
for i, str := range strs {
if i > 0 {
final += " " + str
} else {
final += str
}
}
return final
}
func getPathType(path string) repoType {
if strings.HasPrefix(path, "http://") {
return HTTP
} else if strings.HasPrefix(path, "https://") {
return HTTPS
} else if strings.HasPrefix(path, "git://") {
return GIT
} else if strings.Contains(path, "@") {
return SSH
} else {
return GOPKG
}
}
func getSSHPath(path, user string) string {
pkgpath := projectFromURL(path)
pkgpath = user + "@" + pkgpath
if strings.Count(pkgpath, "/") > 2 {
pkgpath = pkgpath[:strings.LastIndex(pkgpath, "/")]
}
pkgpath = replaceAt(pkgpath, strings.Index(pkgpath, "/"), ":")
pkgpath += ".git"
return pkgpath
}
func parseGitOriginURL(line string) (string, error) {
buf := bytes.NewBuffer([]byte(line))
var giturl string
_, err := fmt.Fscanf(buf, "origin %v (push)", &giturl)
if err != nil {
return "", err
}
return giturl, nil
}