forked from hashicorp/packer-plugin-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
35 lines (32 loc) · 1.04 KB
/
cache.go
File metadata and controls
35 lines (32 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package packer
import (
"os"
"path/filepath"
)
// CachePath returns an absolute path to a cache file or directory
//
// When the directory is not absolute, CachePath will try to make a
// a cache depending on the operating system.
//
// NOTE: cache directory will change depending on operating system dependent
// ex:
// PACKER_CACHE_DIR="" CacheDir() => "./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) (path string, err error) {
defer func() {
// create the dir based on return path if it doesn't exist
os.MkdirAll(filepath.Dir(path), os.ModePerm)
}()
cacheDir := getDefaultCacheDir()
if cd := os.Getenv("PACKER_CACHE_DIR"); cd != "" {
cacheDir = cd
}
paths = append([]string{cacheDir}, paths...)
result, err := filepath.Abs(filepath.Join(paths...))
if err != nil {
return "", err
}
return result, err
}