opencode-cloud 25.1.3

CLI for managing opencode as a persistent cloud service
Documentation
//! Wizard prechecks
//!
//! Validates environment before running the setup wizard.

use anyhow::{Result, bail};
use opencode_cloud_core::docker::DockerClient;
use std::io::IsTerminal;

/// Verify Docker is available and running
///
/// Attempts to connect to Docker and verify the connection.
/// Returns actionable error if Docker is not available.
pub async fn verify_docker_available() -> Result<()> {
    let client = match DockerClient::new() {
        Ok(c) => c,
        Err(_) => {
            bail!(
                "Docker is not available.\n\n\
                Make sure Docker is installed and the daemon is running.\n\n\
                Linux:  sudo systemctl start docker\n\
                macOS:  Open Docker Desktop\n\
                Check:  docker ps\n\
                Check:  ls -l /var/run/docker.sock (Linux default)\n\
                Check:  your user has access to the Docker socket\n\
                Fix:    Linux: sudo usermod -aG docker $USER\n\
                Note:   Restart your session or reboot after installing Docker"
            );
        }
    };

    if client.verify_connection().await.is_err() {
        bail!(
            "Docker is not responding.\n\n\
            Start or restart the Docker daemon, then try again.\n\n\
            Linux:  sudo systemctl start docker\n\
            Linux:  sudo systemctl restart docker\n\
            macOS:  Open Docker Desktop\n\
            Check:  docker ps\n\
            Check:  ls -l /var/run/docker.sock (Linux default)\n\
            Check:  your user has access to the Docker socket\n\
            Fix:    Linux: sudo usermod -aG docker $USER\n\
            Note:   Restart your session or reboot after installing Docker"
        );
    }

    Ok(())
}

/// Verify TTY is available for interactive prompts
///
/// Returns error with guidance if stdin is not a terminal.
pub fn verify_tty() -> Result<()> {
    if !std::io::stdin().is_terminal() {
        bail!(
            "No TTY detected. Use occ config set or provide config file.\n\n\
            For non-interactive setup:\n  \
            occ setup --bootstrap\n\n\
            Then start the service and complete IOTP + passkey onboarding in the web login page.\n\n\
            Or edit the config file directly:\n  \
            ~/.config/opencode-cloud/config.json"
        );
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    // Note: These tests are intentionally minimal as they depend on environment state
    // (Docker running, TTY availability) that varies between CI and local development.

    #[test]
    fn test_verify_tty_runs() {
        // Just verify the function compiles and returns a Result
        // Actual TTY check depends on test environment
        let _ = verify_tty();
    }
}