Check if the given command is installed and its version matches the expected version. Expects the command to support the `--version` flag and return a version string.
(command: &str, expected_version: Version)
| 64 | /// |
| 65 | /// Expects the command to support the `--version` flag and return a version string. |
| 66 | fn is_command_installed(command: &str, expected_version: Version) -> bool { |
| 67 | let is_command_installed = Command::new("which") |
| 68 | .arg(command) |
| 69 | .output() |
| 70 | .is_ok_and(|output| output.status.success()); |
| 71 | |
| 72 | if !is_command_installed { |
| 73 | debug!("{command} is not installed"); |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | let Ok(version_output) = Command::new(command).arg("--version").output() else { |
| 78 | return false; |
| 79 | }; |
| 80 | |
| 81 | if !version_output.status.success() { |
| 82 | debug!( |
| 83 | "Failed to get command version. stderr: {}", |
| 84 | String::from_utf8_lossy(&version_output.stderr) |
| 85 | ); |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | let version_string = String::from_utf8_lossy(&version_output.stdout); |
| 90 | let Ok(version) = versions::parse_from_output(&version_string) else { |
| 91 | return false; |
| 92 | }; |
| 93 | |
| 94 | debug!("Found {command} version: {version}"); |
| 95 | |
| 96 | versions::is_compatible(command, &version, &expected_version) |
| 97 | } |
no test coverage detected