From 9689a34b285c999610fad3bf3069f90e470d9dda Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 26 Sep 2014 16:21:33 -0700 Subject: [PATCH] config/module: GetCopy --- config/module/copy_dir.go | 8 ++++++++ config/module/get.go | 33 +++++++++++++++++++++++++++++++++ config/module/get_test.go | 14 ++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/config/module/copy_dir.go b/config/module/copy_dir.go index 6408913e77..981bba1ba7 100644 --- a/config/module/copy_dir.go +++ b/config/module/copy_dir.go @@ -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, ".") { diff --git a/config/module/get.go b/config/module/get.go index bb811d63e6..14f105548c 100644 --- a/config/module/get.go +++ b/config/module/get.go @@ -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 { diff --git a/config/module/get_test.go b/config/module/get_test.go index 8721694823..cf34f1ae8a 100644 --- a/config/module/get_test.go +++ b/config/module/get_test.go @@ -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