github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/url/forwarding/parse.go (about)

     1  package forwarding
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  )
     8  
     9  // Parse parses a forwarding sub-URL (which is stored as the Path component of
    10  // an endpoint URL) into protocol and address components.
    11  func Parse(url string) (string, string, error) {
    12  	// Ensure that the URL is non-empty.
    13  	if url == "" {
    14  		return "", "", errors.New("empty URL")
    15  	}
    16  
    17  	// Split the specification and ensure that it was correctly formatted.
    18  	components := strings.SplitN(url, ":", 2)
    19  	if len(components) != 2 {
    20  		return "", "", errors.New("incorrectly formatted URL")
    21  	}
    22  
    23  	// Ensure that the protocol is valid.
    24  	if !IsValidProtocol(components[0]) {
    25  		return "", "", fmt.Errorf("invalid protocol: %s", components[0])
    26  	}
    27  
    28  	// Ensure that the address is non-empty. There's not much other validation
    29  	// that we can do easily.
    30  	if components[1] == "" {
    31  		return "", "", errors.New("empty address")
    32  	}
    33  
    34  	// Success.
    35  	return components[0], components[1], nil
    36  }