github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/synchronization/connect.go (about) 1 package synchronization 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/mutagen-io/mutagen/pkg/logging" 8 urlpkg "github.com/mutagen-io/mutagen/pkg/url" 9 ) 10 11 // ProtocolHandler defines the interface that protocol handlers must support in 12 // order to connect to endpoints. 13 type ProtocolHandler interface { 14 // Connect connects to an endpoint using the connection parameters in the 15 // provided URL and the specified prompter (if any). It then initializes the 16 // endpoint using the specified parameters. 17 Connect( 18 ctx context.Context, 19 logger *logging.Logger, 20 url *urlpkg.URL, 21 prompter string, 22 session string, 23 version Version, 24 configuration *Configuration, 25 alpha bool, 26 ) (Endpoint, error) 27 } 28 29 // ProtocolHandlers is a map of registered protocol handlers. It should only be 30 // modified during init() operations. 31 var ProtocolHandlers = map[urlpkg.Protocol]ProtocolHandler{} 32 33 // connect attempts to establish a connection to an endpoint. 34 func connect( 35 ctx context.Context, 36 logger *logging.Logger, 37 url *urlpkg.URL, 38 prompter string, 39 session string, 40 version Version, 41 configuration *Configuration, 42 alpha bool, 43 ) (Endpoint, error) { 44 // Local the appropriate protocol handler. 45 handler, ok := ProtocolHandlers[url.Protocol] 46 if !ok { 47 return nil, fmt.Errorf("unknown protocol: %s", url.Protocol) 48 } else if handler == nil { 49 panic("nil protocol handler registered") 50 } 51 52 // Dispatch the dialing. 53 endpoint, err := handler.Connect(ctx, logger, url, prompter, session, version, configuration, alpha) 54 if err != nil { 55 return nil, fmt.Errorf("unable to connect to endpoint: %w", err) 56 } 57 58 // Success. 59 return endpoint, nil 60 }