9 // GitHubDetector implements Detector to detect GitHub URLs and turn
10 // them into URLs that the Git Getter can understand.
11 type GitHubDetector struct{}
13 func (d *GitHubDetector) Detect(src, _ string) (string, bool, error) {
18 if strings.HasPrefix(src, "github.com/") {
19 return d.detectHTTP(src)
20 } else if strings.HasPrefix(src, "git@github.com:") {
21 return d.detectSSH(src)
27 func (d *GitHubDetector) detectHTTP(src string) (string, bool, error) {
28 parts := strings.Split(src, "/")
30 return "", false, fmt.Errorf(
31 "GitHub URLs should be github.com/username/repo")
34 urlStr := fmt.Sprintf("https://%s", strings.Join(parts[:3], "/"))
35 url, err := url.Parse(urlStr)
37 return "", true, fmt.Errorf("error parsing GitHub URL: %s", err)
40 if !strings.HasSuffix(url.Path, ".git") {
45 url.Path += "//" + strings.Join(parts[3:], "/")
48 return "git::" + url.String(), true, nil
51 func (d *GitHubDetector) detectSSH(src string) (string, bool, error) {
52 idx := strings.Index(src, ":")
53 qidx := strings.Index(src, "?")
60 u.User = url.User("git")
62 u.Path = src[idx+1 : qidx]
64 q, err := url.ParseQuery(src[qidx+1:])
66 return "", true, fmt.Errorf("error parsing GitHub SSH URL: %s", err)
69 u.RawQuery = q.Encode()
72 return "git::" + u.String(), true, nil