package cmd import ( "errors" "fmt" "os" "runtime" "github.com/warrenronsiek/ctask/internal/shell" ) // isTTYCheck is the test seam for terminal detection. Tests override this // package-level variable to control the TTY refusal path without depending // on the real process's stdin/stdout state. Production callers go through // defaultIsTTYCheck. var isTTYCheck = defaultIsTTYCheck func defaultIsTTYCheck() bool { return shell.IsTTY(os.Stdin) && shell.IsTTY(os.Stdout) } // preflightPersistentEntry validates the host environment supports // tmux-based persistent mode and returns the validated tmux binary path // for callers to pass through `session.LaunchOpts.TmuxPath`. // // Order of checks: // // 1. Native Windows refusal — tmux is not supported; recommend WSL. // 2. Nested tmux refusal — `$TMUX` is set in the parent process. // 3. Non-TTY refusal — stdin or stdout is not a terminal. // 4. shell.LookupTmux — handles ErrTmuxNotFound and ErrTmuxTooOld // with platform-aware install hints. // // commandName ("new", "resume", "last", "open", "attach") is rendered into // the bypass hint so each command tells the user the right form. func preflightPersistentEntry(commandName string) (string, error) { bypass := " ctask " + commandName + " --direct" if runtime.GOOS == "windows" && os.Getenv("WSL_DISTRO_NAME") == "" { return "", fmt.Errorf( "ctask persistent mode requires tmux, which is not supported on native Windows.\n\n"+ "Recommended:\n Run ctask from WSL and install tmux there:\n sudo apt install tmux\n\n"+ "Or bypass persistent mode:\n%s", bypass) } if os.Getenv("TMUX") != "" { return "", fmt.Errorf( "ctask persistent mode cannot attach while already inside tmux.\n\n"+ "Run ctask from outside tmux, or bypass persistent mode:\n%s", bypass) } if !isTTYCheck() { return "", fmt.Errorf( "ctask persistent mode requires an interactive terminal.\n\n"+ "Over SSH, use:\n ssh -t ctask %s \n\n"+ "Or bypass persistent mode:\n%s", commandName, bypass) } tmuxPath, ver, err := shell.LookupTmux() if err != nil { if errors.Is(err, shell.ErrTmuxNotFound) { return "", fmt.Errorf( "ctask is configured for persistent sessions, but tmux is not installed.\n\n"+ "Install tmux:\n"+ " Debian/Ubuntu/WSL: sudo apt install tmux\n"+ " macOS: brew install tmux\n"+ " Arch: sudo pacman -S tmux\n"+ " Fedora: sudo dnf install tmux\n\n"+ "Or bypass persistent mode for this command:\n%s\n\n"+ "To disable persistent mode:\n unset CTASK_SESSION_MODE", bypass) } if errors.Is(err, shell.ErrTmuxTooOld) { raw := ver.Raw if raw == "" { raw = "unknown version" } return "", fmt.Errorf( "ctask persistent mode requires tmux 3.0 or newer (found: %s).\n\n"+ "Update tmux:\n"+ " Debian/Ubuntu/WSL: sudo apt install tmux (Debian 10+ ships 2.8; consider backports or a newer release)\n"+ " macOS: brew upgrade tmux\n"+ " Arch: sudo pacman -Syu tmux\n"+ " Fedora: sudo dnf upgrade tmux\n\n"+ "Or bypass persistent mode for this command:\n%s", raw, bypass) } return "", err } return tmuxPath, nil }