From 4bbcb29591967b1277db3eeb1f8b235688f60458 Mon Sep 17 00:00:00 2001 From: HuijingHei Date: Fri, 6 Dec 2024 15:16:55 +0800 Subject: [PATCH] status: support `bootupctl status --json` in container The output like: ``` bash-5.2# bootupctl status --json {"components":["BIOS","EFI"]} ``` Fixes: https://github.com/coreos/bootupd/issues/794 --- .github/workflows/ci.yml | 2 ++ src/cli/bootupctl.rs | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9815a986..a3783b0b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,6 +29,8 @@ jobs: set -xeuo pipefail output=$(sudo podman run --rm -ti localhost/bootupd:latest bootupctl status | tr -d '\r') [ "Available components: BIOS EFI" == "${output}" ] + output=$(sudo podman run --rm -ti localhost/bootupd:latest bootupctl status --json) + [ '{"components":["BIOS","EFI"]}' == "${output}" ] - name: bootc install to disk run: | set -xeuo pipefail diff --git a/src/cli/bootupctl.rs b/src/cli/bootupctl.rs index 574180da..ab30decb 100644 --- a/src/cli/bootupctl.rs +++ b/src/cli/bootupctl.rs @@ -101,7 +101,7 @@ impl CtlCommand { /// Runner for `status` verb. fn run_status(opts: StatusOpts) -> Result<()> { if crate::util::running_in_container() { - return run_status_in_container(); + return run_status_in_container(opts.json); } ensure_running_in_systemd()?; let r = bootupd::status()?; @@ -176,12 +176,21 @@ fn ensure_running_in_systemd() -> Result<()> { } /// If running in container, just print the available payloads -fn run_status_in_container() -> Result<()> { +fn run_status_in_container(json_format: bool) -> Result<()> { let all_components = crate::bootupd::get_components(); if all_components.is_empty() { return Ok(()); } let avail: Vec<_> = all_components.keys().cloned().collect(); - println!("Available components: {}", avail.join(" ")); + if json_format { + let stdout = std::io::stdout(); + let mut stdout = stdout.lock(); + let output: serde_json::Value = serde_json::json!({ + "components": avail + }); + serde_json::to_writer(&mut stdout, &output)?; + } else { + println!("Available components: {}", avail.join(" ")); + } Ok(()) }