config/module: support GitHub paths without //

This commit is contained in:
Mitchell Hashimoto 2014-09-26 15:30:36 -07:00
parent 21d90dcf4f
commit 4fbd5abc63
3 changed files with 25 additions and 1 deletions

View File

@ -25,7 +25,13 @@ func (d *GitHubDetector) Detect(src, _ string) (string, bool, error) {
}
func (d *GitHubDetector) detectHTTP(src string) (string, bool, error) {
urlStr := fmt.Sprintf("https://%s", src)
parts := strings.Split(src, "/")
if len(parts) < 3 {
return "", false, fmt.Errorf(
"GitHub URLs should be github.com/username/repo")
}
urlStr := fmt.Sprintf("https://%s", strings.Join(parts[:3], "/"))
url, err := url.Parse(urlStr)
if err != nil {
return "", true, fmt.Errorf("error parsing GitHub URL: %s", err)
@ -35,6 +41,10 @@ func (d *GitHubDetector) detectHTTP(src string) (string, bool, error) {
url.Path += ".git"
}
if len(parts) > 3 {
url.Path += "//" + strings.Join(parts[3:], "/")
}
return "git::" + url.String(), true, nil
}

View File

@ -12,6 +12,10 @@ func TestGitHubDetector(t *testing.T) {
// HTTP
{"github.com/hashicorp/foo", "git::https://github.com/hashicorp/foo.git"},
{"github.com/hashicorp/foo.git", "git::https://github.com/hashicorp/foo.git"},
{
"github.com/hashicorp/foo/bar",
"git::https://github.com/hashicorp/foo.git//bar",
},
{
"github.com/hashicorp/foo?foo=bar",
"git::https://github.com/hashicorp/foo.git?foo=bar",
@ -23,6 +27,10 @@ func TestGitHubDetector(t *testing.T) {
// SSH
{"git@github.com:hashicorp/foo.git", "git::ssh://git@github.com/hashicorp/foo.git"},
{
"git@github.com:hashicorp/foo.git//bar",
"git::ssh://git@github.com/hashicorp/foo.git//bar",
},
{
"git@github.com:hashicorp/foo.git?foo=bar",
"git::ssh://git@github.com/hashicorp/foo.git?foo=bar",

View File

@ -146,6 +146,12 @@ func (t *Tree) Load(s Storage, mode GetMode) error {
return fmt.Errorf("module %s: %s", m.Name, err)
}
// Check if the detector introduced something new.
source, subDir2 := getDirSubdir(source)
if subDir2 != "" {
subDir = filepath.Join(subDir2, subDir)
}
// Get the directory where this module is so we can load it
dir, ok, err := getStorage(s, source, mode)
if err != nil {