Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: Add JSON output option for pull --check & compare #618

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ use camino::{Utf8Path, Utf8PathBuf};
use cap_std::fs::Dir;
use cap_std_ext::cap_std;
use cap_std_ext::prelude::CapStdExtDirExt;
use clap::{Parser, Subcommand};
use clap::{builder::ArgPredicate, Parser, Subcommand};
use fn_error_context::context;
use io_lifetimes::AsFd;
use ostree::{gio, glib};
use serde_json;
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::ffi::OsString;
Expand Down Expand Up @@ -178,6 +179,10 @@ pub(crate) enum ContainerOpts {
/// Image reference, e.g. ostree-remote-image:someremote:registry:quay.io/exampleos/exampleos:latest
#[clap(value_parser = parse_imgref)]
imgref_new: OstreeImageReference,

/// Use JSON as output format.
#[clap(long)]
json: bool,
},
}

Expand Down Expand Up @@ -234,6 +239,10 @@ pub(crate) enum ContainerImageOpts {
/// the new manifest.
#[clap(long)]
check: Option<Utf8PathBuf>,
Copy link
Member Author

@travier travier Apr 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this Option is not respected by clap and always asked on the command line:

$ ostree container image pull --check check repo ostree-image-signed:registry:a.a/a:a
error: Opening directory at 'repo': No such file or directory (os error 2)

$ ostree container image pull --check repo ostree-image-signed:registry:a.a/a:a
error: the following required arguments were not provided:
  <IMGREF>

Usage: ostree-container container image pull --check <CHECK> <REPO> <IMGREF>

For more information, try '--help'.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, OK, it's a little bit more complex than that for the pull --check option as it optionally writes to a file.


/// Use JSON as output format. Only applies to the --check option.
#[clap(long, requires_if(ArgPredicate::IsPresent, "check"))]
json: bool,
},

/// Output metadata about an already stored container image.
Expand Down Expand Up @@ -717,6 +726,7 @@ async fn container_store(
proxyopts: ContainerProxyOpts,
quiet: bool,
check: Option<Utf8PathBuf>,
json: bool,
) -> Result<()> {
let mut imp = ImageImporter::new(repo, imgref, proxyopts.into()).await?;
let prep = match imp.prepare().await? {
Expand All @@ -739,7 +749,11 @@ async fn container_store(
}
if let Some(previous_state) = prep.previous_state.as_ref() {
let diff = ManifestDiff::new(&previous_state.manifest, &prep.manifest);
diff.print();
if json {
println!("{:#?}", serde_json::to_string(&diff));
} else {
diff.print();
}
}
print_layer_status(&prep);
let printer = (!quiet).then(|| {
Expand Down Expand Up @@ -965,9 +979,10 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
proxyopts,
quiet,
check,
json,
} => {
let repo = parse_repo(&repo)?;
container_store(&repo, &imgref, proxyopts, quiet, check).await
container_store(&repo, &imgref, proxyopts, quiet, check, json).await
}
ContainerImageOpts::History { repo, imgref } => {
let repo = parse_repo(&repo)?;
Expand Down Expand Up @@ -1177,12 +1192,17 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
ContainerOpts::Compare {
imgref_old,
imgref_new,
json,
} => {
let (manifest_old, _) = crate::container::fetch_manifest(&imgref_old).await?;
let (manifest_new, _) = crate::container::fetch_manifest(&imgref_new).await?;
let manifest_diff =
crate::container::ManifestDiff::new(&manifest_old, &manifest_new);
manifest_diff.print();
if json {
println!("{:#?}", serde_json::to_string(&manifest_diff));
} else {
manifest_diff.print();
}
Ok(())
}
},
Expand Down
Loading