mirror of https://github.com/minio/minio.git
Merge pull request #1135 from abperiasamy/vendor-update-go-homedir
vendor update for go-homedir
This commit is contained in:
commit
d561f0cc0b
|
@ -7,8 +7,10 @@ Usage is incredibly simple, just call `homedir.Dir()` to get the home directory
|
||||||
for a user, and `homedir.Expand()` to expand the `~` in a path to the home
|
for a user, and `homedir.Expand()` to expand the `~` in a path to the home
|
||||||
directory.
|
directory.
|
||||||
|
|
||||||
**Why not just use `os/user`?** The built-in `os/user` package requires
|
**Why not just use `os/user`?** The built-in `os/user` package is not
|
||||||
cgo on Darwin systems. This means that any Go code that uses that package
|
available on certain architectures such as i386 or PNaCl. Additionally
|
||||||
cannot cross compile. But 99% of the time the use for `os/user` is just to
|
it has a cgo dependency on Darwin systems. This means that any Go code
|
||||||
retrieve the home directory, which we can do for the current user without
|
that uses that package cannot cross compile. But 99% of the time the
|
||||||
cgo. This library does that, enabling cross-compilation.
|
use for `os/user` is just to retrieve the home directory, which we can
|
||||||
|
do for the current user without cgo. This library does that, enabling
|
||||||
|
cross-compilation.
|
||||||
|
|
|
@ -0,0 +1,64 @@
|
||||||
|
// +build !windows
|
||||||
|
|
||||||
|
// Copyright 2016 (C) Mitchell Hashimoto
|
||||||
|
// Distributed under the MIT License.
|
||||||
|
|
||||||
|
package homedir
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"os/user"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// dir returns the homedir of current user for all POSIX compatible
|
||||||
|
// operating systems.
|
||||||
|
func dir() (string, error) {
|
||||||
|
// First prefer the HOME environmental variable
|
||||||
|
if home := os.Getenv("HOME"); home != "" {
|
||||||
|
return home, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// user.Current is not implemented for i386 and PNaCL like environments.
|
||||||
|
if currUser, err := user.Current(); err == nil {
|
||||||
|
return currUser.HomeDir, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// If that fails, try getent
|
||||||
|
var stdout bytes.Buffer
|
||||||
|
cmd := exec.Command("getent", "passwd", strconv.Itoa(os.Getuid()))
|
||||||
|
cmd.Stdout = &stdout
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
// If "getent" is missing, ignore it
|
||||||
|
if err == exec.ErrNotFound {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if passwd := strings.TrimSpace(stdout.String()); passwd != "" {
|
||||||
|
// username:password:uid:gid:gecos:home:shell
|
||||||
|
passwdParts := strings.SplitN(passwd, ":", 7)
|
||||||
|
if len(passwdParts) > 5 {
|
||||||
|
return passwdParts[5], nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If all else fails, try the shell
|
||||||
|
stdout.Reset()
|
||||||
|
cmd = exec.Command("sh", "-c", "cd && pwd")
|
||||||
|
cmd.Stdout = &stdout
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
result := strings.TrimSpace(stdout.String())
|
||||||
|
if result == "" {
|
||||||
|
return "", errors.New("blank output when reading home directory")
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
// Copyright 2016 (C) Mitchell Hashimoto
|
||||||
|
// Distributed under the MIT License.
|
||||||
|
|
||||||
|
package homedir
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// dir returns the homedir of current user for MS Windows OS.
|
||||||
|
func dir() (string, error) {
|
||||||
|
drive := os.Getenv("HOMEDRIVE")
|
||||||
|
path := os.Getenv("HOMEPATH")
|
||||||
|
home := drive + path
|
||||||
|
if drive == "" || path == "" {
|
||||||
|
home = os.Getenv("USERPROFILE")
|
||||||
|
}
|
||||||
|
if home == "" {
|
||||||
|
return "", errors.New("HOMEDRIVE, HOMEPATH, and USERPROFILE are blank")
|
||||||
|
}
|
||||||
|
|
||||||
|
return home, nil
|
||||||
|
}
|
|
@ -1,14 +1,12 @@
|
||||||
|
// Copyright 2016 (C) Mitchell Hashimoto
|
||||||
|
// Distributed under the MIT License.
|
||||||
|
|
||||||
|
// Package homedir implements a portable function to determine current user's homedir.
|
||||||
package homedir
|
package homedir
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,37 +15,30 @@ import (
|
||||||
var DisableCache bool
|
var DisableCache bool
|
||||||
|
|
||||||
var homedirCache string
|
var homedirCache string
|
||||||
var cacheLock sync.RWMutex
|
var cacheLock sync.Mutex
|
||||||
|
|
||||||
// Dir returns the home directory for the executing user.
|
// Dir returns the home directory for the executing user.
|
||||||
//
|
//
|
||||||
// This uses an OS-specific method for discovering the home directory.
|
// This uses an OS-specific method for discovering the home directory.
|
||||||
// An error is returned if a home directory cannot be detected.
|
// An error is returned if a home directory cannot be detected.
|
||||||
func Dir() (string, error) {
|
func Dir() (string, error) {
|
||||||
if !DisableCache {
|
|
||||||
cacheLock.RLock()
|
|
||||||
cached := homedirCache
|
|
||||||
cacheLock.RUnlock()
|
|
||||||
if cached != "" {
|
|
||||||
return cached, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cacheLock.Lock()
|
cacheLock.Lock()
|
||||||
defer cacheLock.Unlock()
|
defer cacheLock.Unlock()
|
||||||
|
|
||||||
var result string
|
// Return cached homedir if available.
|
||||||
var err error
|
if !DisableCache {
|
||||||
if runtime.GOOS == "windows" {
|
if homedirCache != "" {
|
||||||
result, err = dirWindows()
|
return homedirCache, nil
|
||||||
} else {
|
}
|
||||||
// Unix-like system, so just assume Unix
|
|
||||||
result, err = dirUnix()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Determine OS speific current homedir.
|
||||||
|
result, err := dir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cache for future lookups.
|
||||||
homedirCache = result
|
homedirCache = result
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
@ -75,58 +66,3 @@ func Expand(path string) (string, error) {
|
||||||
|
|
||||||
return filepath.Join(dir, path[1:]), nil
|
return filepath.Join(dir, path[1:]), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func dirUnix() (string, error) {
|
|
||||||
// First prefer the HOME environmental variable
|
|
||||||
if home := os.Getenv("HOME"); home != "" {
|
|
||||||
return home, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// If that fails, try getent
|
|
||||||
var stdout bytes.Buffer
|
|
||||||
cmd := exec.Command("getent", "passwd", strconv.Itoa(os.Getuid()))
|
|
||||||
cmd.Stdout = &stdout
|
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
// If "getent" is missing, ignore it
|
|
||||||
if err != exec.ErrNotFound {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if passwd := strings.TrimSpace(stdout.String()); passwd != "" {
|
|
||||||
// username:password:uid:gid:gecos:home:shell
|
|
||||||
passwdParts := strings.SplitN(passwd, ":", 7)
|
|
||||||
if len(passwdParts) > 5 {
|
|
||||||
return passwdParts[5], nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If all else fails, try the shell
|
|
||||||
stdout.Reset()
|
|
||||||
cmd = exec.Command("sh", "-c", "cd && pwd")
|
|
||||||
cmd.Stdout = &stdout
|
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
result := strings.TrimSpace(stdout.String())
|
|
||||||
if result == "" {
|
|
||||||
return "", errors.New("blank output when reading home directory")
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func dirWindows() (string, error) {
|
|
||||||
drive := os.Getenv("HOMEDRIVE")
|
|
||||||
path := os.Getenv("HOMEPATH")
|
|
||||||
home := drive + path
|
|
||||||
if drive == "" || path == "" {
|
|
||||||
home = os.Getenv("USERPROFILE")
|
|
||||||
}
|
|
||||||
if home == "" {
|
|
||||||
return "", errors.New("HOMEDRIVE, HOMEPATH, and USERPROFILE are blank")
|
|
||||||
}
|
|
||||||
|
|
||||||
return home, nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -30,6 +30,8 @@ func BenchmarkDir(b *testing.B) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDir(t *testing.T) {
|
func TestDir(t *testing.T) {
|
||||||
|
// NOTE: This test is not portable. If user.Current() worked
|
||||||
|
// everywhere, we wouldn't need our package in the first place.
|
||||||
u, err := user.Current()
|
u, err := user.Current()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("err: %s", err)
|
t.Fatalf("err: %s", err)
|
||||||
|
|
|
@ -69,8 +69,8 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"path": "github.com/minio/go-homedir",
|
"path": "github.com/minio/go-homedir",
|
||||||
"revision": "d682a8f0cf139663a984ff12528da460ca963de9",
|
"revision": "0b1069c753c94b3633cc06a1995252dbcc27c7a6",
|
||||||
"revisionTime": "2015-10-24T22:24:27-07:00"
|
"revisionTime": "2016-02-15T17:25:11+05:30"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"path": "github.com/minio/minio-go",
|
"path": "github.com/minio/minio-go",
|
||||||
|
|
Loading…
Reference in New Issue