mirror of
https://github.com/grdl/git-get.git
synced 2026-02-07 12:58:26 +00:00
Fix cyclop issues in url.go
This commit is contained in:
45
pkg/url.go
45
pkg/url.go
@@ -19,26 +19,41 @@ var scpSyntax = regexp.MustCompile(`^([a-zA-Z0-9_]+)@([a-zA-Z0-9._-]+):(.*)$`)
|
|||||||
// When the parsed URL has an empty host, use the defaultHost.
|
// When the parsed URL has an empty host, use the defaultHost.
|
||||||
// When the parsed URL has an empty scheme, use the defaultScheme.
|
// When the parsed URL has an empty scheme, use the defaultScheme.
|
||||||
func ParseURL(rawURL string, defaultHost string, defaultScheme string) (url *urlpkg.URL, err error) {
|
func ParseURL(rawURL string, defaultHost string, defaultScheme string) (url *urlpkg.URL, err error) {
|
||||||
// If rawURL matches the SCP-like syntax, convert it into a standard ssh Path.
|
url, err = parseRawURL(rawURL)
|
||||||
// eg, git@github.com:user/repo => ssh://git@github.com/user/repo
|
if err != nil {
|
||||||
if m := scpSyntax.FindStringSubmatch(rawURL); m != nil {
|
return nil, err
|
||||||
url = &urlpkg.URL{
|
|
||||||
Scheme: "ssh",
|
|
||||||
User: urlpkg.User(m[1]),
|
|
||||||
Host: m[2],
|
|
||||||
Path: path.Join("/", m[3]),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
url, err = urlpkg.Parse(rawURL)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed parsing URL %s: %w", rawURL, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if url.Host == "" && url.Path == "" {
|
if url.Host == "" && url.Path == "" {
|
||||||
return nil, errEmptyURLPath
|
return nil, errEmptyURLPath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
normalizeURL(url, defaultHost, defaultScheme)
|
||||||
|
return url, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseRawURL handles the initial parsing of the raw URL string.
|
||||||
|
func parseRawURL(rawURL string) (*urlpkg.URL, error) {
|
||||||
|
// If rawURL matches the SCP-like syntax, convert it into a standard ssh Path.
|
||||||
|
// eg, git@github.com:user/repo => ssh://git@github.com/user/repo
|
||||||
|
if m := scpSyntax.FindStringSubmatch(rawURL); m != nil {
|
||||||
|
return &urlpkg.URL{
|
||||||
|
Scheme: "ssh",
|
||||||
|
User: urlpkg.User(m[1]),
|
||||||
|
Host: m[2],
|
||||||
|
Path: path.Join("/", m[3]),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
url, err := urlpkg.Parse(rawURL)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed parsing URL %s: %w", rawURL, err)
|
||||||
|
}
|
||||||
|
return url, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// normalizeURL applies all the normalization rules to the parsed URL.
|
||||||
|
func normalizeURL(url *urlpkg.URL, defaultHost string, defaultScheme string) {
|
||||||
if url.Scheme == "git+ssh" {
|
if url.Scheme == "git+ssh" {
|
||||||
url.Scheme = "ssh"
|
url.Scheme = "ssh"
|
||||||
}
|
}
|
||||||
@@ -65,8 +80,6 @@ func ParseURL(rawURL string, defaultHost string, defaultScheme string) (url *url
|
|||||||
url.Path = path.Join(url.Host, url.Path)
|
url.Path = path.Join(url.Host, url.Path)
|
||||||
url.Host = ""
|
url.Host = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
return url, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// URLToPath cleans up the URL and converts it into a path string.
|
// URLToPath cleans up the URL and converts it into a path string.
|
||||||
|
|||||||
Reference in New Issue
Block a user