-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.go
50 lines (46 loc) · 918 Bytes
/
system.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
39
40
41
42
43
44
45
46
47
48
49
50
package system
import (
"fmt"
"os"
"os/user"
"runtime"
)
func GetUserHome(operatingSystem string) string {
switch operatingSystem {
case WINDOWS:
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
case LINUX:
home := os.Getenv("XDG_CONFIG_HOME")
if home != "" {
return home
}
case DARWIN:
return os.Getenv("HOME")
}
return os.Getenv("HOME")
}
func GetHostOS() (string, error) {
currentOs := runtime.GOOS
switch currentOs {
case WINDOWS:
return WINDOWS, nil
case DARWIN:
return DARWIN, nil
case LINUX:
return LINUX, nil
case UNIX:
return UNIX, nil
}
return "", fmt.Errorf("unsupported os: %s", currentOs)
}
func GetCurrentUser() (string, string, error) {
user, err := user.Current()
if err != nil {
return "", "", fmt.Errorf("error fetching %s", err)
}
return user.Username, user.HomeDir, nil
}