Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit bf98b60

Browse files
committed
Add client selection based on repo URL scheme
1 parent 990573b commit bf98b60

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

clients/common.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
package clients
22

33
import (
4+
"fmt"
5+
"net/url"
6+
47
"gopkg.in/src-d/go-git.v2/clients/common"
8+
"gopkg.in/src-d/go-git.v2/clients/file"
59
"gopkg.in/src-d/go-git.v2/clients/http"
10+
"gopkg.in/src-d/go-git.v2/clients/ssh"
611
)
712

8-
func NewGitUploadPackService() common.GitUploadPackService {
9-
return http.NewGitUploadPackService()
13+
// NewGitUploadPackService returns the appropiate upload pack service
14+
// among of the set of supported protocols: HTTP, SSH or file.
15+
// TODO: should this get a scheme as an argument instead of an URL?
16+
func NewGitUploadPackService(repoURL string) (common.GitUploadPackService, error) {
17+
u, err := url.Parse(repoURL)
18+
if err != nil {
19+
return nil, fmt.Errorf("invalid url %q", repoURL)
20+
}
21+
switch u.Scheme {
22+
case "http", "https":
23+
return http.NewGitUploadPackService(), nil
24+
case "ssh":
25+
return ssh.NewGitUploadPackService(), nil
26+
case "file":
27+
return file.NewGitUploadPackService(), nil
28+
default:
29+
return nil, fmt.Errorf("unsupported scheme %q", u.Scheme)
30+
}
1031
}

clients/common_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package clients
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
. "gopkg.in/check.v1"
8+
)
9+
10+
func Test(t *testing.T) { TestingT(t) }
11+
12+
type SuiteCommon struct{}
13+
14+
var _ = Suite(&SuiteCommon{})
15+
16+
func (s *SuiteCommon) TestNewGitUploadPackService(c *C) {
17+
var tests = [...]struct {
18+
input string
19+
err bool
20+
expected string
21+
}{
22+
{"ht/ml://example.com", true, "<nil>"},
23+
{"", true, "<nil>"},
24+
{"-", true, "<nil>"},
25+
{"!@", true, "<nil>"},
26+
{"badscheme://github.com/src-d/go-git", true, "<nil>"},
27+
{"http://github.com/src-d/go-git", false, "*http.GitUploadPackService"},
28+
{"https://github.com/src-d/go-git", false, "*http.GitUploadPackService"},
29+
{"ssh://github.com/src-d/go-git", false, "*ssh.GitUploadPackService"},
30+
{"file://github.com/src-d/go-git", false, "*file.GitUploadPackService"},
31+
}
32+
33+
for i, t := range tests {
34+
output, err := NewGitUploadPackService(t.input)
35+
c.Assert(err != nil, Equals, t.err, Commentf("%d) %q: wrong error value", i, t.input))
36+
c.Assert(fmt.Sprintf("%T", output), Equals, t.expected, Commentf("%d) %q: wrong type", i, t.input))
37+
}
38+
}

clients/file/git_upload_pack.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package file
2+
3+
import (
4+
"io"
5+
6+
"gopkg.in/src-d/go-git.v2/clients/common"
7+
)
8+
9+
type GitUploadPackService struct{}
10+
11+
func NewGitUploadPackService() *GitUploadPackService {
12+
return &GitUploadPackService{}
13+
}
14+
15+
func (s *GitUploadPackService) Connect(url common.Endpoint) error {
16+
return nil
17+
}
18+
19+
func (s *GitUploadPackService) ConnectWithAuth(url common.Endpoint, auth common.AuthMethod) error {
20+
return nil
21+
}
22+
23+
func (s *GitUploadPackService) Info() (*common.GitUploadPackInfo, error) {
24+
return nil, nil
25+
}
26+
27+
func (s *GitUploadPackService) Fetch(r *common.GitUploadPackRequest) (io.ReadCloser, error) {
28+
return nil, nil
29+
}

0 commit comments

Comments
 (0)