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

     1  package url
     2  
     3  import (
     4  	"errors"
     5  )
     6  
     7  // Parse parses a raw URL string into a URL type. It accepts information about
     8  // the URL kind (e.g. synchronization vs. forwarding) and position (i.e. the URL
     9  // is considered an alpha/source URL if first is true and a beta/destination URL
    10  // otherwise).
    11  func Parse(raw string, kind Kind, first bool) (*URL, error) {
    12  	// Ensure that the kind is supported.
    13  	if !kind.Supported() {
    14  		panic("unsupported URL kind")
    15  	}
    16  
    17  	// Don't allow empty raw URLs.
    18  	if raw == "" {
    19  		return nil, errors.New("empty URL")
    20  	}
    21  
    22  	// Dispatch URL parsing based on type. We have to be careful about the
    23  	// ordering here because URLs may be classified as multiple types (e.g. a
    24  	// Docker URL would also be classified as an SCP-style SSH URL), but we only
    25  	// want them to be parsed according to the better and more specific match.
    26  	// If we don't match anything, we assume the URL is a local path.
    27  	if isDockerURL(raw) {
    28  		return parseDocker(raw, kind, first)
    29  	} else if isSCPSSHURL(raw, kind) {
    30  		return parseSCPSSH(raw, kind)
    31  	} else {
    32  		return parseLocal(raw, kind)
    33  	}
    34  }