forked from hashicorp/go-getter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
79 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |