Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit 8d3db9c

Browse files
committed
cli: Add JSON output option for pull --check & compare
This will make it easier for other higher level tools (Plasma Discover for example) to read the output of those commands.
1 parent 62bac42 commit 8d3db9c

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

lib/src/cli.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ use camino::{Utf8Path, Utf8PathBuf};
1010
use cap_std::fs::Dir;
1111
use cap_std_ext::cap_std;
1212
use cap_std_ext::prelude::CapStdExtDirExt;
13-
use clap::{Parser, Subcommand};
13+
use clap::{builder::ArgPredicate, Parser, Subcommand};
1414
use fn_error_context::context;
1515
use io_lifetimes::AsFd;
1616
use ostree::{gio, glib};
17+
use serde_json;
1718
use std::borrow::Cow;
1819
use std::collections::BTreeMap;
1920
use std::ffi::OsString;
@@ -178,6 +179,10 @@ pub(crate) enum ContainerOpts {
178179
/// Image reference, e.g. ostree-remote-image:someremote:registry:quay.io/exampleos/exampleos:latest
179180
#[clap(value_parser = parse_imgref)]
180181
imgref_new: OstreeImageReference,
182+
183+
/// Use JSON as output format.
184+
#[clap(long)]
185+
json: bool,
181186
},
182187
}
183188

@@ -234,6 +239,10 @@ pub(crate) enum ContainerImageOpts {
234239
/// the new manifest.
235240
#[clap(long)]
236241
check: Option<Utf8PathBuf>,
242+
243+
/// Use JSON as output format. Only applies to the --check option.
244+
#[clap(long, requires_if(ArgPredicate::IsPresent, "check"))]
245+
json: bool,
237246
},
238247

239248
/// Output metadata about an already stored container image.
@@ -717,6 +726,7 @@ async fn container_store(
717726
proxyopts: ContainerProxyOpts,
718727
quiet: bool,
719728
check: Option<Utf8PathBuf>,
729+
json: bool,
720730
) -> Result<()> {
721731
let mut imp = ImageImporter::new(repo, imgref, proxyopts.into()).await?;
722732
let prep = match imp.prepare().await? {
@@ -739,7 +749,11 @@ async fn container_store(
739749
}
740750
if let Some(previous_state) = prep.previous_state.as_ref() {
741751
let diff = ManifestDiff::new(&previous_state.manifest, &prep.manifest);
742-
diff.print();
752+
if json {
753+
println!("{:#?}", serde_json::to_string(&diff));
754+
} else {
755+
diff.print();
756+
}
743757
}
744758
print_layer_status(&prep);
745759
let printer = (!quiet).then(|| {
@@ -965,9 +979,10 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
965979
proxyopts,
966980
quiet,
967981
check,
982+
json,
968983
} => {
969984
let repo = parse_repo(&repo)?;
970-
container_store(&repo, &imgref, proxyopts, quiet, check).await
985+
container_store(&repo, &imgref, proxyopts, quiet, check, json).await
971986
}
972987
ContainerImageOpts::History { repo, imgref } => {
973988
let repo = parse_repo(&repo)?;
@@ -1177,12 +1192,17 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
11771192
ContainerOpts::Compare {
11781193
imgref_old,
11791194
imgref_new,
1195+
json,
11801196
} => {
11811197
let (manifest_old, _) = crate::container::fetch_manifest(&imgref_old).await?;
11821198
let (manifest_new, _) = crate::container::fetch_manifest(&imgref_new).await?;
11831199
let manifest_diff =
11841200
crate::container::ManifestDiff::new(&manifest_old, &manifest_new);
1185-
manifest_diff.print();
1201+
if json {
1202+
println!("{:#?}", serde_json::to_string(&manifest_diff));
1203+
} else {
1204+
manifest_diff.print();
1205+
}
11861206
Ok(())
11871207
}
11881208
},

0 commit comments

Comments
 (0)