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