Skip to content

Commit 4c9e709

Browse files
authored
Merge pull request #68 from teddylear/feature/xdg-base-dir-2
Feature/xdg base dir 2
2 parents d47c130 + 006be29 commit 4c9e709

10 files changed

Lines changed: 435 additions & 25 deletions

packer/cache.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,29 @@ 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
//
18-
// ex:
13+
// TODO: Update this with better examples
14+
// NOTE: cache directory will change depending on operating system dependent
15+
// For Windows:
1916
// PACKER_CACHE_DIR="" CacheDir() => "./packer_cache/
2017
// PACKER_CACHE_DIR="" CacheDir("foo") => "./packer_cache/foo
2118
// PACKER_CACHE_DIR="bar" CacheDir("foo") => "./bar/foo
2219
// PACKER_CACHE_DIR="/home/there" CacheDir("foo", "bar") => "/home/there/foo/bar
20+
// For Unix:
21+
// PACKER_CACHE_DIR="", XDG_CONFIG_HOME="", Default_config CacheDir() => "$HOME/cache/packer"
22+
// PACKER_CACHE_DIR="", XDG_CONFIG_HOME="", Default_config CacheDir("foo") => "$HOME/cache/packer/foo"
23+
// PACKER_CACHE_DIR="bar", XDG_CONFIG_HOME="", Default_config CacheDir("foo") => "./bar/foo
24+
// PACKER_CACHE_DIR="/home/there", XDG_CONFIG_HOME="", Default_config CacheDir("foo", "bar") => "/home/there/foo/bar
2325
func CachePath(paths ...string) (path string, err error) {
2426
defer func() {
2527
// create the dir based on return path if it doesn't exist
2628
os.MkdirAll(filepath.Dir(path), os.ModePerm)
2729
}()
28-
cacheDir := DefaultCacheDir
30+
cacheDir := getDefaultCacheDir()
2931
if cd := os.Getenv("PACKER_CACHE_DIR"); cd != "" {
3032
cacheDir = cd
3133
}

packer/cache_config_unix.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 xdgCacheHome := os.Getenv("XDG_CACHE_HOME"); xdgCacheHome != "" {
14+
return filepath.Join(xdgCacheHome, "packer")
15+
}
16+
17+
homeDir := os.Getenv("HOME")
18+
defaultConfigFileDir = filepath.Join(homeDir, ".cache", "packer")
19+
20+
return defaultConfigFileDir
21+
}

packer/cache_config_unix_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// +build darwin freebsd linux netbsd openbsd solaris
2+
3+
package packer
4+
5+
import (
6+
"io/ioutil"
7+
"os"
8+
"path/filepath"
9+
"testing"
10+
)
11+
12+
func TestCachePath(t *testing.T) {
13+
// temporary directories for env vars
14+
xdgCacheHomeTempDir, err := ioutil.TempDir(os.TempDir(), "*")
15+
if err != nil {
16+
t.Fatalf("Failed to create temp test directory: failing test: %v", err)
17+
}
18+
defer os.RemoveAll(xdgCacheHomeTempDir)
19+
packerCacheTempDir, err := ioutil.TempDir(os.TempDir(), "*")
20+
if err != nil {
21+
t.Fatalf("Failed to create temp test directory: failing test: %v", err)
22+
}
23+
defer os.RemoveAll(packerCacheTempDir)
24+
25+
// reset env
26+
packerCacheDir := os.Getenv("PACKER_CACHE_DIR")
27+
os.Setenv("PACKER_CACHE_DIR", "")
28+
defer func() {
29+
os.Setenv("PACKER_CACHE_DIR", packerCacheDir)
30+
}()
31+
32+
xdgCacheHomeDir := os.Getenv("XDG_CACHE_HOME")
33+
os.Setenv("XDG_CACHE_HOME", "")
34+
defer func() {
35+
os.Setenv("XDG_CACHE_HOME", xdgCacheHomeDir)
36+
}()
37+
38+
type args struct {
39+
paths []string
40+
}
41+
tests := []struct {
42+
name string
43+
args args
44+
env map[string]string
45+
want string
46+
wantErr bool
47+
}{
48+
{
49+
"base",
50+
args{},
51+
nil,
52+
filepath.Join(os.Getenv("HOME"), ".cache", "packer"),
53+
false,
54+
},
55+
{
56+
"base and path",
57+
args{[]string{"a", "b"}},
58+
nil,
59+
filepath.Join(os.Getenv("HOME"), ".cache", "packer", "a", "b"),
60+
false,
61+
},
62+
{
63+
"env PACKER_CACHE_DIR and path",
64+
args{[]string{"a", "b"}},
65+
map[string]string{"PACKER_CACHE_DIR": packerCacheTempDir},
66+
filepath.Join(packerCacheTempDir, "a", "b"),
67+
false,
68+
},
69+
{
70+
"env XDG_CACHE_HOME and path",
71+
args{[]string{"a", "b"}},
72+
map[string]string{"XDG_CACHE_HOME": xdgCacheHomeTempDir},
73+
filepath.Join(xdgCacheHomeTempDir, "packer", "a", "b"),
74+
false,
75+
},
76+
{
77+
"env PACKER_CACHE_DIR, XDG_CACHE_HOME, and path",
78+
args{[]string{"a", "b"}},
79+
map[string]string{
80+
"XDG_CACHE_HOME": xdgCacheHomeTempDir,
81+
"PACKER_CACHE_DIR": packerCacheTempDir,
82+
},
83+
filepath.Join(packerCacheTempDir, "a", "b"),
84+
false,
85+
},
86+
}
87+
for _, tt := range tests {
88+
t.Run(tt.name, func(t *testing.T) {
89+
for k, v := range tt.env {
90+
os.Setenv(k, v)
91+
}
92+
got, err := CachePath(tt.args.paths...)
93+
if (err != nil) != tt.wantErr {
94+
t.Errorf("CachePath() error = %v, wantErr %v", err, tt.wantErr)
95+
return
96+
}
97+
if got != tt.want {
98+
t.Errorf("CachePath() = %v, want %v", got, tt.want)
99+
}
100+
resetTestEnv()
101+
})
102+
}
103+
}
104+
105+
func resetTestEnv() {
106+
os.Setenv("PACKER_CACHE_DIR", "")
107+
os.Setenv("XDG_CACHE_HOME", "")
108+
}

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+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build windows
2+
13
package packer
24

