-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.go
61 lines (49 loc) · 1.09 KB
/
git.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
package main
import (
"errors"
"regexp"
)
// protocols supported by git
const (
ProtocolSSH = "ssh"
ProtocolGit = "git"
ProtocolHTTP = "http"
ProtocolHTTPS = "https"
ProtocolFTP = "ftp"
ProtocolFTPS = "ftps"
ProtocolFile = "file"
)
var (
regexpGit = regexp.MustCompile(`^git:`)
regexpHTTP = regexp.MustCompile(`^http:`)
regexpHTTPS = regexp.MustCompile(`^https:`)
regexpFTP = regexp.MustCompile(`^ftp:`)
regexpFTPS = regexp.MustCompile(`^ftps:`)
regexpSSH = regexp.MustCompile(`^(ssh|git):|^([^@]*@)?[^./]+(\.[^./]+)+(\/[^/]*)+`)
regexpFILE = regexp.MustCompile(`^file:|(\.)?(\/[^/]+)+`)
)
func getProtocol(s string) (string, error) {
b := []byte(s)
if regexpGit.Match(b) {
return ProtocolGit, nil
}
if regexpHTTP.Match(b) {
return ProtocolHTTP, nil
}
if regexpHTTPS.Match(b) {
return ProtocolHTTPS, nil
}
if regexpFTP.Match(b) {
return ProtocolFTP, nil
}
if regexpFTPS.Match(b) {
return ProtocolFTPS, nil
}
if regexpSSH.Match(b) {
return ProtocolSSH, nil
}
if regexpFILE.Match(b) {
return ProtocolFile, nil
}
return "", errors.New("error")
}