mirror of
https://github.com/tmedwards/tweego.git
synced 2025-07-04 21:50:33 -04:00
Initial Bitbucket→GitHub migration commit, based on release v2.0.0.
This commit is contained in:
commit
57e1aa52ff
36 changed files with 5026 additions and 0 deletions
43
user.go
Normal file
43
user.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
Copyright © 2014–2019 Thomas Michael Edwards. All rights reserved.
|
||||
Use of this source code is governed by a Simplified BSD License which
|
||||
can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"os/user"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func userHomeDir() (string, error) {
|
||||
// Prefer the user's `HOME` environment variable.
|
||||
if homeDir := os.Getenv("HOME"); homeDir != "" {
|
||||
return homeDir, nil
|
||||
}
|
||||
|
||||
// Elsewise, use the user's `.HomeDir` info.
|
||||
if curUser, err := user.Current(); err == nil && curUser.HomeDir != "" {
|
||||
return curUser.HomeDir, nil
|
||||
}
|
||||
|
||||
// Failovers for Windows, though they should be unnecessary in Go ≥v1.7.
|
||||
if runtime.GOOS == "windows" {
|
||||
// Prefer the user's `USERPROFILE` environment variable.
|
||||
if homeDir := os.Getenv("USERPROFILE"); homeDir != "" {
|
||||
return homeDir, nil
|
||||
}
|
||||
|
||||
// Elsewise, use the user's `HOMEDRIVE` and `HOMEPATH` environment variables.
|
||||
homeDrive := os.Getenv("HOMEDRIVE")
|
||||
homePath := os.Getenv("HOMEPATH")
|
||||
if homeDrive != "" && homePath != "" {
|
||||
return homeDrive + homePath, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", errors.New("Cannot find user home directory.")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue