github.com/microsoft/fabrikate@v1.0.0-alpha.1.0.20210115014322-dc09194d0885/internal/url/core.go (about) 1 package url 2 3 import ( 4 "net/url" 5 "path" 6 "strings" 7 ) 8 9 // ToPath converts a url to a path like string. 10 func ToPath(u string) (string, error) { 11 noProtocol, err := removeProtocol(u) 12 if err != nil { 13 return "", err 14 } 15 16 var pathSegments []string 17 for _, v := range strings.Split(noProtocol, "/") { 18 if v != "" { 19 pathSegments = append(pathSegments, v) 20 } 21 } 22 23 return path.Join(pathSegments...), nil 24 } 25 26 func removeProtocol(repoURL string) (string, error) { 27 // Return the original URL if it does not start with a protocol 28 if !strings.Contains(repoURL, "://") { 29 return repoURL, nil 30 } 31 32 // Parse the URL, remove the Scheme and leading "/" 33 u, err := url.Parse(repoURL) 34 if err != nil { 35 return "", err 36 } 37 u.Scheme = "" 38 39 return strings.TrimLeft(u.String(), "/"), nil 40 }