Skip to content

Commit

Permalink
Merge pull request #639 from cgwalters/cli-output-format
Browse files Browse the repository at this point in the history
status: Add explicit `--format` argument
  • Loading branch information
cgwalters authored Jun 26, 2024
2 parents 54abf86 + 12d653e commit 5e9279d
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 @@ -12,6 +12,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 @@ -107,13 +108,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 @@ -773,6 +789,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
Expand Up @@ -12,6 +12,7 @@ use ostree_ext::oci_spec::image::ImageConfiguration;
use ostree_ext::ostree;
use ostree_ext::sysroot::SysrootLock;

use crate::cli::OutputFormat;
use crate::spec::{BootEntry, BootOrder, Host, HostSpec, HostStatus, HostType, ImageStatus};
use crate::spec::{ImageReference, ImageSignature};

Expand Down Expand Up @@ -317,11 +318,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 5e9279d

Please sign in to comment.