packer.CachePath: try to create dir if it doesn't exist

This commit is contained in:
Adrien Delorme 2019-03-18 16:15:02 +01:00
parent 51294287fa
commit 4cb58446f7
1 changed files with 6 additions and 1 deletions

View File

@ -11,6 +11,7 @@ var DefaultCacheDir = "packer_cache"
//
// When the directory is not absolute, CachePath will try to get
// current working directory to be able to return a full path.
// CachePath tries to create the resulting path if it doesn't exist.
//
// CachePath can error in case it cannot find the cwd.
//
@ -19,7 +20,11 @@ var DefaultCacheDir = "packer_cache"
// PACKER_CACHE_DIR="" CacheDir("foo") => "./packer_cache/foo
// PACKER_CACHE_DIR="bar" CacheDir("foo") => "./bar/foo
// PACKER_CACHE_DIR="/home/there" CacheDir("foo", "bar") => "/home/there/foo/bar
func CachePath(paths ...string) (string, error) {
func CachePath(paths ...string) (path string, err error) {
defer func() {
// create the dir based on return path it it doesn't exist
os.MkdirAll(path, os.ModePerm)
}()
cacheDir := DefaultCacheDir
if cd := os.Getenv("PACKER_CACHE_DIR"); cd != "" {
cacheDir = cd