-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config/module: use the raw source as part of the key
This changes the key for the storage to be the _raw_ source from the module, not the fully expanded source. Example: it'll be a relative path instead of an absolute path. This allows the ".terraform/modules" directory to be portable when moving to other machines. This was a behavior that existed in <= 0.7.2 and was broken with #8398. This amends that and adds a test to verify.
- Loading branch information
Showing
3 changed files
with
169 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package copy | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// From: https://gist.github.com/m4ng0squ4sh/92462b38df26839a3ca324697c8cba04 | ||
|
||
// CopyFile copies the contents of the file named src to the file named | ||
// by dst. The file will be created if it does not already exist. If the | ||
// destination file exists, all it's contents will be replaced by the contents | ||
// of the source file. The file mode will be copied from the source and | ||
// the copied data is synced/flushed to stable storage. | ||
func CopyFile(src, dst string) (err error) { | ||
in, err := os.Open(src) | ||
if err != nil { | ||
return | ||
} | ||
defer in.Close() | ||
|
||
out, err := os.Create(dst) | ||
if err != nil { | ||
return | ||
} | ||
defer func() { | ||
if e := out.Close(); e != nil { | ||
err = e | ||
} | ||
}() | ||
|
||
_, err = io.Copy(out, in) | ||
if err != nil { | ||
return | ||
} | ||
|
||
err = out.Sync() | ||
if err != nil { | ||
return | ||
} | ||
|
||
si, err := os.Stat(src) | ||
if err != nil { | ||
return | ||
} | ||
err = os.Chmod(dst, si.Mode()) | ||
if err != nil { | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
// CopyDir recursively copies a directory tree, attempting to preserve permissions. | ||
// Source directory must exist, destination directory must *not* exist. | ||
// Symlinks are ignored and skipped. | ||
func CopyDir(src string, dst string) (err error) { | ||
src = filepath.Clean(src) | ||
dst = filepath.Clean(dst) | ||
|
||
si, err := os.Stat(src) | ||
if err != nil { | ||
return err | ||
} | ||
if !si.IsDir() { | ||
return fmt.Errorf("source is not a directory") | ||
} | ||
|
||
_, err = os.Stat(dst) | ||
if err != nil && !os.IsNotExist(err) { | ||
return | ||
} | ||
if err == nil { | ||
return fmt.Errorf("destination already exists") | ||
} | ||
|
||
err = os.MkdirAll(dst, si.Mode()) | ||
if err != nil { | ||
return | ||
} | ||
|
||
entries, err := ioutil.ReadDir(src) | ||
if err != nil { | ||
return | ||
} | ||
|
||
for _, entry := range entries { | ||
srcPath := filepath.Join(src, entry.Name()) | ||
dstPath := filepath.Join(dst, entry.Name()) | ||
|
||
// If the entry is a symlink, we copy the contents | ||
for entry.Mode()&os.ModeSymlink != 0 { | ||
target, err := os.Readlink(srcPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
entry, err = os.Stat(target) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if entry.IsDir() { | ||
err = CopyDir(srcPath, dstPath) | ||
if err != nil { | ||
return | ||
} | ||
} else { | ||
err = CopyFile(srcPath, dstPath) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
} | ||
|
||
return | ||
} |