Skip to content

Commit

Permalink
status: Add explicit --format argument
Browse files Browse the repository at this point in the history
Prep for adding `--format=human` for example. But this
is also useful for anything that explicitly wants to consume
YAML today.

Signed-off-by: Colin Walters <walters@verbum.org>
  • Loading branch information
cgwalters committed Jun 25, 2024
1 parent 969d032 commit 7a79b8d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
15 changes: 15 additions & 0 deletions lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use camino::Utf8PathBuf;
use cap_std_ext::cap_std;
use cap_std_ext::cap_std::fs::Dir;
use clap::Parser;
use clap::ValueEnum;
use fn_error_context::context;
use ostree::gio;
use ostree_container::store::PrepareResult;
Expand Down Expand Up @@ -106,13 +107,26 @@ pub(crate) struct EditOpts {
pub(crate) quiet: bool,
}

#[derive(Debug, Clone, ValueEnum, PartialEq, Eq)]
#[clap(rename_all = "lowercase")]
pub(crate) enum OutputFormat {
/// Output in YAML format.
YAML,
/// Output in JSON format.
JSON,
}

/// Perform an status operation
#[derive(Debug, Parser, PartialEq, Eq)]
pub(crate) struct StatusOpts {
/// Output in JSON format.
#[clap(long)]
pub(crate) json: bool,

/// The output format.
#[clap(long)]
pub(crate) format: Option<OutputFormat>,

/// Only display status for the booted deployment.
#[clap(long)]
pub(crate) booted: bool,
Expand Down Expand Up @@ -772,6 +786,7 @@ fn test_parse_opts() {
Opt::parse_including_static(["bootc", "status"]),
Opt::Status(StatusOpts {
json: false,
format: None,
booted: false
})
));
Expand Down
16 changes: 12 additions & 4 deletions lib/src/status.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::VecDeque;

use crate::cli::OutputFormat;
use crate::spec::{BootEntry, BootOrder, Host, HostSpec, HostStatus, HostType, ImageStatus};
use crate::spec::{ImageReference, ImageSignature};
use anyhow::{Context, Result};
Expand Down Expand Up @@ -316,11 +317,18 @@ pub(crate) async fn status(opts: super::cli::StatusOpts) -> Result<()> {
// Filter to just the serializable status structures.
let out = std::io::stdout();
let mut out = out.lock();
if opts.json {
serde_json::to_writer(&mut out, &host).context("Writing to stdout")?;
} else {
serde_yaml::to_writer(&mut out, &host).context("Writing to stdout")?;
let format = opts.format.unwrap_or_else(|| {
if opts.json {
OutputFormat::JSON
} else {
OutputFormat::YAML
}
});
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),
}
.context("Writing to stdout")?;

Ok(())
}
Expand Down
4 changes: 3 additions & 1 deletion tests/booted/001-test-status.nu
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std assert
use tap.nu

tap begin "verify bootc status --json looks sane"
tap begin "verify bootc status output formats"

let st = bootc status --json | from json
assert equal $st.apiVersion org.containers.bootc/v1alpha1
let st = bootc status --format=yaml | from yaml
assert equal $st.apiVersion org.containers.bootc/v1alpha1
tap ok

0 comments on commit 7a79b8d

Please sign in to comment.