Merge pull request #7036 from arizvisa/safe.packer-config_file_unix
Replaced unsafe method of determining a user's home directory with an implementation based on os/user.
This commit is contained in:
commit
6f24bc48ac
|
@ -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
|
||||
}
|
||||
|
|
|
@ -2,53 +2,7 @@
|
|||
|
||||
package packer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
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
|
||||
}
|
||||
|
||||
// If that fails, try the shell
|
||||
var stdout bytes.Buffer
|
||||
cmd := exec.Command("sh", "-c", "eval echo ~$USER")
|
||||
cmd.Stdout = &stdout
|
||||
if err := cmd.Run(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
result := strings.TrimSpace(stdout.String())
|
||||
if result == "" {
|
||||
return "", errors.New("blank output")
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue