Debian Codegraph @

plumbing/transport/client/client.go

raw
1 // Package client contains helper function to deal with the different client
2 // protocols.
3 package client
4
5 import (
6 "fmt"
7
8 "github.com/go-git/go-git/v5/plumbing/transport"
9 "github.com/go-git/go-git/v5/plumbing/transport/file"
10 "github.com/go-git/go-git/v5/plumbing/transport/git"
11 "github.com/go-git/go-git/v5/plumbing/transport/http"
12 "github.com/go-git/go-git/v5/plumbing/transport/ssh"
13 )
14
15 // Protocols are the protocols supported by default.
16 var Protocols = map[string]transport.Transport{
17 "http": http.DefaultClient,
18 "https": http.DefaultClient,
19 "ssh": ssh.DefaultClient,
20 "git": git.DefaultClient,
21 "file": file.DefaultClient,
22 }
23
24 // InstallProtocol adds or modifies an existing protocol.
25 func InstallProtocol(scheme string, c transport.Transport) {
26 if c == nil {
27 delete(Protocols, scheme)
28 return
29 }
30
31 Protocols[scheme] = c
32 }
33
34 // NewClient returns the appropriate client among of the set of known protocols:
35 // http://, https://, ssh:// and file://.
36 // See `InstallProtocol` to add or modify protocols.
37 func NewClient(endpoint *transport.Endpoint) (transport.Transport, error) {
38 return getTransport(endpoint)
39 }
40
41 func getTransport(endpoint *transport.Endpoint) (transport.Transport, error) {
42 f, ok := Protocols[endpoint.Protocol]
43 if !ok {
44 return nil, fmt.Errorf("unsupported scheme %q", endpoint.Protocol)
45 }
46
47 if f == nil {
48 return nil, fmt.Errorf("malformed client for scheme %q, client is defined as nil", endpoint.Protocol)
49 }
50 return f, nil
51 }