Skip to content

Commit

Permalink
SourceDirSubdir is exported
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Oct 15, 2015
1 parent b513a9b commit 2463fe5
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 70 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *Client) Get() error {
// and then copy over the proper subdir.
var realDst string
dst := c.Dst
src, subDir := getDirSubdir(src)
src, subDir := SourceDirSubdir(src)
if subDir != "" {
tmpDir, err := ioutil.TempDir("", "tf")
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func Detect(src string, pwd string, ds []Detector) (string, error) {
getForce, getSrc := getForcedGetter(src)

// Separate out the subdir if there is one, we don't pass that to detect
getSrc, subDir := getDirSubdir(getSrc)
getSrc, subDir := SourceDirSubdir(getSrc)

u, err := url.Parse(getSrc)
if err == nil && u.Scheme != "" {
Expand All @@ -60,7 +60,7 @@ func Detect(src string, pwd string, ds []Detector) (string, error) {

var detectForce string
detectForce, result = getForcedGetter(result)
result, detectSubdir := getDirSubdir(result)
result, detectSubdir := SourceDirSubdir(result)

// If we have a subdir from the detection, then prepend it to our
// requested subdir.
Expand Down
32 changes: 0 additions & 32 deletions get.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"net/url"
"os/exec"
"regexp"
"strings"
"syscall"
)

Expand Down Expand Up @@ -107,37 +106,6 @@ func getRunCommand(cmd *exec.Cmd) error {
return fmt.Errorf("error running %s: %s", cmd.Path, buf.String())
}

// getDirSubdir takes a source and returns a tuple of the URL without
// the subdir and the URL with the subdir.
func getDirSubdir(src string) (string, string) {
// Calcaulate an offset to avoid accidentally marking the scheme
// as the dir.
var offset int
if idx := strings.Index(src, "://"); idx > -1 {
offset = idx + 3
}

// First see if we even have an explicit subdir
idx := strings.Index(src[offset:], "//")
if idx == -1 {
return src, ""
}

idx += offset
subdir := src[idx+2:]
src = src[:idx]

// Next, check if we have query parameters and push them onto the
// URL.
if idx = strings.Index(subdir, "?"); idx > -1 {
query := subdir[idx:]
subdir = subdir[:idx]
src += query
}

return src, subdir
}

// getForcedGetter takes a source and returns the tuple of the forced
// getter and the raw URL (without the force syntax).
func getForcedGetter(src string) (string, string) {
Expand Down
2 changes: 1 addition & 1 deletion get_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (g *HttpGetter) Get(dst string, u *url.URL) error {

// If there is a subdir component, then we download the root separately
// into a temporary directory, then copy over the proper subdir.
source, subDir := getDirSubdir(source)
source, subDir := SourceDirSubdir(source)
if subDir == "" {
return Get(dst, source)
}
Expand Down
34 changes: 0 additions & 34 deletions get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,37 +59,3 @@ func TestGet_fileSubdir(t *testing.T) {
t.Fatalf("err: %s", err)
}
}

func TestGetDirSubdir(t *testing.T) {
cases := []struct {
Input string
Dir, Sub string
}{
{
"hashicorp.com",
"hashicorp.com", "",
},
{
"hashicorp.com//foo",
"hashicorp.com", "foo",
},
{
"hashicorp.com//foo?bar=baz",
"hashicorp.com?bar=baz", "foo",
},
{
"file://foo//bar",
"file://foo", "bar",
},
}

for i, tc := range cases {
adir, asub := getDirSubdir(tc.Input)
if adir != tc.Dir {
t.Fatalf("%d: bad dir: %#v", i, adir)
}
if asub != tc.Sub {
t.Fatalf("%d: bad sub: %#v", i, asub)
}
}
}
36 changes: 36 additions & 0 deletions source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package getter

import (
"strings"
)

// SourceDirSubdir takes a source and returns a tuple of the URL without
// the subdir and the URL with the subdir.
func SourceDirSubdir(src string) (string, string) {
// Calcaulate an offset to avoid accidentally marking the scheme
// as the dir.
var offset int
if idx := strings.Index(src, "://"); idx > -1 {
offset = idx + 3
}

// First see if we even have an explicit subdir
idx := strings.Index(src[offset:], "//")
if idx == -1 {
return src, ""
}

idx += offset
subdir := src[idx+2:]
src = src[:idx]

// Next, check if we have query parameters and push them onto the
// URL.
if idx = strings.Index(subdir, "?"); idx > -1 {
query := subdir[idx:]
subdir = subdir[:idx]
src += query
}

return src, subdir
}
39 changes: 39 additions & 0 deletions source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package getter

import (
"testing"
)

func TestSourceDirSubdir(t *testing.T) {
cases := []struct {
Input string
Dir, Sub string
}{
{
"hashicorp.com",
"hashicorp.com", "",
},
{
"hashicorp.com//foo",
"hashicorp.com", "foo",
},
{
"hashicorp.com//foo?bar=baz",
"hashicorp.com?bar=baz", "foo",
},
{
"file://foo//bar",
"file://foo", "bar",
},
}

for i, tc := range cases {
adir, asub := SourceDirSubdir(tc.Input)
if adir != tc.Dir {
t.Fatalf("%d: bad dir: %#v", i, adir)
}
if asub != tc.Sub {
t.Fatalf("%d: bad sub: %#v", i, asub)
}
}
}

0 comments on commit 2463fe5

Please sign in to comment.