Skip to main content

isothrum_machine/
tools.rs

1//! The command-line tools a machine check reports: whether each answers its version probe, and the
2//! version it printed. A tool that can't be started, fails its probe or runs out of the probe's
3//! time reads as not installed, with no version: the check goes on, it never fails for one tool.
4
5use serde::{Deserialize, Serialize};
6
7use crate::context::Context;
8use crate::host::{Packages, usual_packages};
9
10pub mod ovftool;
11
12/// A command-line tool as the check found it.
13#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
14#[cfg_attr(any(test, feature = "ts-rs"), derive(ts_rs::TS))]
15#[serde(rename_all = "camelCase")]
16pub struct Tool {
17    /// Whether it answered its version probe.
18    pub installed: bool,
19    /// The first line of what it printed, when it answered.
20    pub version: Option<String>,
21}
22
23impl Tool {
24    /// A tool that isn't installed.
25    pub const MISSING: Tool = Tool {
26        installed: false,
27        version: None,
28    };
29
30    /// An installed tool, at `version` when it said one.
31    #[must_use]
32    pub fn installed(version: Option<String>) -> Tool {
33        Tool { installed: true, version }
34    }
35}
36
37/// The version probes of the tools a check reports, each a program and the arguments it answers
38/// its version with.
39pub mod probes {
40    /// The Docker CLI.
41    pub const DOCKER: (&str, &[&str]) = ("docker", &["--version"]);
42    /// Docker Compose, the Docker CLI's plugin.
43    pub const COMPOSE: (&str, &[&str]) = ("docker", &["compose", "version", "--short"]);
44    /// Vagrant.
45    pub const VAGRANT: (&str, &[&str]) = ("vagrant", &["--version"]);
46    /// Terraform.
47    pub const TERRAFORM: (&str, &[&str]) = ("terraform", &["version"]);
48    /// The AWS CLI.
49    pub const AWS: (&str, &[&str]) = ("aws", &["--version"]);
50    /// The Azure CLI: `az version` prints JSON, its version read from it ([`super::azure_cli`]).
51    /// A `--query` would need quotes, which can't pass through `cmd.exe` (`az` is a `.cmd`).
52    pub const AZURE: (&str, &[&str]) = ("az", &["version", "--output", "json"]);
53    /// The Google Cloud CLI.
54    pub const GCLOUD: (&str, &[&str]) = ("gcloud", &["--version"]);
55}
56
57/// `program args`, a version probe: installed when it exits 0, its version the first line it
58/// printed ([`first_line`]).
59pub async fn version(cx: &Context, (program, args): (&str, &[&str])) -> Tool {
60    match cx.read_ok(program, args).await {
61        Ok(out) => Tool::installed(first_line(&out)),
62        Err(_) => Tool::MISSING,
63    }
64}
65
66/// The first non-blank line, trimmed: a tool whose output opens with a blank line (a banner's
67/// spacing) or ends its lines with `\r\n` reads with its version, not an empty one.
68#[must_use]
69pub fn first_line(out: &str) -> Option<String> {
70    out.lines().map(str::trim).find(|l| !l.is_empty()).map(str::to_owned)
71}
72
73/// The Azure CLI ([`probes::AZURE`]), its version the `azure-cli` entry of the JSON it prints.
74pub async fn azure_cli(cx: &Context) -> Tool {
75    let (program, args) = probes::AZURE;
76    match cx.read_ok(program, args).await {
77        Ok(out) => Tool::installed(isothrum_runner::azure_cli_version(&out)),
78        Err(_) => Tool::MISSING,
79    }
80}
81
82/// The cloud providers' command-line tools.
83#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
84#[cfg_attr(any(test, feature = "ts-rs"), derive(ts_rs::TS))]
85#[serde(rename_all = "camelCase")]
86pub struct CloudClis {
87    /// The AWS CLI (`aws`).
88    pub aws: Tool,
89    /// The Azure CLI (`az`).
90    pub azure: Tool,
91    /// The Google Cloud CLI (`gcloud`).
92    pub gcloud: Tool,
93}
94
95/// Which cloud CLIs answer here, asked side by side.
96pub async fn cloud_clis(cx: &Context) -> CloudClis {
97    let (aws, azure, gcloud) = tokio::join!(version(cx, probes::AWS), azure_cli(cx), version(cx, probes::GCLOUD));
98    CloudClis { aws, azure, gcloud }
99}
100
101/// The package manager a setup installs through, as the check found it.
102#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
103#[cfg_attr(any(test, feature = "ts-rs"), derive(ts_rs::TS))]
104#[serde(rename_all = "camelCase")]
105pub struct PackageManager {
106    /// Its name: `Homebrew`, `winget`, `apt` or `dnf`.
107    pub name: String,
108    /// Whether it answered its version probe.
109    pub installed: bool,
110    /// The first line of its version (`Homebrew 4.6.3`), when it said one.
111    pub version: Option<String>,
112}
113
114/// The context's host's package manager ([`crate::Host::packages`]) and whether it answers its
115/// `--version`; when the host has none, the one its OS usually has ([`usual_packages`]), not
116/// installed.
117pub async fn package_manager(cx: &Context) -> PackageManager {
118    let answer = match cx.host.packages {
119        Some(packages) => cx.read_ok(packages.program(), &["--version"]).await.ok(),
120        None => None,
121    };
122    package_manager_of(cx.host.packages, cx.host.os, answer.as_deref())
123}
124
125/// The package manager `packages` (else `os`'s usual one), given its `--version` output when it
126/// answered.
127fn package_manager_of(packages: Option<Packages>, os: crate::HostOs, answer: Option<&str>) -> PackageManager {
128    PackageManager {
129        name: packages.map_or(usual_packages(os), Packages::name).to_owned(),
130        installed: answer.is_some(),
131        version: answer.and_then(first_line),
132    }
133}
134
135#[cfg(test)]
136mod tests;