Skip to content

Commit 5e9279d

Browse files
authored
Merge pull request #639 from cgwalters/cli-output-format
status: Add explicit `--format` argument
2 parents 54abf86 + 12d653e commit 5e9279d

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

docs/src/man/bootc-status.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ bootc-status - Display status
44

55
# SYNOPSIS
66

7-
**bootc status** \[**\--json**\] \[**\--booted**\]
7+
**bootc status** \[**\--format**\] \[**\--booted**\]
88
\[**-h**\|**\--help**\]
99

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

2020
# OPTIONS
2121

22-
**\--json**
22+
**\--format**=*FORMAT*
2323

24-
: Output in JSON format
24+
: The output format\
25+
26+
\
27+
*Possible values:*
28+
29+
> - yaml: Output in YAML format
30+
>
31+
> - json: Output in JSON format
2532
2633
**\--booted**
2734

lib/src/cli.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use camino::Utf8PathBuf;
1212
use cap_std_ext::cap_std;
1313
use cap_std_ext::cap_std::fs::Dir;
1414
use clap::Parser;
15+
use clap::ValueEnum;
1516
use fn_error_context::context;
1617
use ostree::gio;
1718
use ostree_container::store::PrepareResult;
@@ -107,13 +108,28 @@ pub(crate) struct EditOpts {
107108
pub(crate) quiet: bool,
108109
}
109110

111+
#[derive(Debug, Clone, ValueEnum, PartialEq, Eq)]
112+
#[clap(rename_all = "lowercase")]
113+
pub(crate) enum OutputFormat {
114+
/// Output in YAML format.
115+
YAML,
116+
/// Output in JSON format.
117+
JSON,
118+
}
119+
110120
/// Perform an status operation
111121
#[derive(Debug, Parser, PartialEq, Eq)]
112122
pub(crate) struct StatusOpts {
113123
/// Output in JSON format.
114-
#[clap(long)]
124+
///
125+
/// Superceded by the `format` option.
126+
#[clap(long, hide = true)]
115127
pub(crate) json: bool,
116128

129+
/// The output format.
130+
#[clap(long)]
131+
pub(crate) format: Option<OutputFormat>,
132+
117133
/// Only display status for the booted deployment.
118134
#[clap(long)]
119135
pub(crate) booted: bool,
@@ -773,6 +789,7 @@ fn test_parse_opts() {
773789
Opt::parse_including_static(["bootc", "status"]),
774790
Opt::Status(StatusOpts {
775791
json: false,
792+
format: None,
776793
booted: false
777794
})
778795
));

lib/src/status.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use ostree_ext::oci_spec::image::ImageConfiguration;
1212
use ostree_ext::ostree;
1313
use ostree_ext::sysroot::SysrootLock;
1414

15+
use crate::cli::OutputFormat;
1516
use crate::spec::{BootEntry, BootOrder, Host, HostSpec, HostStatus, HostType, ImageStatus};
1617
use crate::spec::{ImageReference, ImageSignature};
1718

@@ -317,11 +318,18 @@ pub(crate) async fn status(opts: super::cli::StatusOpts) -> Result<()> {
317318
// Filter to just the serializable status structures.
318319
let out = std::io::stdout();
319320
let mut out = out.lock();
320-
if opts.json {
321-
serde_json::to_writer(&mut out, &host).context("Writing to stdout")?;
322-
} else {
323-
serde_yaml::to_writer(&mut out, &host).context("Writing to stdout")?;
321+
let format = opts.format.unwrap_or_else(|| {
322+
if opts.json {
323+
OutputFormat::JSON
324+
} else {
325+
OutputFormat::YAML
326+
}
327+
});
328+
match format {
329+
OutputFormat::JSON => serde_json::to_writer(&mut out, &host).map_err(anyhow::Error::new),
330+
OutputFormat::YAML => serde_yaml::to_writer(&mut out, &host).map_err(anyhow::Error::new),
324331
}
332+
.context("Writing to stdout")?;
325333

326334
Ok(())
327335
}

tests/booted/001-test-status.nu

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use std assert
22
use tap.nu
33

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

66
let st = bootc status --json | from json
77
assert equal $st.apiVersion org.containers.bootc/v1alpha1
8+
let st = bootc status --format=yaml | from yaml
9+
assert equal $st.apiVersion org.containers.bootc/v1alpha1
810
tap ok

0 commit comments

Comments
 (0)