1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #34040 from justincormack/windows-homedir

Split homedir files by operating system
This commit is contained in:
Akihiro Suda 2017-07-11 01:27:50 +09:00 committed by GitHub
commit e639a70fbe
2 changed files with 27 additions and 8 deletions

View file

@ -1,8 +1,9 @@
// +build !windows
package homedir
import (
"os"
"runtime"
"github.com/opencontainers/runc/libcontainer/user"
)
@ -10,9 +11,6 @@ import (
// Key returns the env var name for the user's home dir based on
// the platform being run on
func Key() string {
if runtime.GOOS == "windows" {
return "USERPROFILE"
}
return "HOME"
}
@ -21,7 +19,7 @@ func Key() string {
// Returned path should be used with "path/filepath" to form new paths.
func Get() string {
home := os.Getenv(Key())
if home == "" && runtime.GOOS != "windows" {
if home == "" {
if u, err := user.CurrentUser(); err == nil {
return u.Home
}
@ -32,8 +30,5 @@ func Get() string {
// GetShortcutString returns the string that is shortcut to user's home directory
// in the native shell of the platform running on.
func GetShortcutString() string {
if runtime.GOOS == "windows" {
return "%USERPROFILE%" // be careful while using in format functions
}
return "~"
}

View file

@ -0,0 +1,24 @@
package homedir
import (
"os"
)
// Key returns the env var name for the user's home dir based on
// the platform being run on
func Key() string {
return "USERPROFILE"
}
// Get returns the home directory of the current user with the help of
// environment variables depending on the target operating system.
// Returned path should be used with "path/filepath" to form new paths.
func Get() string {
return os.Getenv(Key())
}
// GetShortcutString returns the string that is shortcut to user's home directory
// in the native shell of the platform running on.
func GetShortcutString() string {
return "%USERPROFILE%" // be careful while using in format functions
}