github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/synchronization/protocols/local/protocol.go (about)

     1  package local
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/mutagen-io/mutagen/pkg/logging"
     8  	"github.com/mutagen-io/mutagen/pkg/synchronization"
     9  	"github.com/mutagen-io/mutagen/pkg/synchronization/endpoint/local"
    10  	urlpkg "github.com/mutagen-io/mutagen/pkg/url"
    11  )
    12  
    13  // protocolHandler implements the synchronization.ProtocolHandler interface for
    14  // connecting to local endpoints.
    15  type protocolHandler struct{}
    16  
    17  // Connect connects to a local endpoint.
    18  func (h *protocolHandler) Connect(
    19  	_ context.Context,
    20  	logger *logging.Logger,
    21  	url *urlpkg.URL,
    22  	prompter string,
    23  	session string,
    24  	version synchronization.Version,
    25  	configuration *synchronization.Configuration,
    26  	alpha bool,
    27  ) (synchronization.Endpoint, error) {
    28  	// Verify that the URL is of the correct kind and protocol.
    29  	if url.Kind != urlpkg.Kind_Synchronization {
    30  		panic("non-synchronization URL dispatched to synchronization protocol handler")
    31  	} else if url.Protocol != urlpkg.Protocol_Local {
    32  		panic("non-local URL dispatched to local protocol handler")
    33  	}
    34  
    35  	// Create a local endpoint.
    36  	endpoint, err := local.NewEndpoint(logger, url.Path, session, version, configuration, alpha)
    37  	if err != nil {
    38  		return nil, fmt.Errorf("unable to create local endpoint: %w", err)
    39  	}
    40  
    41  	// Success.
    42  	return endpoint, nil
    43  }
    44  
    45  func init() {
    46  	// Register the local protocol handler with the synchronization package.
    47  	synchronization.ProtocolHandlers[urlpkg.Protocol_Local] = &protocolHandler{}
    48  }