github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/homedir/homedir_unix.go (about)

     1  // +build !windows
     2  
     3  package homedir // import "github.com/demonoid81/moby/pkg/homedir"
     4  
     5  import (
     6  	"os"
     7  	"os/user"
     8  )
     9  
    10  // Key returns the env var name for the user's home dir based on
    11  // the platform being run on
    12  func Key() string {
    13  	return "HOME"
    14  }
    15  
    16  // Get returns the home directory of the current user with the help of
    17  // environment variables depending on the target operating system.
    18  // Returned path should be used with "path/filepath" to form new paths.
    19  //
    20  // If linking statically with cgo enabled against glibc, ensure the
    21  // osusergo build tag is used.
    22  //
    23  // If needing to do nss lookups, do not disable cgo or set osusergo.
    24  func Get() string {
    25  	home := os.Getenv(Key())
    26  	if home == "" {
    27  		if u, err := user.Current(); err == nil {
    28  			return u.HomeDir
    29  		}
    30  	}
    31  	return home
    32  }
    33  
    34  // GetShortcutString returns the string that is shortcut to user's home directory
    35  // in the native shell of the platform running on.
    36  func GetShortcutString() string {
    37  	return "~"
    38  }