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"
26            );
27        }
28    };
29
30    if client.verify_connection().await.is_err() {
31        bail!(
32            "Docker is not responding.\n\n\
33            Start or restart the Docker daemon, then try again.\n\n\
34            Linux:  sudo systemctl start docker\n\
35            Linux:  sudo systemctl restart docker\n\
36            macOS:  Open Docker Desktop\n\
37            Check:  docker ps\n\
38            Check:  ls -l /var/run/docker.sock (Linux default)\n\
39            Check:  your user has access to the Docker socket\n\
40            Fix:    Linux: sudo usermod -aG docker $USER"
41        );
42    }
43
44    Ok(())
45}
46
47/// Verify TTY is available for interactive prompts
48///
49/// Returns error with guidance if stdin is not a terminal.
50pub fn verify_tty() -> Result<()> {
51    if !std::io::stdin().is_terminal() {
52        bail!(
53            "No TTY detected. Use occ config set or provide config file.\n\n\
54            For non-interactive setup, pre-set auth credentials then run commands:\n  \
55            occ config set username <user>\n  \
56            occ config set password  # will prompt\n\n\
57            Or edit the config file directly:\n  \
58            ~/.config/opencode-cloud/config.json"
59        );
60    }
61    Ok(())
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    // Note: These tests are intentionally minimal as they depend on environment state
69    // (Docker running, TTY availability) that varies between CI and local development.
70
71    #[test]
72    fn test_verify_tty_runs() {
73        // Just verify the function compiles and returns a Result
74        // Actual TTY check depends on test environment
75        let _ = verify_tty();
76    }
77}