-
Notifications
You must be signed in to change notification settings - Fork 8
/
homedir.go
38 lines (32 loc) · 1.14 KB
/
homedir.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package platform
import (
"fmt"
"github.com/mitchellh/go-homedir"
"github.com/ARM-software/golang-utils/utils/commonerrors"
)
// GetHomeDirectory returns the home directory of a user.
func GetHomeDirectory(username string) (string, error) {
user, err := GetUser(username)
if err != nil {
return GetDefaultHomeDirectory(username)
}
return fetchHomeDirectory(username, user.HomeDir)
}
// GetDefaultHomeDirectory returns the default home directory for a user based on the convention listed in https://en.wikipedia.org/wiki/Home_directory#Default_home_directory_per_operating_system
func GetDefaultHomeDirectory(username string) (string, error) {
if username == "" {
return "", fmt.Errorf("%w: missing username", commonerrors.ErrUndefined)
}
return determineDefaultHomeDirectory(username)
}
func fetchHomeDirectory(username, homedirPath string) (home string, err error) {
home, err = homedir.Expand(homedirPath)
if err == nil {
return
}
home, err = GetDefaultHomeDirectory(username)
if err != nil {
err = fmt.Errorf("%w: could not determine user [%v]'s home directory: %v", commonerrors.ErrUnexpected, username, err.Error())
}
return
}