github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/home/home.go (about)

     1  //go:build !js
     2  // +build !js
     3  
     4  package home
     5  
     6  import (
     7  	"os"
     8  	"os/user"
     9  
    10  	"github.com/lmorg/murex/utils"
    11  	"github.com/lmorg/murex/utils/consts"
    12  )
    13  
    14  // MyDir is the $USER directory.
    15  // Typically /home/$USER/ on non-Windows systems, or \users\$USER on Windows.
    16  var MyDir string
    17  
    18  func init() {
    19  	usr, err := user.Current()
    20  	if err != nil {
    21  		os.Stderr.WriteString(err.Error() + utils.NewLineString)
    22  		MyDir = consts.PathSlash
    23  		return
    24  	}
    25  
    26  	MyDir = usr.HomeDir
    27  }
    28  
    29  // UserDir is the home directory of a `username`.
    30  func UserDir(username string) string {
    31  	usr, err := user.Lookup(username)
    32  	if err != nil {
    33  		return consts.PathSlash
    34  	}
    35  
    36  	return usr.HomeDir
    37  }