config/module: GetCopy

This commit is contained in:
Mitchell Hashimoto 2014-09-26 16:21:33 -07:00
parent c91fd76fe8
commit 9689a34b28
3 changed files with 55 additions and 0 deletions

View File

@ -10,10 +10,18 @@ import (
// copyDir copies the src directory contents into dst. Both directories
// should already exist.
func copyDir(dst, src string) error {
src, err := filepath.EvalSymlinks(src)
if err != nil {
return err
}
walkFn := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path == src {
return nil
}
basePath := filepath.Base(path)
if strings.HasPrefix(basePath, ".") {

View File

@ -63,6 +63,9 @@ func Get(dst, src string) error {
if err != nil {
return err
}
if err := os.RemoveAll(tmpDir); err != nil {
return err
}
defer os.RemoveAll(tmpDir)
realDst = dst
@ -104,6 +107,36 @@ func Get(dst, src string) error {
return nil
}
// GetCopy is the same as Get except that it downloads a copy of the
// module represented by source.
//
// This copy will omit and dot-prefixed files (such as .git/, .hg/) and
// can't be updated on its own.
func GetCopy(dst, src string) error {
// Create the temporary directory to do the real Get to
tmpDir, err := ioutil.TempDir("", "tf")
if err != nil {
return err
}
if err := os.RemoveAll(tmpDir); err != nil {
return err
}
defer os.RemoveAll(tmpDir)
// Get to that temporary dir
if err := Get(tmpDir, src); err != nil {
return err
}
// Make sure the destination exists
if err := os.MkdirAll(dst, 0755); err != nil {
return err
}
// Copy to the final location
return copyDir(dst, tmpDir)
}
// getRunCommand is a helper that will run a command and capture the output
// in the case an error happens.
func getRunCommand(cmd *exec.Cmd) error {

View File

@ -60,6 +60,20 @@ func TestGet_fileSubdir(t *testing.T) {
}
}
func TestGetCopy_file(t *testing.T) {
dst := tempDir(t)
u := testModule("basic")
if err := GetCopy(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)
}
}
func TestGetDirSubdir(t *testing.T) {
cases := []struct {
Input string