aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/transport/client
diff options
context:
space:
mode:
Diffstat (limited to 'plumbing/transport/client')
-rw-r--r--plumbing/transport/client/client.go32
-rw-r--r--plumbing/transport/client/client_test.go73
-rw-r--r--plumbing/transport/client/example_test.go21
3 files changed, 126 insertions, 0 deletions
diff --git a/plumbing/transport/client/client.go b/plumbing/transport/client/client.go
new file mode 100644
index 0000000..5c6da05
--- /dev/null
+++ b/plumbing/transport/client/client.go
@@ -0,0 +1,32 @@
+package client
+
+import (
+ "fmt"
+
+ "gopkg.in/src-d/go-git.v4/plumbing/transport"
+ "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
+ "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
+)
+
+// Protocols are the protocols supported by default.
+var Protocols = map[string]transport.Client{
+ "http": http.DefaultClient,
+ "https": http.DefaultClient,
+ "ssh": ssh.DefaultClient,
+}
+
+// InstallProtocol adds or modifies an existing protocol.
+func InstallProtocol(scheme string, c transport.Client) {
+ Protocols[scheme] = c
+}
+
+// NewClient returns the appropriate client among of the set of known protocols:
+// HTTP, SSH. See `InstallProtocol` to add or modify protocols.
+func NewClient(endpoint transport.Endpoint) (transport.Client, error) {
+ f, ok := Protocols[endpoint.Scheme]
+ if !ok {
+ return nil, fmt.Errorf("unsupported scheme %q", endpoint.Scheme)
+ }
+
+ return f, nil
+}
diff --git a/plumbing/transport/client/client_test.go b/plumbing/transport/client/client_test.go
new file mode 100644
index 0000000..90bad57
--- /dev/null
+++ b/plumbing/transport/client/client_test.go
@@ -0,0 +1,73 @@
+package client
+
+import (
+ "fmt"
+ "net/http"
+ "testing"
+
+ "gopkg.in/src-d/go-git.v4/plumbing/transport"
+
+ . "gopkg.in/check.v1"
+)
+
+func Test(t *testing.T) { TestingT(t) }
+
+type ClientSuite struct{}
+
+var _ = Suite(&ClientSuite{})
+
+func (s *ClientSuite) TestNewClientHTTP(c *C) {
+ e, err := transport.NewEndpoint("http://github.com/src-d/go-git")
+ c.Assert(err, IsNil)
+
+ output, err := NewClient(e)
+ c.Assert(err, IsNil)
+ c.Assert(typeAsString(output), Equals, "*http.Client")
+
+ e, err = transport.NewEndpoint("https://github.com/src-d/go-git")
+ c.Assert(err, IsNil)
+
+ output, err = NewClient(e)
+ c.Assert(err, IsNil)
+ c.Assert(typeAsString(output), Equals, "*http.Client")
+}
+
+func (s *ClientSuite) TestNewClientSSH(c *C) {
+ e, err := transport.NewEndpoint("ssh://github.com/src-d/go-git")
+ c.Assert(err, IsNil)
+
+ output, err := NewClient(e)
+ c.Assert(err, IsNil)
+ c.Assert(typeAsString(output), Equals, "*ssh.Client")
+}
+
+func (s *ClientSuite) TestNewClientUnknown(c *C) {
+ e, err := transport.NewEndpoint("unknown://github.com/src-d/go-git")
+ c.Assert(err, IsNil)
+
+ _, err = NewClient(e)
+ c.Assert(err, NotNil)
+}
+
+func (s *ClientSuite) TestInstallProtocol(c *C) {
+ InstallProtocol("newscheme", &dummyClient{})
+ c.Assert(Protocols["newscheme"], NotNil)
+}
+
+type dummyClient struct {
+ *http.Client
+}
+
+func (*dummyClient) NewFetchPackSession(transport.Endpoint) (
+ transport.FetchPackSession, error) {
+ return nil, nil
+}
+
+func (*dummyClient) NewSendPackSession(transport.Endpoint) (
+ transport.SendPackSession, error) {
+ return nil, nil
+}
+
+func typeAsString(v interface{}) string {
+ return fmt.Sprintf("%T", v)
+}
diff --git a/plumbing/transport/client/example_test.go b/plumbing/transport/client/example_test.go
new file mode 100644
index 0000000..3e7a4f0
--- /dev/null
+++ b/plumbing/transport/client/example_test.go
@@ -0,0 +1,21 @@
+package client_test
+
+import (
+ "crypto/tls"
+ "net/http"
+
+ "gopkg.in/src-d/go-git.v4/plumbing/transport/client"
+ githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
+)
+
+func ExampleInstallProtocol() {
+ // Create custom net/http client that.
+ httpClient := &http.Client{
+ Transport: &http.Transport{
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
+ },
+ }
+
+ // Install it as default client for https URLs.
+ client.InstallProtocol("https", githttp.NewClient(httpClient))
+}