github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/pkg/homedir/homedir.go (about)

     1  package homedir
     2  
     3  import (
     4  	"os"
     5  	"runtime"
     6  
     7  	"github.com/opencontainers/runc/libcontainer/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  	if runtime.GOOS == "windows" {
    14  		return "USERPROFILE"
    15  	}
    16  	return "HOME"
    17  }
    18  
    19  // Get returns the home directory of the current user with the help of
    20  // environment variables depending on the target operating system.
    21  // Returned path should be used with "path/filepath" to form new paths.
    22  func Get() string {
    23  	home := os.Getenv(Key())
    24  	if home == "" && runtime.GOOS != "windows" {
    25  		if u, err := user.CurrentUser(); err == nil {
    26  			return u.Home
    27  		}
    28  	}
    29  	return home
    30  }
    31  
    32  // GetShortcutString returns the string that is shortcut to user's home directory
    33  // in the native shell of the platform running on.
    34  func GetShortcutString() string {
    35  	if runtime.GOOS == "windows" {
    36  		return "%USERPROFILE%" // be careful while using in format functions
    37  	}
    38  	return "~"
    39  }