config/module: detect supports subdirs

This commit is contained in:
Mitchell Hashimoto 2014-09-27 09:29:12 -07:00
parent e041a52dab
commit 0a2d06268c
2 changed files with 41 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package module
import ( import (
"fmt" "fmt"
"net/url" "net/url"
"path/filepath"
) )
// Detector defines the interface that an invalid URL or a URL with a blank // Detector defines the interface that an invalid URL or a URL with a blank
@ -34,6 +35,9 @@ func init() {
func Detect(src string, pwd string) (string, error) { func Detect(src string, pwd string) (string, error) {
getForce, getSrc := getForcedGetter(src) getForce, getSrc := getForcedGetter(src)
// Separate out the subdir if there is one, we don't pass that to detect
getSrc, subDir := getDirSubdir(getSrc)
u, err := url.Parse(getSrc) u, err := url.Parse(getSrc)
if err == nil && u.Scheme != "" { if err == nil && u.Scheme != "" {
// Valid URL // Valid URL
@ -51,6 +55,25 @@ func Detect(src string, pwd string) (string, error) {
var detectForce string var detectForce string
detectForce, result = getForcedGetter(result) detectForce, result = getForcedGetter(result)
result, detectSubdir := getDirSubdir(result)
// If we have a subdir from the detection, then prepend it to our
// requested subdir.
if detectSubdir != "" {
if subDir != "" {
subDir = filepath.Join(detectSubdir, subDir)
} else {
subDir = detectSubdir
}
}
if subDir != "" {
u, err := url.Parse(result)
if err != nil {
return "", fmt.Errorf("Error parsing URL: %s", err)
}
u.Path += "//" + subDir
result = u.String()
}
// Preserve the forced getter if it exists. We try to use the // Preserve the forced getter if it exists. We try to use the
// original set force first, followed by any force set by the // original set force first, followed by any force set by the

View File

@ -13,7 +13,24 @@ func TestDetect(t *testing.T) {
}{ }{
{"./foo", "/foo", "file:///foo/foo", false}, {"./foo", "/foo", "file:///foo/foo", false},
{"git::./foo", "/foo", "git::file:///foo/foo", false}, {"git::./foo", "/foo", "git::file:///foo/foo", false},
{"git::github.com/hashicorp/foo", "", "git::https://github.com/hashicorp/foo.git", false}, {
"git::github.com/hashicorp/foo",
"",
"git::https://github.com/hashicorp/foo.git",
false,
},
{
"./foo//bar",
"/foo",
"file:///foo/foo//bar",
false,
},
{
"git::github.com/hashicorp/foo//bar",
"",
"git::https://github.com/hashicorp/foo.git//bar",
false,
},
} }
for i, tc := range cases { for i, tc := range cases {