Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/xdg base dir 2 #68

Merged
merged 13 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
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
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
azr marked this conversation as resolved.
Show resolved Hide resolved
}
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 != "" {
azr marked this conversation as resolved.
Show resolved Hide resolved
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"
}