make code of homeDir, configFile & configDir not os specific anymore
This commit is contained in:
parent
5147ac0364
commit
1055c119bc
|
@ -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 (
|
||||
"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
|
||||
}
|
||||
|
|
|
@ -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