Skip to content

Commit 3df48ac

Browse files
authored
Allow creating oci-archive and oci-dir without image name (#132)
In some cases, one want to create an oci-archive or oci-dir which never pushed to registry.
1 parent 7806cf3 commit 3df48ac

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

ocipkg/src/image/layout.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ use anyhow::{bail, Context, Result};
66
use oci_spec::image::{Descriptor, DescriptorBuilder, ImageIndex, ImageManifest, MediaType};
77
use std::path::Path;
88

9-
/// Handler of [OCI Image Layout] with containing single manifest and its name.
9+
/// Handler of [OCI Image Layout] with containing single manifest
1010
///
11-
/// - [OCI Image Layout] allows empty image name, i.e. no `org.opencontainers.image.ref.name` annotation, but this trait does not allow it.
1211
/// - [OCI Image Layout] allows containing multiple manifests in a single layout,
1312
/// this trait assumes a single manifest in a single layout.
1413
///
1514
/// [OCI Image Layout]: https://github.com/opencontainers/image-spec/blob/v1.1.0/image-layout.md
1615
///
1716
pub trait Image {
18-
/// The name of this image.
17+
/// The name of this image. This fails if the image does not have name.
1918
fn get_name(&mut self) -> Result<ImageName>;
2019

2120
/// Get blob content.

ocipkg/src/image/oci_archive.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,25 @@ use std::{
1414

1515
/// Build an [OciArchive]
1616
pub struct OciArchiveBuilder {
17-
image_name: ImageName,
17+
image_name: Option<ImageName>,
1818
path: PathBuf,
1919
ar: tar::Builder<fs::File>,
2020
}
2121

2222
impl OciArchiveBuilder {
23+
pub fn new_unnamed(path: PathBuf) -> Result<Self> {
24+
if path.exists() {
25+
bail!("File already exists: {}", path.display());
26+
}
27+
let f = fs::File::create(&path)?;
28+
let ar = tar::Builder::new(f);
29+
Ok(Self {
30+
ar,
31+
path,
32+
image_name: None,
33+
})
34+
}
35+
2336
pub fn new(path: PathBuf, image_name: ImageName) -> Result<Self> {
2437
if path.exists() {
2538
bail!("File already exists: {}", path.display());
@@ -29,7 +42,7 @@ impl OciArchiveBuilder {
2942
Ok(Self {
3043
ar,
3144
path,
32-
image_name,
45+
image_name: Some(image_name),
3346
})
3447
}
3548
}
@@ -51,8 +64,12 @@ impl ImageBuilder for OciArchiveBuilder {
5164
.media_type(MediaType::ImageManifest)
5265
.size(size)
5366
.digest(digest.to_string())
54-
.annotations(hashmap! {
55-
"org.opencontainers.image.ref.name".to_string() => self.image_name.to_string()
67+
.annotations(if let Some(name) = &self.image_name {
68+
hashmap! {
69+
"org.opencontainers.image.ref.name".to_string() => name.to_string()
70+
}
71+
} else {
72+
hashmap! {}
5673
})
5774
.build()?;
5875
let index = ImageIndexBuilder::default()

ocipkg/src/image/oci_dir.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use super::get_name_from_index;
1616

1717
/// Build an [OciDir]
1818
pub struct OciDirBuilder {
19-
image_name: ImageName,
19+
image_name: Option<ImageName>,
2020
oci_dir_root: PathBuf,
2121
is_finished: bool,
2222
}
@@ -37,13 +37,25 @@ impl Drop for OciDirBuilder {
3737
}
3838

3939
impl OciDirBuilder {
40+
pub fn new_unnamed(oci_dir_root: PathBuf) -> Result<Self> {
41+
if oci_dir_root.exists() {
42+
bail!("oci-dir {} already exists", oci_dir_root.display());
43+
}
44+
fs::create_dir_all(&oci_dir_root)?;
45+
Ok(Self {
46+
image_name: None,
47+
oci_dir_root,
48+
is_finished: false,
49+
})
50+
}
51+
4052
pub fn new(oci_dir_root: PathBuf, image_name: ImageName) -> Result<Self> {
4153
if oci_dir_root.exists() {
4254
bail!("oci-dir {} already exists", oci_dir_root.display());
4355
}
4456
fs::create_dir_all(&oci_dir_root)?;
4557
Ok(Self {
46-
image_name,
58+
image_name: Some(image_name),
4759
oci_dir_root,
4860
is_finished: false,
4961
})
@@ -68,8 +80,12 @@ impl ImageBuilder for OciDirBuilder {
6880
.media_type(MediaType::ImageManifest)
6981
.size(size)
7082
.digest(digest.to_string())
71-
.annotations(hashmap! {
72-
"org.opencontainers.image.ref.name".to_string() => self.image_name.to_string(),
83+
.annotations(if let Some(name) = &self.image_name {
84+
hashmap! {
85+
"org.opencontainers.image.ref.name".to_string() => name.to_string()
86+
}
87+
} else {
88+
hashmap! {}
7389
})
7490
.build()?;
7591
let index = ImageIndexBuilder::default()

0 commit comments

Comments
 (0)