forked from hashicorp/packer-plugin-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_file_unix.go
More file actions
49 lines (39 loc) · 1.17 KB
/
config_file_unix.go
File metadata and controls
49 lines (39 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// +build darwin freebsd linux netbsd openbsd solaris
package pathing
import (
"errors"
"log"
"os"
"path/filepath"
)
const (
defaultConfigFile = ".packerconfig"
defaultConfigDir = ".packer.d"
)
func configDir() (path string, err error) {
if cd := os.Getenv("PACKER_CONFIG_DIR"); cd != "" {
log.Printf("Detected config directory from env var: %s", cd)
return filepath.Join(cd, defaultConfigDir), nil
}
var dir string
homedir := os.Getenv("HOME")
if homedir == "" {
return "", errors.New("No $HOME environment variable found, required to set Config Directory")
}
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
}