Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
//
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋🏼
Could you please update the examples down here in this list depending on the OS ? That would make it easier for me. 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super awesome, thanks.

// 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
Comment thread
azr marked this conversation as resolved.
Outdated
}
21 changes: 21 additions & 0 deletions packer/cache_config_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// +build darwin freebsd linux netbsd openbsd solaris

package packer

import (
"os"
"path/filepath"
)

func getDefaultCacheDir() string {
var defaultConfigFileDir string

if xdgCacheHome := os.Getenv("XDG_CACHE_HOME"); xdgCacheHome != "" {
defaultConfigFileDir = filepath.Join(xdgCacheHome, "packer")
} else {
Comment thread
teddylear marked this conversation as resolved.
Outdated
homeDir := os.Getenv("HOME")
defaultConfigFileDir = filepath.Join(homeDir, ".cache", "packer")
}

return defaultConfigFileDir
}
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.

16 changes: 0 additions & 16 deletions pathing/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,6 @@ func configFile() (string, error) {
return filepath.Join(dir, defaultConfigFile), nil
}

func configDir() (string, error) {
var dir string
if cd := os.Getenv("PACKER_CONFIG_DIR"); cd != "" {
log.Printf("Detected config directory from env var: %s", cd)
dir = cd
} else {
homedir, err := homeDir()
if err != nil {
return "", err
}
dir = homedir
}

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

// Given a path, check to see if it's using ~ to reference a user directory.
// If so, then replace that component with the requested user directory.
// In "~/", "~" gets replaced by current user's home dir.
Expand Down
35 changes: 35 additions & 0 deletions pathing/config_file_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,42 @@

package pathing

import (
"log"
"os"
"path/filepath"
)

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

func configDir() (path string, err error) {
var dir string
homedir := os.Getenv("HOME")
if homedir == "" {
return "", err
}
Comment thread
azr marked this conversation as resolved.
if cd := os.Getenv("PACKER_CONFIG_DIR"); cd != "" {
log.Printf("Detected config directory from env var: %s", cd)
dir = filepath.Join(cd, defaultConfigDir)
} else if hasDefaultConfigFileLocation(homedir) {
dir = filepath.Join(homedir, defaultConfigDir)
log.Printf("Old default config directory found: %s", dir)
} else if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" {
log.Printf("Detected xdg config directory from env var: %s", xdgConfigHome)
dir = filepath.Join(xdgConfigHome, "packer")
} else {
dir = filepath.Join(homedir, ".config", "packer")
}

return dir, nil
}

func hasDefaultConfigFileLocation(homedir string) bool {
if _, err := os.Stat(filepath.Join(homedir, defaultConfigDir)); err != nil {
return false
}
return true
}
22 changes: 22 additions & 0 deletions pathing/config_file_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,29 @@

package pathing

import (
"log"
"os"
"path/filepath"
)

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

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)
dir = filepath.Join(cd, defaultConfigDir)
} else {
Comment thread
teddylear marked this conversation as resolved.
Outdated
homedir, err := homeDir()
if err != nil {
return "", err
}
dir = filepath.Join(homedir, defaultConfigDir)
}

return dir, nil
}