2019-09-26 21:05:38 +00:00
|
|
|
// +build !windows,cgo !windows,osusergo
|
2017-07-10 14:10:43 +01:00
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package homedir // import "github.com/docker/docker/pkg/homedir"
|
2015-02-06 10:18:49 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2019-09-23 22:16:38 +00:00
|
|
|
"os/user"
|
2015-02-06 10:18:49 -08:00
|
|
|
)
|
|
|
|
|
2015-02-06 14:17:34 -08:00
|
|
|
// Key returns the env var name for the user's home dir based on
|
|
|
|
// the platform being run on
|
|
|
|
func Key() string {
|
|
|
|
return "HOME"
|
|
|
|
}
|
|
|
|
|
2015-02-06 10:18:49 -08:00
|
|
|
// 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.
|
2019-09-23 22:16:38 +00:00
|
|
|
// If compiling statically, ensure the osusergo build tag is used.
|
|
|
|
// If needing to do nss lookups, do not compile statically.
|
2015-02-06 10:18:49 -08:00
|
|
|
func Get() string {
|
2015-03-10 09:55:55 -04:00
|
|
|
home := os.Getenv(Key())
|
2017-07-10 14:10:43 +01:00
|
|
|
if home == "" {
|
2019-09-23 22:16:38 +00:00
|
|
|
if u, err := user.Current(); err == nil {
|
|
|
|
return u.HomeDir
|
2015-03-10 09:55:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return home
|
2015-02-06 10:18:49 -08:00
|
|
|
}
|
2015-02-18 22:53:04 -08:00
|
|
|
|
|
|
|
// 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 "~"
|
|
|
|
}
|