github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/pkg/homedir/homedir.go (about)

     1  package homedir
     2  
     3  import (
     4  	"os"
     5  	"os/user"
     6  	"runtime"
     7  )
     8  
     9  // Key returns the env var name for the user's home dir based on
    10  // the platform being run on.
    11  //
    12  // Deprecated: this function is no longer used, and will be removed in the next release.
    13  func Key() string {
    14  	return envKeyName
    15  }
    16  
    17  // Get returns the home directory of the current user with the help of
    18  // environment variables depending on the target operating system.
    19  // Returned path should be used with "path/filepath" to form new paths.
    20  //
    21  // On non-Windows platforms, it falls back to nss lookups, if the home
    22  // directory cannot be obtained from environment-variables.
    23  //
    24  // If linking statically with cgo enabled against glibc, ensure the
    25  // osusergo build tag is used.
    26  //
    27  // If needing to do nss lookups, do not disable cgo or set osusergo.
    28  func Get() string {
    29  	home, _ := os.UserHomeDir()
    30  	if home == "" && runtime.GOOS != "windows" {
    31  		if u, err := user.Current(); err == nil {
    32  			return u.HomeDir
    33  		}
    34  	}
    35  	return home
    36  }
    37  
    38  // GetShortcutString returns the string that is shortcut to user's home directory
    39  // in the native shell of the platform running on.
    40  //
    41  // Deprecated: this function is no longer used, and will be removed in the next release.
    42  func GetShortcutString() string {
    43  	return homeShortCut
    44  }