-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshell_darwin.go
42 lines (34 loc) · 1000 Bytes
/
shell_darwin.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
//go:build darwin
package shell
import (
"os"
"os/exec"
"os/user"
"regexp"
)
var dsclUserShellRegexp = regexp.MustCompile(`\AUserShell:\s+(.*?)\s*\z`) //nolint:gochecknoglobals
// CurrentUserShell returns the current user's shell.
func CurrentUserShell() (string, bool) {
// If the SHELL environment variable is set, use it.
if shell, ok := os.LookupEnv("SHELL"); ok {
return shell, true
}
// Try to get the current user. If we can't then fallback to the default
// shell.
u, err := user.Current()
if err != nil {
return DefaultShell(), false
}
// If getpwnam_r is available, use it.
if shell, ok := cgoGetUserShell(u.Username); ok {
return shell, true
}
// If dscl is available, use it.
if output, err := exec.Command("dscl", ".", "-read", u.HomeDir, "UserShell").Output(); err == nil { //nolint:gosec
if m := dsclUserShellRegexp.FindSubmatch(output); m != nil {
return string(m[1]), true
}
}
// Fallback to the default shell.
return DefaultShell(), false
}