Skip to content

Commit

Permalink
Updating default config and cache directory for unix systems
Browse files Browse the repository at this point in the history
  • Loading branch information
teddylear committed Jun 24, 2021
1 parent 6422b57 commit 4ae7de2
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 60 deletions.
11 changes: 7 additions & 4 deletions packer/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"path/filepath"
)

var DefaultCacheDir = "packer_cache"

// CachePath returns an absolute path to a cache file or directory
//
// When the directory is not absolute, CachePath will try to get
Expand All @@ -15,6 +13,7 @@ var DefaultCacheDir = "packer_cache"
//
// CachePath can error in case it cannot find the cwd.
//
// NOTE: this is os dependent
// ex:
// PACKER_CACHE_DIR="" CacheDir() => "./packer_cache/
// PACKER_CACHE_DIR="" CacheDir("foo") => "./packer_cache/foo
Expand All @@ -25,11 +24,15 @@ func CachePath(paths ...string) (path string, err error) {
// create the dir based on return path if it doesn't exist
os.MkdirAll(filepath.Dir(path), os.ModePerm)
}()
cacheDir := DefaultCacheDir
cacheDir := getDefaultCacheDir()
if cd := os.Getenv("PACKER_CACHE_DIR"); cd != "" {
cacheDir = cd
}

paths = append([]string{cacheDir}, paths...)
return filepath.Abs(filepath.Join(paths...))
result, err := filepath.Abs(filepath.Join(paths...))
if err != nil {
return "", err
}
return result, err
}
20 changes: 20 additions & 0 deletions packer/cache_config_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// +build darwin freebsd linux netbsd openbsd solaris

package packer

import (
"os"
"path/filepath"
)

func getDefaultCacheDir() string {
var defaultConfigFileDir string

if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" {
defaultConfigFileDir = xdgConfigHome
} else {
defaultConfigFileDir = filepath.Join(os.Getenv("HOME"), "cache")
}

return filepath.Join(defaultConfigFileDir, "packer")
}
11 changes: 11 additions & 0 deletions packer/cache_config_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// +build windows

package packer

const (
defaultConfigFile = "packer_cache"
)

func getDefaultCacheDir() string {
return defaultConfigFile
}
52 changes: 0 additions & 52 deletions packer/cache_test.go

This file was deleted.

3 changes: 2 additions & 1 deletion pathing/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func configFile() (string, error) {
}
dir = homedir
}

return filepath.Join(dir, defaultConfigFile), nil
}

Expand All @@ -83,7 +84,7 @@ func configDir() (string, error) {
dir = homedir
}

return filepath.Join(dir, defaultConfigDir), nil
return filepath.Join(dir, getDefaultConfigDir()), nil
}

// Given a path, check to see if it's using ~ to reference a user directory.
Expand Down
21 changes: 19 additions & 2 deletions pathing/config_file_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,24 @@

package pathing

import (
"os"
"path/filepath"
)

const (
defaultConfigFile = ".packerconfig"
defaultConfigDir = ".packer.d"
defaultConfigFile = "packer"
)

func getDefaultConfigDir() string {

var defaultConfigFileDir string

if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" {
defaultConfigFileDir = xdgConfigHome
} else {
defaultConfigFileDir = filepath.Join(os.Getenv("HOME"), "config")
}

return filepath.Join(defaultConfigFileDir, "packer")
}
5 changes: 4 additions & 1 deletion pathing/config_file_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ package pathing

const (
defaultConfigFile = "packer.config"
defaultConfigDir = "packer.d"
)

func getDefaultConfigDir() string {
return "packer.d"
}

0 comments on commit 4ae7de2

Please sign in to comment.