Skip to content

Commit

Permalink
Co-authored cleanups
Browse files Browse the repository at this point in the history
Signed-off-by: Colin Walters <[email protected]>
  • Loading branch information
cgwalters committed Sep 4, 2024
1 parent 9cd3ced commit 3e7658b
Showing 1 changed file with 48 additions and 45 deletions.
93 changes: 48 additions & 45 deletions lib/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ pub(crate) async fn status(opts: super::cli::StatusOpts) -> Result<()> {
let legacy_opt = if opts.json {
OutputFormat::Json
} else {
if std::io::stdout().is_terminal() {
if std::io::stdout().is_terminal() {
OutputFormat::HumanReadable
} else {
OutputFormat::Yaml
Expand All @@ -317,54 +317,50 @@ pub(crate) async fn status(opts: super::cli::StatusOpts) -> Result<()> {
match format {
OutputFormat::Json => serde_json::to_writer(&mut out, &host).map_err(anyhow::Error::new),
OutputFormat::Yaml => serde_yaml::to_writer(&mut out, &host).map_err(anyhow::Error::new),
OutputFormat::HumanReadable => human_readable_output(&mut out, &host),
OutputFormat::HumanReadable => human_readable_output(&mut out, &host),
}
.context("Writing to stdout")?;

Ok(())
}

#[derive(Default)]
struct ImageOpts {
version: String,
timestamp: String,
transport: String,
digest: String,
}

fn human_readable_output(mut out: impl Write, host: &Host) -> Result<()> {
for (status_string, status) in [
("staged", &host.status.staged),
("booted", &host.status.booted),
("rollback", &host.status.rollback),
] {
if let Some(host_status) = status {
let mut image_opts: ImageOpts = ImageOpts::default();
if let Some(image) = &host_status.image {
out.write_all(format!("Current {} image: {}", status_string, image.image.image).as_bytes())?;

if let Some(version) = &image.version {
image_opts.version = version.to_owned();
} else {
image_opts.version = ("No image version defined").to_owned();
}

if let Some(timestamp) = &image.timestamp {
image_opts.timestamp = timestamp.to_string();
} else {
image_opts.timestamp = ("No timestamp present").to_owned();
}
image_opts.transport = image.image.transport.clone();
image_opts.digest = image.image_digest.clone();

out.write_all(format!("
Image version: {} ({})
Image transport: {}
Image digest: {}\n",
image_opts.version, image_opts.timestamp, image_opts.transport, image_opts.digest).as_bytes())?;

out.write_all(
format!("Current {} image: {}", status_string, image.image.image).as_bytes(),
)?;

let version = image
.version
.as_deref()
.unwrap_or("No image version defined");
let timestamp = image
.timestamp
.as_ref()
.map(|t| t.to_string())
.unwrap_or_else(|| "No timestamp present".to_owned());
let transport = &image.image.transport;
let digest = &image.image_digest;

out.write_all(
format!(
"
Image version: {version} ({timestamp})
Image transport: {transport}
Image digest: {digest}\n"
)
.as_bytes(),
)?;
} else {
out.write_all(format!("No {} image present, checksum defined\n", status_string).as_bytes())?;
out.write_all(
format!("No {} image present, checksum defined\n", status_string).as_bytes(),
)?;
}
} else {
out.write_all(format!("No {} image present\n", status_string).as_bytes())?;
Expand All @@ -382,15 +378,18 @@ fn test_human_readable_base_spec() {
human_readable_output(&mut w, &host).unwrap();
let w = String::from_utf8(w).unwrap();
dbg!(&w);
assert!(w.contains("Current staged image: quay.io/example/someimage:latest"));
assert!(w.contains("Image version: nightly (2023-10-14 19:22:15 UTC)"));
assert!(w.contains("Image transport: registry"));
assert!(w.contains("Image digest: sha256:16dc2b6256b4ff0d2ec18d2dbfb06d117904010c8cf9732cdb022818cf7a7566"));
assert!(w.contains("Current booted image: quay.io/example/someimage:latest"));
assert!(w.contains("Image version: nightly (2023-09-30 19:22:16 UTC)"));
assert!(w.contains("Image transport: registry"));
assert!(w.contains("Image digest: sha256:736b359467c9437c1ac915acaae952aad854e07eb4a16a94999a48af08c83c34"));
assert!(w.contains("No rollback image present"));
let expected = indoc::indoc! { r"
Current staged image: quay.io/example/someimage:latest
Image version: nightly (2023-10-14 19:22:15 UTC)
Image transport: registry
Image digest: sha256:16dc2b6256b4ff0d2ec18d2dbfb06d117904010c8cf9732cdb022818cf7a7566
Current booted image: quay.io/example/someimage:latest
Image version: nightly (2023-09-30 19:22:16 UTC)
Image transport: registry
Image digest: sha256:736b359467c9437c1ac915acaae952aad854e07eb4a16a94999a48af08c83c34
No rollback image present
"};
similar_asserts::assert_eq!(w, expected);
}

#[test]
Expand Down Expand Up @@ -420,7 +419,9 @@ fn test_human_readable_staged_spec() {
assert!(w.contains("Current staged image: quay.io/centos-bootc/centos-bootc:stream9"));
assert!(w.contains("Image version: stream9.20240807.0 (No timestamp present)"));
assert!(w.contains("Image transport: registry"));
assert!(w.contains("Image digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38"));
assert!(w.contains(
"Image digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38"
));
assert!(w.contains("No booted image present, checksum defined"));
assert!(w.contains("No rollback image present"));
}
Expand All @@ -438,7 +439,9 @@ fn test_human_readable_booted_spec() {
assert!(w.contains("Current booted image: quay.io/centos-bootc/centos-bootc:stream9"));
assert!(w.contains("Image version: stream9.20240807.0 (No timestamp present)"));
assert!(w.contains("Image transport: registry"));
assert!(w.contains("Image digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38"));
assert!(w.contains(
"Image digest: sha256:47e5ed613a970b6574bfa954ab25bb6e85656552899aa518b5961d9645102b38"
));
assert!(w.contains("No rollback image present"));
}

Expand Down

0 comments on commit 3e7658b

Please sign in to comment.