35
import (

pathing/config_file.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,6 @@ func configFile() (string, error) {
7070
return filepath.Join(dir, defaultConfigFile), nil
7171
}
7272

73-
func configDir() (string, error) {
74-
var dir string
75-
if cd := os.Getenv("PACKER_CONFIG_DIR"); cd != "" {
76-
log.Printf("Detected config directory from env var: %s", cd)
77-
dir = cd
78-
} else {
79-
homedir, err := homeDir()
80-
if err != nil {
81-
return "", err
82-
}
83-
dir = homedir
84-
}
85-
86-
return filepath.Join(dir, defaultConfigDir), nil
87-
}
88-
8973
// Given a path, check to see if it's using ~ to reference a user directory.
9074
// If so, then replace that component with the requested user directory.
9175
// In "~/", "~" gets replaced by current user's home dir.

pathing/config_file_unix.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,48 @@
22

33
package pathing
44

5+
import (
6+
"errors"
7+
"log"
8+
"os"
9+
"path/filepath"
10+
)
11+
512
const (
613
defaultConfigFile = ".packerconfig"
714
defaultConfigDir = ".packer.d"
815
)
16+
17+
func configDir() (path string, err error) {
18+
19+
if cd := os.Getenv("PACKER_CONFIG_DIR"); cd != "" {
20+
log.Printf("Detected config directory from env var: %s", cd)
21+
return filepath.Join(cd, defaultConfigDir), nil
22+
}
23+
24+
var dir string
25+
homedir := os.Getenv("HOME")
26+
27+
if homedir == "" {
28+
return "", errors.New("No $HOME environment variable found, required to set Config Directory")
29+
}
30+
31+
if hasDefaultConfigFileLocation(homedir) {
32+
dir = filepath.Join(homedir, defaultConfigDir)
33+
log.Printf("Old default config directory found: %s", dir)
34+
} else if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" {
35+
log.Printf("Detected xdg config directory from env var: %s", xdgConfigHome)
36+
dir = filepath.Join(xdgConfigHome, "packer")
37+
} else {
38+
dir = filepath.Join(homedir, ".config", "packer")
39+
}
40+
41+
return dir, nil
42+
}
43+
44+
func hasDefaultConfigFileLocation(homedir string) bool {
45+
if _, err := os.Stat(filepath.Join(homedir, defaultConfigDir)); err != nil {
46+
return false
47+
}
48+
return true
49+
}

0 commit comments

Comments
 (0)