From a8fd3fd6c8970bbf2d21e97c33fbb131f2fd4167 Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Tue, 16 Aug 2016 01:00:13 +0200 Subject: clients/common: Endpoint SCP like support --- clients/common/common.go | 17 +++++++++++++++++ clients/common/common_test.go | 6 ++++++ 2 files changed, 23 insertions(+) diff --git a/clients/common/common.go b/clients/common/common.go index c8dc7de..9099016 100644 --- a/clients/common/common.go +++ b/clients/common/common.go @@ -7,6 +7,7 @@ import ( "io" "io/ioutil" "net/url" + "regexp" "strings" "gopkg.in/src-d/go-git.v4/core" @@ -36,7 +37,14 @@ type AuthMethod interface { type Endpoint url.URL +var ( + isSchemeRegExp = regexp.MustCompile("^[^:]+://") + scpLikeUrlRegExp = regexp.MustCompile("^(?P[^@]+@)?(?P[^:]+):/?(?P.+)$") +) + func NewEndpoint(endpoint string) (Endpoint, error) { + endpoint = transformSCPLikeIfNeeded(endpoint) + u, err := url.Parse(endpoint) if err != nil { return Endpoint{}, core.NewPermanentError(err) @@ -51,6 +59,15 @@ func NewEndpoint(endpoint string) (Endpoint, error) { return Endpoint(*u), nil } +func transformSCPLikeIfNeeded(endpoint string) string { + if !isSchemeRegExp.MatchString(endpoint) && scpLikeUrlRegExp.MatchString(endpoint) { + m := scpLikeUrlRegExp.FindStringSubmatch(endpoint) + return fmt.Sprintf("ssh://%s%s/%s", m[1], m[2], m[3]) + } + + return endpoint +} + func (e *Endpoint) String() string { u := url.URL(*e) return u.String() diff --git a/clients/common/common_test.go b/clients/common/common_test.go index 80934fc..d2d3696 100644 --- a/clients/common/common_test.go +++ b/clients/common/common_test.go @@ -22,6 +22,12 @@ func (s *SuiteCommon) TestNewEndpoint(c *C) { c.Assert(e.String(), Equals, "ssh://git@github.com/user/repository.git") } +func (s *SuiteCommon) TestNewEndpointSCPLike(c *C) { + e, err := NewEndpoint("git@github.com:user/repository.git") + c.Assert(err, IsNil) + c.Assert(e.String(), Equals, "ssh://git@github.com/user/repository.git") +} + func (s *SuiteCommon) TestNewEndpointWrongForgat(c *C) { e, err := NewEndpoint("foo") c.Assert(err, Not(IsNil)) -- cgit