Debian Codegraph @

shell_darwin.go

raw symbols SCIP
1 //go:build darwin
2 // +build darwin
3
4 package shell
5
6 import (
7 "os"
8 "os/exec"
9 "os/user"
10 "regexp"
11 )
12
13 //nolint:gochecknoglobals
14 var dsclUserShellRegexp = regexp.MustCompile(`\AUserShell:\s+(.*?)\s*\z`)
15
16 // CurrentUserShell returns the current user's shell.
17 func CurrentUserShell() (string, bool) {
18 // If the SHELL environment variable is set, use it.
19 if shell, ok := os.LookupEnv("SHELL"); ok {
20 return shell, true
21 }
22
23 // Try to get the current user. If we can't then fallback to the default
24 // shell.
25 u, err := user.Current()
26 if err != nil {
27 return DefaultShell(), false
28 }
29
30 // If getpwnam_r is available, use it.
31 if shell, ok := cgoGetUserShell(u.Username); ok {
32 return shell, true
33 }
34
35 // If dscl is available, use it.
36 //nolint:gosec
37 if output, err := exec.Command("dscl", ".", "-read", u.HomeDir, "UserShell").Output(); err == nil {
38 if m := dsclUserShellRegexp.FindSubmatch(output); m != nil {
39 return string(m[1]), true
40 }
41 }
42
43 // Fallback to the default shell.
44 return DefaultShell(), false
45 }