make code of homeDir, configFile & configDir not os specific anymore

This commit is contained in:
Adrien Delorme 2018-11-30 15:47:43 +01:00
parent 5147ac0364
commit 1055c119bc
3 changed files with 52 additions and 90 deletions

View File

@ -1,7 +1,9 @@
package packer package packer
import ( import (
"log"
"os" "os"
"os/user"
"path/filepath" "path/filepath"
) )
@ -38,3 +40,47 @@ func ConfigTmpDir() (string, error) {
} }
return td, nil return td, nil
} }
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
}

View File

@ -2,53 +2,7 @@
package packer package packer
import ( const (
"log" defaultConfigFile = ".packerconfig"
"os" defaultConfigDir = ".packer.d"
"os/user"
"path/filepath"
) )
func configFile() (string, error) {
dir, err := homeDir()
if err != nil {
return "", err
}
return filepath.Join(dir, ".packerconfig"), nil
}
func configDir() (string, error) {
dir, err := homeDir()
if err != nil {
return "", err
}
return filepath.Join(dir, ".packer.d"), nil
}
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
}

View File

@ -2,45 +2,7 @@
package packer package packer
import ( const (
"path/filepath" defaultConfigFile = "packer.config"
"syscall" defaultConfigDir = "packer.d"
"unsafe"
) )
var (
shell = syscall.MustLoadDLL("Shell32.dll")
getFolderPath = shell.MustFindProc("SHGetFolderPathW")
)
const CSIDL_APPDATA = 26
func configFile() (string, error) {
dir, err := homeDir()
if err != nil {
return "", err
}
return filepath.Join(dir, "packer.config"), nil
}
func configDir() (string, error) {
dir, err := homeDir()
if err != nil {
return "", err
}
return filepath.Join(dir, "packer.d"), nil
}
func homeDir() (string, error) {
b := make([]uint16, syscall.MAX_PATH)
// See: http://msdn.microsoft.com/en-us/library/windows/desktop/bb762181(v=vs.85).aspx
r, _, err := getFolderPath.Call(0, CSIDL_APPDATA, 0, 0, uintptr(unsafe.Pointer(&b[0])))
if uint32(r) != 0 {
return "", err
}
return syscall.UTF16ToString(b), nil
}