aboutsummaryrefslogtreecommitdiffstats
path: root/clients
diff options
context:
space:
mode:
authorAlberto Cortés <alberto@sourced.tech>2015-12-16 19:41:29 +0100
committerAlberto Cortés <alberto@sourced.tech>2015-12-16 19:41:29 +0100
commitbf98b6096fd1e813ebadbb1f71d4b2d4e48bdb4b (patch)
tree662e91b988a5bf79fe9c6d1a02a2df91d4596d27 /clients
parent990573b9bf06d12fdafbe09664e47736a48746d8 (diff)
downloadgo-git-bf98b6096fd1e813ebadbb1f71d4b2d4e48bdb4b.tar.gz
Add client selection based on repo URL scheme
Diffstat (limited to 'clients')
-rw-r--r--clients/common.go25
-rw-r--r--clients/common_test.go38
-rw-r--r--clients/file/git_upload_pack.go29
3 files changed, 90 insertions, 2 deletions
diff --git a/clients/common.go b/clients/common.go
index fcbacf5..b2e906d 100644
--- a/clients/common.go
+++ b/clients/common.go
@@ -1,10 +1,31 @@
package clients
import (
+ "fmt"
+ "net/url"
+
"gopkg.in/src-d/go-git.v2/clients/common"
+ "gopkg.in/src-d/go-git.v2/clients/file"
"gopkg.in/src-d/go-git.v2/clients/http"
+ "gopkg.in/src-d/go-git.v2/clients/ssh"
)
-func NewGitUploadPackService() common.GitUploadPackService {
- return http.NewGitUploadPackService()
+// NewGitUploadPackService returns the appropiate upload pack service
+// among of the set of supported protocols: HTTP, SSH or file.
+// TODO: should this get a scheme as an argument instead of an URL?
+func NewGitUploadPackService(repoURL string) (common.GitUploadPackService, error) {
+ u, err := url.Parse(repoURL)
+ if err != nil {
+ return nil, fmt.Errorf("invalid url %q", repoURL)
+ }
+ switch u.Scheme {
+ case "http", "https":
+ return http.NewGitUploadPackService(), nil
+ case "ssh":
+ return ssh.NewGitUploadPackService(), nil
+ case "file":
+ return file.NewGitUploadPackService(), nil
+ default:
+ return nil, fmt.Errorf("unsupported scheme %q", u.Scheme)
+ }
}
diff --git a/clients/common_test.go b/clients/common_test.go
new file mode 100644
index 0000000..f8bcbd3
--- /dev/null
+++ b/clients/common_test.go
@@ -0,0 +1,38 @@
+package clients
+
+import (
+ "fmt"
+ "testing"
+
+ . "gopkg.in/check.v1"
+)
+
+func Test(t *testing.T) { TestingT(t) }
+
+type SuiteCommon struct{}
+
+var _ = Suite(&SuiteCommon{})
+
+func (s *SuiteCommon) TestNewGitUploadPackService(c *C) {
+ var tests = [...]struct {
+ input string
+ err bool
+ expected string
+ }{
+ {"ht/ml://example.com", true, "<nil>"},
+ {"", true, "<nil>"},
+ {"-", true, "<nil>"},
+ {"!@", true, "<nil>"},
+ {"badscheme://github.com/src-d/go-git", true, "<nil>"},
+ {"http://github.com/src-d/go-git", false, "*http.GitUploadPackService"},
+ {"https://github.com/src-d/go-git", false, "*http.GitUploadPackService"},
+ {"ssh://github.com/src-d/go-git", false, "*ssh.GitUploadPackService"},
+ {"file://github.com/src-d/go-git", false, "*file.GitUploadPackService"},
+ }
+
+ for i, t := range tests {
+ output, err := NewGitUploadPackService(t.input)
+ c.Assert(err != nil, Equals, t.err, Commentf("%d) %q: wrong error value", i, t.input))
+ c.Assert(fmt.Sprintf("%T", output), Equals, t.expected, Commentf("%d) %q: wrong type", i, t.input))
+ }
+}
diff --git a/clients/file/git_upload_pack.go b/clients/file/git_upload_pack.go
new file mode 100644
index 0000000..34ee958
--- /dev/null
+++ b/clients/file/git_upload_pack.go
@@ -0,0 +1,29 @@
+package file
+
+import (
+ "io"
+
+ "gopkg.in/src-d/go-git.v2/clients/common"
+)
+
+type GitUploadPackService struct{}
+
+func NewGitUploadPackService() *GitUploadPackService {
+ return &GitUploadPackService{}
+}
+
+func (s *GitUploadPackService) Connect(url common.Endpoint) error {
+ return nil
+}
+
+func (s *GitUploadPackService) ConnectWithAuth(url common.Endpoint, auth common.AuthMethod) error {
+ return nil
+}
+
+func (s *GitUploadPackService) Info() (*common.GitUploadPackInfo, error) {
+ return nil, nil
+}
+
+func (s *GitUploadPackService) Fetch(r *common.GitUploadPackRequest) (io.ReadCloser, error) {
+ return nil, nil
+}