Skip to main content

auths_cli/adapters/
system_diagnostic.rs

1//! POSIX-based diagnostic adapter — subprocess calls live here, nowhere else.
2
3use auths_sdk::ports::diagnostics::{
4    CheckResult, CryptoDiagnosticProvider, DiagnosticError, GitDiagnosticProvider,
5};
6use std::process::Command;
7
8/// Production adapter that shells out to system binaries.
9pub struct PosixDiagnosticAdapter;
10
11impl GitDiagnosticProvider for PosixDiagnosticAdapter {
12    fn check_git_version(&self) -> Result<CheckResult, DiagnosticError> {
13        let output = Command::new("git").arg("--version").output();
14        let (passed, message) = match output {
15            Ok(out) if out.status.success() => {
16                let version = String::from_utf8_lossy(&out.stdout).trim().to_string();
17                (true, Some(version))
18            }
19            _ => (false, Some("git command not found on PATH".to_string())),
20        };
21        Ok(CheckResult {
22            name: "Git installed".to_string(),
23            passed,
24            message,
25            config_issues: vec![],
26        })
27    }
28
29    fn get_git_config(&self, key: &str) -> Result<Option<String>, DiagnosticError> {
30        let output = Command::new("git")
31            .args(["config", "--global", "--get", key])
32            .output()
33            .map_err(|e| DiagnosticError::ExecutionFailed(e.to_string()))?;
34
35        if output.status.success() {
36            Ok(String::from_utf8(output.stdout)
37                .ok()
38                .map(|s| s.trim().to_string()))
39        } else {
40            Ok(None)
41        }
42    }
43}
44
45impl CryptoDiagnosticProvider for PosixDiagnosticAdapter {
46    fn check_ssh_keygen_available(&self) -> Result<CheckResult, DiagnosticError> {
47        let output = Command::new("ssh-keygen").arg("-V").output();
48        let (passed, message) = match output {
49            Ok(out) if out.status.success() => (true, Some("ssh-keygen found on PATH".to_string())),
50            _ => (
51                false,
52                Some("ssh-keygen command not found on PATH".to_string()),
53            ),
54        };
55        Ok(CheckResult {
56            name: "ssh-keygen installed".to_string(),
57            passed,
58            message,
59            config_issues: vec![],
60        })
61    }
62}