Skip to content

Commit d47956a

Browse files
committed
ostree-ext/cli: add --digestfile to ostree container image pull
This is something that I've wanted many times. There's no easy way from e.g. bash to convert an image ref into the escaped OSTree ref and so it's always awkward to be able to tell what ref/commit was pulled down. Let's just match podman here and add a `--digestfile` switch for this.
1 parent bcd318f commit d47956a

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

ostree-ext/src/cli.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ pub(crate) enum ContainerImageOpts {
238238
#[clap(value_parser = parse_imgref)]
239239
imgref: OstreeImageReference,
240240

241+
/// File to which to write the resulting OSTree commit digest
242+
#[clap(long)]
243+
ostree_digestfile: Option<Utf8PathBuf>,
244+
241245
#[clap(flatten)]
242246
proxyopts: ContainerProxyOpts,
243247

@@ -863,13 +867,15 @@ async fn container_info(imgref: &OstreeImageReference) -> Result<()> {
863867
async fn container_store(
864868
repo: &ostree::Repo,
865869
imgref: &OstreeImageReference,
870+
ostree_digestfile: Option<Utf8PathBuf>,
866871
proxyopts: ContainerProxyOpts,
867872
quiet: bool,
868873
check: Option<Utf8PathBuf>,
869874
) -> Result<()> {
870875
let mut imp = ImageImporter::new(repo, imgref, proxyopts.into()).await?;
871876
let prep = match imp.prepare().await? {
872877
PrepareResult::AlreadyPresent(c) => {
878+
write_digest_file(ostree_digestfile, &c.merge_commit)?;
873879
println!("No changes in {} => {}", imgref, c.merge_commit);
874880
return Ok(());
875881
}
@@ -913,10 +919,19 @@ async fn container_store(
913919
if let Some(ref text) = import.verify_text {
914920
println!("{text}");
915921
}
922+
write_digest_file(ostree_digestfile, &import.merge_commit)?;
916923
println!("Wrote: {} => {}", imgref, import.merge_commit);
917924
Ok(())
918925
}
919926

927+
fn write_digest_file(digestfile: Option<Utf8PathBuf>, digest: &str) -> Result<()> {
928+
if let Some(digestfile) = digestfile.as_deref() {
929+
let rootfs = Dir::open_ambient_dir("/", cap_std::ambient_authority())?;
930+
rootfs.write(digestfile.as_str().trim_start_matches('/'), digest)?;
931+
}
932+
Ok(())
933+
}
934+
920935
/// Output the container image history
921936
async fn container_history(repo: &ostree::Repo, imgref: &ImageReference) -> Result<()> {
922937
let img = crate::container::store::query_image(repo, imgref)?
@@ -1095,12 +1110,14 @@ async fn run_from_opt(opt: Opt) -> Result<()> {
10951110
ContainerImageOpts::Pull {
10961111
repo,
10971112
imgref,
1113+
ostree_digestfile,
10981114
proxyopts,
10991115
quiet,
11001116
check,
11011117
} => {
11021118
let repo = parse_repo(&repo)?;
1103-
container_store(&repo, &imgref, proxyopts, quiet, check).await
1119+
container_store(&repo, &imgref, ostree_digestfile, proxyopts, quiet, check)
1120+
.await
11041121
}
11051122
ContainerImageOpts::Reexport {
11061123
repo,

0 commit comments

Comments
 (0)