Skip to main content

opencode_cloud/wizard/
prechecks.rs

1//! Wizard prechecks
2//!
3//! Validates environment before running the setup wizard.
4
5use anyhow::{Result, bail};
6use opencode_cloud_core::docker::DockerClient;
7use std::io::IsTerminal;
8
9/// Verify Docker is available and running
10///
11/// Attempts to connect to Docker and verify the connection.
12/// Returns actionable error if Docker is not available.
13pub async fn verify_docker_available() -> Result<()> {
14    let client = match DockerClient::new() {
15        Ok(c) => c,
16        Err(_) => {
17            bail!(
18                "Docker is not available.\n\n\
19                Make sure Docker is installed and the daemon is running.\n\n\
20                Linux:  sudo systemctl start docker\n\
21                macOS:  Open Docker Desktop\n\
22                Check:  docker ps\n\
23                Check:  ls -l /var/run/docker.sock (Linux default)\n\
24                Check:  your user has access to the Docker socket\n\
25                Fix:    Linux: sudo usermod -aG docker $USER\n\
26                Note:   Restart your session or reboot after installing Docker"
27            );
28        }
29    };
30
31    if client.verify_connection().await.is_err() {
32        bail!(
33            "Docker is not responding.\n\n\
34            Start or restart the Docker daemon, then try again.\n\n\
35            Linux:  sudo systemctl start docker\n\
36            Linux:  sudo systemctl restart docker\n\
37            macOS:  Open Docker Desktop\n\
38            Check:  docker ps\n\
39            Check:  ls -l /var/run/docker.sock (Linux default)\n\
40            Check:  your user has access to the Docker socket\n\
41            Fix:    Linux: sudo usermod -aG docker $USER\n\
42            Note:   Restart your session or reboot after installing Docker"
43        );
44    }
45
46    Ok(())
47}
48
49/// Verify TTY is available for interactive prompts
50///
51/// Returns error with guidance if stdin is not a terminal.
52pub fn verify_tty() -> Result<()> {
53    if !std::io::stdin().is_terminal() {
54        bail!(
55            "No TTY detected. Use occ config set or provide config file.\n\n\
56            For non-interactive setup, pre-set auth credentials then run commands:\n  \
57            occ config set username <user>\n  \
58            occ config set password  # will prompt\n\n\
59            Or edit the config file directly:\n  \
60            ~/.config/opencode-cloud/config.json"
61        );
62    }
63    Ok(())
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    // Note: These tests are intentionally minimal as they depend on environment state
71    // (Docker running, TTY availability) that varies between CI and local development.
72
73    #[test]
74    fn test_verify_tty_runs() {
75        // Just verify the function compiles and returns a Result
76        // Actual TTY check depends on test environment
77        let _ = verify_tty();
78    }
79}