2017-08-11 18:13:55 -04:00
|
|
|
// +build darwin freebsd linux netbsd openbsd solaris
|
2013-06-18 01:09:07 -04:00
|
|
|
|
2015-10-16 20:32:36 -04:00
|
|
|
package packer
|
2013-06-18 01:09:07 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
2018-11-24 01:38:33 -05:00
|
|
|
"os/user"
|
2013-06-18 01:09:07 -04:00
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
func configFile() (string, error) {
|
2014-09-08 16:44:58 -04:00
|
|
|
dir, err := homeDir()
|
2013-06-18 01:09:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(dir, ".packerconfig"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func configDir() (string, error) {
|
2014-09-08 16:44:58 -04:00
|
|
|
dir, err := homeDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(dir, ".packer.d"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func homeDir() (string, error) {
|
2018-11-24 01:38:33 -05:00
|
|
|
var u *user.User
|
|
|
|
|
|
|
|
/// First prefer the HOME environmental variable
|
|
|
|
if home, ok := os.LookupEnv("HOME"); ok {
|
2013-06-18 01:09:07 -04:00
|
|
|
log.Printf("Detected home directory from env var: %s", home)
|
|
|
|
return home, nil
|
|
|
|
}
|
|
|
|
|
2018-11-24 01:38:33 -05:00
|
|
|
/// Fall back to the passwd database if not found which follows
|
|
|
|
/// the same semantics as bourne shell
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Check username specified in the environment first
|
|
|
|
if username, ok := os.LookupEnv("USER"); ok {
|
|
|
|
u, err = user.Lookup(username)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Otherwise we assume the current user
|
|
|
|
u, err = user.Current()
|
2013-06-18 01:09:07 -04:00
|
|
|
}
|
|
|
|
|
2018-11-24 01:38:33 -05:00
|
|
|
// Fail if we were unable to read the record
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2013-06-18 01:09:07 -04:00
|
|
|
}
|
|
|
|
|
2018-11-24 01:38:33 -05:00
|
|
|
return u.HomeDir, nil
|
2013-06-18 01:09:07 -04:00
|
|
|
}
|