From 1055c119bc8a77704f4a1f5cc5920a946c0773dd Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Fri, 30 Nov 2018 15:47:43 +0100 Subject: [PATCH] make code of homeDir, configFile & configDir not os specific anymore --- packer/config_file.go | 46 +++++++++++++++++++++++++++++++ packer/config_file_unix.go | 52 ++--------------------------------- packer/config_file_windows.go | 44 ++--------------------------- 3 files changed, 52 insertions(+), 90 deletions(-) diff --git a/packer/config_file.go b/packer/config_file.go index edd10edee..043b40ae8 100644 --- a/packer/config_file.go +++ b/packer/config_file.go @@ -1,7 +1,9 @@ package packer import ( + "log" "os" + "os/user" "path/filepath" ) @@ -38,3 +40,47 @@ func ConfigTmpDir() (string, error) { } 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 +} diff --git a/packer/config_file_unix.go b/packer/config_file_unix.go index 5c7a31760..ab3ac73be 100644 --- a/packer/config_file_unix.go +++ b/packer/config_file_unix.go @@ -2,53 +2,7 @@ package packer -import ( - "log" - "os" - "os/user" - "path/filepath" +const ( + defaultConfigFile = ".packerconfig" + defaultConfigDir = ".packer.d" ) - -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 -} diff --git a/packer/config_file_windows.go b/packer/config_file_windows.go index d0bcc1c50..b249c492d 100644 --- a/packer/config_file_windows.go +++ b/packer/config_file_windows.go @@ -2,45 +2,7 @@ package packer -import ( - "path/filepath" - "syscall" - "unsafe" +const ( + defaultConfigFile = "packer.config" + defaultConfigDir = "packer.d" ) - -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 -}