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 26, 2024
1 parent 969d032 commit fd4eb0b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
13 changes: 10 additions & 3 deletions docs/src/man/bootc-status.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ bootc-status - Display status

# SYNOPSIS

**bootc status** \[**\--json**\] \[**\--booted**\]
**bootc status** \[**\--format**\] \[**\--booted**\]
\[**-h**\|**\--help**\]

# DESCRIPTION
Expand All @@ -19,9 +19,16 @@ The exact API format is not currently declared stable.

# OPTIONS

**\--json**
**\--format**=*FORMAT*

: Output in JSON format
: The output format\

\
*Possible values:*

> - yaml: Output in YAML format
>
> - json: Output in JSON format
**\--booted**

Expand Down
19 changes: 18 additions & 1 deletion 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,28 @@ 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)]
///
/// Superceded by the `format` option.
#[clap(long, hide = true)]
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 +788,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 fd4eb0b

Please sign in to comment.