Ctrl K
ComunidadGuías…

agent/usershell/usershell_darwin.go

Rama main · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
package usershell

import (
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"golang.org/x/xerrors"
)

// get resolves the user's shell via dscl, falling back to $SHELL. It
// backs SystemEnvInfo.Shell. Callers resolve the shell through an
// EnvInfoer.
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.
	if !filepath.IsLocal(username) {
		return "", xerrors.Errorf("username is nonlocal path: %s", username)
	}
	//nolint: gosec // input checked above
	out, _ := exec.Command("dscl", ".", "-read", filepath.Join("/Users", username), "UserShell").Output() //nolint:gocritic
	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)
}