|
| 1 | +//! Dependency Verification |
| 2 | +//! |
| 3 | +//! This module provides dependency verification functionality for applications |
| 4 | +//! that need to ensure required system dependencies are installed before execution. |
| 5 | +//! It checks dependencies and provides clear error messages with installation guidance. |
| 6 | +
|
| 7 | +use thiserror::Error; |
| 8 | +use tracing::{error, info}; |
| 9 | + |
| 10 | +use crate::{Dependency, DependencyManager, DetectionError}; |
| 11 | + |
| 12 | +// ============================================================================ |
| 13 | +// PUBLIC API - Main Functions |
| 14 | +// ============================================================================ |
| 15 | + |
| 16 | +/// Verify that all required dependencies are installed |
| 17 | +/// |
| 18 | +/// This function checks each dependency in the provided list and reports |
| 19 | +/// clear errors if any are missing. It does NOT attempt automatic installation, |
| 20 | +/// allowing the user to control when and how dependencies are installed. |
| 21 | +/// |
| 22 | +/// # Errors |
| 23 | +/// |
| 24 | +/// Returns an error if: |
| 25 | +/// - One or more dependencies are not installed |
| 26 | +/// - Detection system fails to check a dependency |
| 27 | +/// |
| 28 | +/// # Example |
| 29 | +/// |
| 30 | +/// ```no_run |
| 31 | +/// use torrust_dependency_installer::{Dependency, verify_dependencies}; |
| 32 | +/// |
| 33 | +/// // Verify all dependencies for a full workflow |
| 34 | +/// let deps = &[Dependency::OpenTofu, Dependency::Ansible, Dependency::Lxd]; |
| 35 | +/// verify_dependencies(deps)?; |
| 36 | +/// |
| 37 | +/// // Verify only specific dependencies |
| 38 | +/// let deps = &[Dependency::Ansible]; |
| 39 | +/// verify_dependencies(deps)?; |
| 40 | +/// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 41 | +/// ``` |
| 42 | +pub fn verify_dependencies(dependencies: &[Dependency]) -> Result<(), DependencyVerificationError> { |
| 43 | + let manager = DependencyManager::new(); |
| 44 | + let mut missing = Vec::new(); |
| 45 | + |
| 46 | + info!("Verifying dependencies"); |
| 47 | + |
| 48 | + for &dep in dependencies { |
| 49 | + let detector = manager.get_detector(dep); |
| 50 | + |
| 51 | + match detector.is_installed() { |
| 52 | + Ok(true) => { |
| 53 | + info!( |
| 54 | + dependency = detector.name(), |
| 55 | + status = "installed", |
| 56 | + "Dependency check passed" |
| 57 | + ); |
| 58 | + } |
| 59 | + Ok(false) => { |
| 60 | + error!( |
| 61 | + dependency = detector.name(), |
| 62 | + status = "not installed", |
| 63 | + "Dependency check failed" |
| 64 | + ); |
| 65 | + missing.push(dep); |
| 66 | + } |
| 67 | + Err(e) => { |
| 68 | + error!( |
| 69 | + dependency = detector.name(), |
| 70 | + error = %e, |
| 71 | + "Failed to detect dependency" |
| 72 | + ); |
| 73 | + return Err(DependencyVerificationError::DetectionFailed { |
| 74 | + dependency: dep, |
| 75 | + source: e, |
| 76 | + }); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if missing.is_empty() { |
| 82 | + info!("All required dependencies are available"); |
| 83 | + Ok(()) |
| 84 | + } else { |
| 85 | + Err(DependencyVerificationError::MissingDependencies { |
| 86 | + dependencies: missing, |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// ============================================================================ |
| 92 | +// ERROR TYPES - Secondary Concerns |
| 93 | +// ============================================================================ |
| 94 | + |
| 95 | +/// Errors that can occur during dependency verification |
| 96 | +#[derive(Debug, Error)] |
| 97 | +pub enum DependencyVerificationError { |
| 98 | + /// One or more required dependencies are not installed |
| 99 | + #[error("Missing required dependencies: {}", format_dependency_list(.dependencies))] |
| 100 | + MissingDependencies { |
| 101 | + /// List of missing dependencies |
| 102 | + dependencies: Vec<Dependency>, |
| 103 | + }, |
| 104 | + |
| 105 | + /// Failed to detect if a dependency is installed |
| 106 | + #[error("Failed to detect dependency '{dependency}': {source}")] |
| 107 | + DetectionFailed { |
| 108 | + /// The dependency that could not be detected |
| 109 | + dependency: Dependency, |
| 110 | + /// The underlying detection error |
| 111 | + #[source] |
| 112 | + source: DetectionError, |
| 113 | + }, |
| 114 | +} |
| 115 | + |
| 116 | +impl DependencyVerificationError { |
| 117 | + /// Get actionable error message with installation instructions |
| 118 | + #[must_use] |
| 119 | + pub fn actionable_message(&self) -> String { |
| 120 | + match self { |
| 121 | + Self::MissingDependencies { dependencies } => { |
| 122 | + let dep_list = format_dependency_list(dependencies); |
| 123 | + format!( |
| 124 | + "Missing required dependencies: {dep_list}\n\n\ |
| 125 | + To install all dependencies automatically, run:\n \ |
| 126 | + cargo run --bin dependency-installer install\n\n\ |
| 127 | + Or install specific dependencies:\n \ |
| 128 | + cargo run --bin dependency-installer install <dependency>\n\n\ |
| 129 | + For manual installation instructions, see:\n \ |
| 130 | + https://github.com/torrust/torrust-tracker-deployer/blob/main/packages/dependency-installer/README.md" |
| 131 | + ) |
| 132 | + } |
| 133 | + Self::DetectionFailed { dependency, source } => { |
| 134 | + format!( |
| 135 | + "Failed to detect dependency '{dependency}': {source}\n\n\ |
| 136 | + This may indicate a system configuration issue.\n\ |
| 137 | + Please ensure the dependency detection tool is working correctly." |
| 138 | + ) |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// ============================================================================ |
| 145 | +// PRIVATE - Helper Functions |
| 146 | +// ============================================================================ |
| 147 | + |
| 148 | +fn format_dependency_list(dependencies: &[Dependency]) -> String { |
| 149 | + dependencies |
| 150 | + .iter() |
| 151 | + .map(ToString::to_string) |
| 152 | + .collect::<Vec<_>>() |
| 153 | + .join(", ") |
| 154 | +} |
| 155 | + |
| 156 | +// NOTE: No unit tests here - verification logic is tested via Docker-based |
| 157 | +// integration tests in packages/dependency-installer/tests/ which provide |
| 158 | +// reliable, controlled environments. Unit tests would be environment-dependent |
| 159 | +// and unreliable across different CI/dev setups. |
0 commit comments