config/module: Get can support subdirs

This commit is contained in:
Mitchell Hashimoto 2014-09-26 16:11:13 -07:00
parent 615192a6c4
commit c91fd76fe8
3 changed files with 54 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import (
"io"
"os"
"path/filepath"
"strings"
)
// copyDir copies the src directory contents into dst. Both directories
@ -14,7 +15,13 @@ func copyDir(dst, src string) error {
return err
}
dstPath := filepath.Join(dst, filepath.Base(path))
basePath := filepath.Base(path)
if strings.HasPrefix(basePath, ".") {
// Skip any dot files
return nil
}
dstPath := filepath.Join(dst, basePath)
// If we have a directory, make that subdirectory, then continue
// the walk.

View File

@ -3,8 +3,11 @@ package module
import (
"bytes"
"fmt"
"io/ioutil"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"syscall"
@ -51,6 +54,21 @@ func Get(dst, src string) error {
var force string
force, src = getForcedGetter(src)
// If there is a subdir component, then we download the root separately
// and then copy over the proper subdir.
var realDst string
src, subDir := getDirSubdir(src)
if subDir != "" {
tmpDir, err := ioutil.TempDir("", "tf")
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)
realDst = dst
dst = subDir
}
u, err := url.Parse(src)
if err != nil {
return err
@ -68,9 +86,22 @@ func Get(dst, src string) error {
err = g.Get(dst, u)
if err != nil {
err = fmt.Errorf("error downloading module '%s': %s", src, err)
return err
}
return err
// If we have a subdir, copy that over
if subDir != "" {
if err := os.RemoveAll(realDst); err != nil {
return err
}
if err := os.MkdirAll(realDst, 0755); err != nil {
return err
}
return copyDir(realDst, filepath.Join(dst, subDir))
}
return nil
}
// getRunCommand is a helper that will run a command and capture the output

View File

@ -46,6 +46,20 @@ func TestGet_fileForced(t *testing.T) {
}
}
func TestGet_fileSubdir(t *testing.T) {
dst := tempDir(t)
u := testModule("basic//subdir")
if err := Get(dst, u); err != nil {
t.Fatalf("err: %s", err)
}
mainPath := filepath.Join(dst, "sub.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestGetDirSubdir(t *testing.T) {
cases := []struct {
Input string