Skip to content

Commit f8d8c28

Browse files
committed
Updating default config and cache directory for unix systems
1 parent 6422b57 commit f8d8c28

7 files changed

Lines changed: 66 additions & 66 deletions

File tree

packer/cache.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@ import (
55
"path/filepath"
66
)
77

8-
var DefaultCacheDir = "packer_cache"
9-
108
// CachePath returns an absolute path to a cache file or directory
119
//
12-
// When the directory is not absolute, CachePath will try to get
13-
// current working directory to be able to return a full path.
14-
// CachePath tries to create the resulting path if it doesn't exist.
15-
//
16-
// CachePath can error in case it cannot find the cwd.
10+
// When the directory is not absolute, CachePath will try to make a
11+
// a cache depending on the operating system.
1712
//
13+
// NOTE: cache directory will change depending on operating system dependent
1814
// ex:
1915
// PACKER_CACHE_DIR="" CacheDir() => "./packer_cache/
2016
// PACKER_CACHE_DIR="" CacheDir("foo") => "./packer_cache/foo
@@ -25,11 +21,15 @@ func CachePath(paths ...string) (path string, err error) {
2521
// create the dir based on return path if it doesn't exist
2622
os.MkdirAll(filepath.Dir(path), os.ModePerm)
2723
}()
28-
cacheDir := DefaultCacheDir
24+
cacheDir := getDefaultCacheDir()
2925
if cd := os.Getenv("PACKER_CACHE_DIR"); cd != "" {
3026
cacheDir = cd
3127
}
3228

3329
paths = append([]string{cacheDir}, paths...)
34-
return filepath.Abs(filepath.Join(paths...))
30+
result, err := filepath.Abs(filepath.Join(paths...))
31+
if err != nil {
32+
return "", err
33+
}
34+
return result, err
3535
}

packer/cache_config_unix.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// +build darwin freebsd linux netbsd openbsd solaris
2+
3+
package packer
4+
5+
import (
6+
"os"
7+
"path/filepath"
8+
)
9+
10+
func getDefaultCacheDir() string {
11+
var defaultConfigFileDir string
12+
13+
if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" {
14+
defaultConfigFileDir = xdgConfigHome
15+
} else {
16+
defaultConfigFileDir = filepath.Join(os.Getenv("HOME"), "cache")
17+
}
18+
19+
return filepath.Join(defaultConfigFileDir, "packer")
20+
}

packer/cache_config_windows.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// +build windows
2+
3+
package packer
4+
5+
const (
6+
defaultConfigFile = "packer_cache"
7+
)
8+
9+
func getDefaultCacheDir() string {
10+
return defaultConfigFile
11+
}

packer/cache_test.go

Lines changed: 0 additions & 52 deletions
This file was deleted.

pathing/config_file.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ func configFile() (string, error) {
6767
}
6868
dir = homedir
6969
}
70+
7071
return filepath.Join(dir, defaultConfigFile), nil
7172
}
7273

73-
func configDir() (string, error) {
74+
func configDir() (path string, err error) {
7475
var dir string
7576
if cd := os.Getenv("PACKER_CONFIG_DIR"); cd != "" {
7677
log.Printf("Detected config directory from env var: %s", cd)
@@ -83,7 +84,7 @@ func configDir() (string, error) {
8384
dir = homedir
8485
}
8586

86-
return filepath.Join(dir, defaultConfigDir), nil
87+
return filepath.Join(dir, getDefaultConfigDir()), nil
8788
}
8889

8990
// Given a path, check to see if it's using ~ to reference a user directory.

pathing/config_file_unix.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,24 @@
22

33
package pathing
44

5+
import (
6+
"os"
7+
"path/filepath"
8+
)
9+
510
const (
6-
defaultConfigFile = ".packerconfig"
7-
defaultConfigDir = ".packer.d"
11+
defaultConfigFile = "packer"
812
)
13+
14+
func getDefaultConfigDir() string {
15+
16+
var defaultConfigFileDir string
17+
18+
if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" {
19+
defaultConfigFileDir = xdgConfigHome
20+
} else {
21+
defaultConfigFileDir = filepath.Join(os.Getenv("HOME"), "config")
22+
}
23+
24+
return filepath.Join(defaultConfigFileDir, "packer")
25+
}

pathing/config_file_windows.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ package pathing
44

55
const (
66
defaultConfigFile = "packer.config"
7-
defaultConfigDir = "packer.d"
87
)
8+
9+
func getDefaultConfigDir() string {
10+
return "packer.d"
11+
}

0 commit comments

Comments
 (0)