2022-02-18 23:13:32 -06:00
|
|
|
package usershell
|
|
|
|
|
|
2023-07-11 20:27:50 +03:00
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
)
|
2022-02-18 23:13:32 -06:00
|
|
|
|
|
|
|
|
// Get returns the $SHELL environment variable.
|
2025-02-26 09:03:27 +00:00
|
|
|
// Deprecated: use SystemEnvInfo.UserShell instead.
|
2023-07-11 20:27:50 +03:00
|
|
|
func Get(username string) (string, error) {
|
|
|
|
|
// This command will output "UserShell: /bin/zsh" if successful, we
|
|
|
|
|
// can ignore the error since we have fallback behavior.
|
2023-12-12 17:02:28 +04:00
|
|
|
if !filepath.IsLocal(username) {
|
|
|
|
|
return "", xerrors.Errorf("username is nonlocal path: %s", username)
|
|
|
|
|
}
|
|
|
|
|
//nolint: gosec // input checked above
|
2025-03-03 04:47:42 +01:00
|
|
|
out, _ := exec.Command("dscl", ".", "-read", filepath.Join("/Users", username), "UserShell").Output() //nolint:gocritic
|
2023-07-11 20:27:50 +03:00
|
|
|
s, ok := strings.CutPrefix(string(out), "UserShell: ")
|
|
|
|
|
if ok {
|
|
|
|
|
return strings.TrimSpace(s), nil
|
|
|
|
|
}
|
|
|
|
|
if s = os.Getenv("SHELL"); s != "" {
|
|
|
|
|
return s, nil
|
|
|
|
|
}
|
|
|
|
|
return "", xerrors.Errorf("shell for user %q not found via dscl or in $SHELL", username)
|
2022-02-18 23:13:32 -06:00
|
|
|
}
|