6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-08 08:44:18 +00:00

Add URL parsing function and its unit tests

This commit is contained in:
Grzegorz Dlugoszewski
2020-05-21 22:48:28 +02:00
parent 91cf26ef27
commit bb3a243829
2 changed files with 91 additions and 0 deletions

28
pkg/url.go Normal file
View File

@@ -0,0 +1,28 @@
package pkg
import (
"net/url"
"path"
"strings"
"github.com/pkg/errors"
)
// TODO: maybe use https://github.com/whilp/git-urls?
func URLToPath(rawurl string) (string, error) {
parsed, err := url.Parse(rawurl)
if err != nil {
return "", errors.Wrap(err, "Failed parsing URL")
}
repoHost := strings.Split(parsed.Host, ":")[0]
repoPath := parsed.Path
//repoPath = strings.TrimSuffix(repoPath, ".git/")
//repoPath = strings.TrimSuffix(repoPath, ".git")
localPath := path.Join(repoHost, repoPath)
localPath = strings.TrimSuffix(localPath, ".git")
return localPath, nil
}