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 f8d8c28
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 66 deletions.
18 changes: 9 additions & 9 deletions packer/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ 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
// 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.
// 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
Expand All @@ -25,11 +21,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.

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

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

func configDir() (string, error) {
func configDir() (path string, err error) {
var dir string
if cd := os.Getenv("PACKER_CONFIG_DIR"); cd != "" {
log.Printf("Detected config directory from env var: %s", cd)
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 f8d8c28

Please sign in to comment.