Installs packages with caching support. This function provides a common pattern for installing tools on Ubuntu/Debian systems with automatic caching to speed up subsequent installations (e.g., in CI environments). # Arguments `system_info` - System information to determine compatibility `setup_cache_dir` - Optional directory to restore from/save to cache `is_installed` - Function that checks if
(
system_info: &SystemInfo,
setup_cache_dir: Option<&Path>,
is_installed: F,
install_packages: I,
)
| 48 | /// ).await?; |
| 49 | /// ``` |
| 50 | pub async fn install_cached<F, I, Fut>( |
| 51 | system_info: &SystemInfo, |
| 52 | setup_cache_dir: Option<&Path>, |
| 53 | is_installed: F, |
| 54 | install_packages: I, |
| 55 | ) -> Result<()> |
| 56 | where |
| 57 | F: Fn() -> bool, |
| 58 | I: FnOnce() -> Fut, |
| 59 | Fut: std::future::Future<Output = Result<Vec<String>>>, |
| 60 | { |
| 61 | if is_installed() { |
| 62 | debug!("Tool already installed, skipping installation"); |
| 63 | return Ok(()); |
| 64 | } |
| 65 | |
| 66 | // Try to restore from cache first |
| 67 | if let Some(cache_dir) = setup_cache_dir { |
| 68 | restore_from_cache(system_info, cache_dir)?; |
| 69 | |
| 70 | if is_installed() { |
| 71 | info!("Tool has been successfully restored from cache"); |
| 72 | return Ok(()); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Install and get the package names for caching |
| 77 | let cache_packages = install_packages().await?; |
| 78 | |
| 79 | info!("Installation completed successfully"); |
| 80 | |
| 81 | // Save to cache after successful installation |
| 82 | if let Some(cache_dir) = setup_cache_dir { |
| 83 | let cache_refs: Vec<&str> = cache_packages.iter().map(|s| s.as_str()).collect(); |
| 84 | save_to_cache(system_info, cache_dir, &cache_refs)?; |
| 85 | } |
| 86 | |
| 87 | Ok(()) |
| 88 | } |
| 89 | |
| 90 | /// Returns whether a package is currently installed according to `dpkg`. |
| 91 | pub fn is_package_installed(package: &str) -> bool { |
no test coverage detected