config/module: support forced getters with TYPE::URL syntax
This commit is contained in:
parent
cf4885d2fd
commit
acb6d12a75
|
@ -30,20 +30,29 @@ func init() {
|
|||
// This is safe to be called with an already valid source string: Detect
|
||||
// will just return it.
|
||||
func Detect(src string, pwd string) (string, error) {
|
||||
u, err := url.Parse(src)
|
||||
getForce, getSrc := getForcedGetter(src)
|
||||
|
||||
u, err := url.Parse(getSrc)
|
||||
if err == nil && u.Scheme != "" {
|
||||
// Valid URL
|
||||
return src, nil
|
||||
}
|
||||
|
||||
for _, d := range Detectors {
|
||||
result, ok, err := d.Detect(src, pwd)
|
||||
result, ok, err := d.Detect(getSrc, pwd)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if ok {
|
||||
return result, nil
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
// Preserve the forced getter if it exists
|
||||
if getForce != "" {
|
||||
result = fmt.Sprintf("%s::%s", getForce, result)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("invalid source string: %s", src)
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package module
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDetect(t *testing.T) {
|
||||
cases := []struct {
|
||||
Input string
|
||||
Pwd string
|
||||
Output string
|
||||
Err bool
|
||||
}{
|
||||
{"./foo", "/foo", "file:///foo/foo", false},
|
||||
{"git::./foo", "/foo", "git::file:///foo/foo", false},
|
||||
}
|
||||
|
||||
for i, tc := range cases {
|
||||
output, err := Detect(tc.Input, tc.Pwd)
|
||||
if (err != nil) != tc.Err {
|
||||
t.Fatalf("%d: bad err: %s", i, err)
|
||||
}
|
||||
if output != tc.Output {
|
||||
t.Fatalf("%d: bad output: %s", i, output)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"net/url"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
|
@ -24,6 +25,10 @@ type Getter interface {
|
|||
// be used to get a dependency.
|
||||
var Getters map[string]Getter
|
||||
|
||||
// forcedRegexp is the regular expression that finds forced getters. This
|
||||
// syntax is schema::url, example: git::https://foo.com
|
||||
var forcedRegexp = regexp.MustCompile(`^([A-Za-z]+)::(.+)$`)
|
||||
|
||||
func init() {
|
||||
Getters = map[string]Getter{
|
||||
"file": new(FileGetter),
|
||||
|
@ -37,15 +42,21 @@ func init() {
|
|||
// src is a URL, whereas dst is always just a file path to a folder. This
|
||||
// folder doesn't need to exist. It will be created if it doesn't exist.
|
||||
func Get(dst, src string) error {
|
||||
var force string
|
||||
force, src = getForcedGetter(src)
|
||||
|
||||
u, err := url.Parse(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if force == "" {
|
||||
force = u.Scheme
|
||||
}
|
||||
|
||||
g, ok := Getters[u.Scheme]
|
||||
g, ok := Getters[force]
|
||||
if !ok {
|
||||
return fmt.Errorf(
|
||||
"module download not supported for scheme '%s'", u.Scheme)
|
||||
"module download not supported for scheme '%s'", force)
|
||||
}
|
||||
|
||||
err = g.Get(dst, u)
|
||||
|
@ -79,3 +90,15 @@ func getRunCommand(cmd *exec.Cmd) error {
|
|||
|
||||
return fmt.Errorf("error running %s: %s", cmd.Path, buf.String())
|
||||
}
|
||||
|
||||
// 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) {
|
||||
var forced string
|
||||
if ms := forcedRegexp.FindStringSubmatch(src); ms != nil {
|
||||
forced = ms[1]
|
||||
src = ms[2]
|
||||
}
|
||||
|
||||
return forced, src
|
||||
}
|
||||
|
|
|
@ -30,3 +30,18 @@ func TestGet_file(t *testing.T) {
|
|||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGet_fileForced(t *testing.T) {
|
||||
dst := tempDir(t)
|
||||
u := testModule("basic")
|
||||
u = "file::"+u
|
||||
|
||||
if err := Get(dst, u); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
mainPath := filepath.Join(dst, "main.tf")
|
||||
if _, err := os.Stat(mainPath); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue