2015-10-16 20:32:36 -04:00
|
|
|
package packer
|
|
|
|
|
2015-10-16 20:36:29 -04:00
|
|
|
import (
|
2018-11-30 09:47:43 -05:00
|
|
|
"log"
|
2015-10-16 20:36:29 -04:00
|
|
|
"os"
|
2018-11-30 09:47:43 -05:00
|
|
|
"os/user"
|
2015-10-16 20:36:29 -04:00
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2015-10-16 20:32:36 -04:00
|
|
|
// ConfigFile returns the default path to the configuration file. On
|
|
|
|
// Unix-like systems this is the ".packerconfig" file in the home directory.
|
|
|
|
// On Windows, this is the "packer.config" file in the application data
|
|
|
|
// directory.
|
|
|
|
func ConfigFile() (string, error) {
|
|
|
|
return configFile()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigDir returns the configuration directory for Packer.
|
|
|
|
func ConfigDir() (string, error) {
|
|
|
|
return configDir()
|
|
|
|
}
|
2015-10-16 20:36:29 -04:00
|
|
|
|
|
|
|
// ConfigTmpDir returns the configuration tmp directory for Packer
|
|
|
|
func ConfigTmpDir() (string, error) {
|
|
|
|
if tmpdir := os.Getenv("PACKER_TMP_DIR"); tmpdir != "" {
|
|
|
|
return filepath.Abs(tmpdir)
|
|
|
|
}
|
|
|
|
configdir, err := configDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
td := filepath.Join(configdir, "tmp")
|
|
|
|
_, err = os.Stat(td)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(td, 0755); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return td, nil
|
|
|
|
}
|
2018-11-30 09:47:43 -05:00
|
|
|
|
|
|
|
func homeDir() (string, error) {
|
|
|
|
|
|
|
|
// First prefer the HOME environmental variable
|
|
|
|
if home := os.Getenv("HOME"); home != "" {
|
|
|
|
log.Printf("Detected home directory from env var: %s", home)
|
|
|
|
return home, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to the passwd database if not found which follows
|
|
|
|
// the same semantics as bourne shell
|
|
|
|
u, err := user.Current()
|
|
|
|
|
|
|
|
// Get homedir from specified username
|
|
|
|
// if it is set and different than what we have
|
|
|
|
if username := os.Getenv("USER"); username != "" && err == nil && u.Username != username {
|
|
|
|
u, err = user.Lookup(username)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fail if we were unable to read the record
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.HomeDir, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func configFile() (string, error) {
|
|
|
|
dir, err := homeDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(dir, defaultConfigFile), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func configDir() (string, error) {
|
|
|
|
dir, err := homeDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(dir, defaultConfigDir), nil
|
|
|
|
}
|