From a65bcbc63bef24cf219c63d1b8cfb309c95d1c0f Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Sat, 13 Aug 2016 20:07:21 +0200 Subject: clients: new Endpoint implementation and InstallProtocol function --- clients/common.go | 42 ++++++++++++------------------------------ 1 file changed, 12 insertions(+), 30 deletions(-) (limited to 'clients/common.go') diff --git a/clients/common.go b/clients/common.go index 8d45a34..1eeacf0 100644 --- a/clients/common.go +++ b/clients/common.go @@ -13,52 +13,34 @@ package clients import ( "fmt" - "net/url" "gopkg.in/src-d/go-git.v4/clients/common" "gopkg.in/src-d/go-git.v4/clients/http" "gopkg.in/src-d/go-git.v4/clients/ssh" ) -// DefaultProtocols are the protocols supported by default. -var DefaultProtocols = map[string]common.GitUploadPackService{ - "http": http.NewGitUploadPackService(), - "https": http.NewGitUploadPackService(), - "ssh": ssh.NewGitUploadPackService(), -} - -// KnownProtocols holds the current set of known protocols. Initially -// it gets its contents from `DefaultProtocols`. See `InstallProtocol` -// below to add or modify this variable. -var KnownProtocols = make(map[string]common.GitUploadPackService, len(DefaultProtocols)) +type GitUploadPackServiceFactory func(common.Endpoint) common.GitUploadPackService -func init() { - for k, v := range DefaultProtocols { - InstallProtocol(k, v) - } +// Protocols are the protocols supported by default. +var Protocols = map[string]GitUploadPackServiceFactory{ + "http": http.NewGitUploadPackService, + "https": http.NewGitUploadPackService, + "ssh": ssh.NewGitUploadPackService, } // InstallProtocol adds or modifies an existing protocol. -func InstallProtocol(scheme string, service common.GitUploadPackService) { - if service == nil { - panic("nil service") - } - - KnownProtocols[scheme] = service +func InstallProtocol(scheme string, f GitUploadPackServiceFactory) { + Protocols[scheme] = f } // NewGitUploadPackService returns the appropriate upload pack service // among of the set of known protocols: HTTP, SSH. See `InstallProtocol` // to add or modify protocols. -func NewGitUploadPackService(repoURL string) (common.GitUploadPackService, error) { - u, err := url.Parse(repoURL) - if err != nil { - return nil, fmt.Errorf("invalid url %q", repoURL) - } - s, ok := KnownProtocols[u.Scheme] +func NewGitUploadPackService(endpoint common.Endpoint) (common.GitUploadPackService, error) { + f, ok := Protocols[endpoint.Scheme] if !ok { - return nil, fmt.Errorf("unsupported scheme %q", u.Scheme) + return nil, fmt.Errorf("unsupported scheme %q", endpoint.Scheme) } - return s, nil + return f(endpoint), nil } -- cgit