-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetectors.go
98 lines (83 loc) · 2.41 KB
/
detectors.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package is
import (
"os"
"runtime"
"strings"
"os/exec"
)
// Root returns true if current user is 'root' or user is in sudo mode.
//
// For windows it's always false.
func Root() bool {
return Unix() && os.Getuid() == 0
}
// Windows returns true for Microsoft Windows Platform.
func Windows() bool {
return runtime.GOOS == "windows"
}
// WindowsWSL return true if running under Windows WSL env.
func WindowsWSL() bool {
out, err := exec.Command("uname", "-r").Output()
if err != nil {
return false
}
return strings.Contains(string(out), "windows_standard")
}
// Unix returns true for Linux, Darwin, and Others Unix-like Platforms.
//
// [NOTE] for unix platforms, more assetions and tests needed.
func Unix() bool {
return runtime.GOOS == "linux" || runtime.GOOS == "darwin" || runtime.GOOS == "unix"
}
// Linux return true for General Linux Distros.
func Linux() bool {
return runtime.GOOS == "linux"
}
// Darwin returns true if running under macOS platform, including both Intel and Silicon.
func Darwin() bool {
return runtime.GOOS == "darwin"
}
// DarwinSilicon returns true if running under Apple Silicon.
func DarwinSilicon() bool {
return runtime.GOOS == "darwin" && runtime.GOARCH == "arm64"
}
// DarwinIntel returns true if running under Apple Intel Machines.
func DarwinIntel() bool {
return runtime.GOOS == "darwin" && runtime.GOARCH == "amd64"
}
// Bash returns true if application is running under a Bash shell.
func Bash() bool {
return os.Getenv("BASH_VERSION") != "" || os.Getenv("BASH") != ""
}
// Zsh returns true if application is running under a Zsh shell.
func Zsh() bool {
return strings.Contains(ShellName(), "/bin/zsh") && os.Getenv("ZSH_NAME") != ""
}
// Fish returns true if application is running under a Fish shell.
func Fish() bool {
return os.Getenv("FISH_VERSION") != "" && strings.Contains(ShellName(), "/bin/fish")
}
// Powershell returns true if application is running under a Windows Powershell shell.
//
// Not testing yet.
func Powershell() bool {
return os.Getenv("PS1") != ""
}
// ShellName returns current SHELL's name.
//
// For Windows, it could be "cmd.exe" or "powershell.exe".
// For Linux or Unix or Darwin, it returns environment variable $SHELL.
// Else it's empty "".
func ShellName() string {
switch runtime.GOOS {
case "windows":
if Powershell() {
return "powershell.exe"
}
return "cmd.exe"
case "linux", "darwin":
return os.Getenv("SHELL")
default:
return ""
}
}