@@ -74,6 +74,30 @@ use url::Url;
74
74
/// # Ok::<(), anyhow::Error>(())
75
75
/// ```
76
76
///
77
+ /// [Reference] can be a digest:
78
+ ///
79
+ /// ```text
80
+ /// quay.io/jitesoft/alpine:sha256:6755355f801f8e3694bffb1a925786813462cea16f1ce2b0290b6a48acf2500c
81
+ /// ^^^^^^^-------------------- hostname
82
+ /// ^^^^^^^^^^^^^^^---- name
83
+ /// reference ---^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
84
+ /// ```
85
+ ///
86
+ /// ```
87
+ /// use ocipkg::{ImageName, distribution::{Name, Reference}};
88
+ /// let name = ImageName::parse("quay.io/jitesoft/alpine:sha256:6755355f801f8e3694bffb1a925786813462cea16f1ce2b0290b6a48acf2500c")?;
89
+ /// assert_eq!(
90
+ /// name,
91
+ /// ImageName {
92
+ /// hostname: "quay.io".to_string(),
93
+ /// port: None,
94
+ /// name: Name::new("jitesoft/alpine")?,
95
+ /// reference: Reference::new("sha256:6755355f801f8e3694bffb1a925786813462cea16f1ce2b0290b6a48acf2500c")?,
96
+ /// }
97
+ /// );
98
+ /// # Ok::<(), anyhow::Error>(())
99
+ /// ```
100
+ ///
77
101
/// Default values
78
102
/// ---------------
79
103
/// If `hostname` is absent, use `registry-1.docker.io` for docker compatibility:
@@ -175,13 +199,11 @@ impl ImageName {
175
199
176
200
/// Encode image name into a path by `{hostname}/{name}/__{reference}` or `{hostname}__{port}/{name}/__{reference}` if port is specified.
177
201
pub fn as_path ( & self ) -> PathBuf {
202
+ let reference = self . reference . replace ( ':' , "__" ) ;
178
203
PathBuf :: from ( if let Some ( port) = self . port {
179
- format ! (
180
- "{}__{}/{}/__{}" ,
181
- self . hostname, port, self . name, self . reference
182
- )
204
+ format ! ( "{}__{}/{}/__{}" , self . hostname, port, self . name, reference)
183
205
} else {
184
- format ! ( "{}/{}/__{}" , self . hostname, self . name, self . reference)
206
+ format ! ( "{}/{}/__{}" , self . hostname, self . name, reference)
185
207
} )
186
208
}
187
209
@@ -217,9 +239,10 @@ impl ImageName {
217
239
let name = Name :: new ( & components[ 1 ..n - 1 ] . join ( "/" ) ) ?;
218
240
219
241
let reference = Reference :: new (
220
- components[ n - 1 ]
242
+ & components[ n - 1 ]
221
243
. strip_prefix ( "__" )
222
- . with_context ( || anyhow ! ( "Missing tag in path: {}" , path. display( ) ) ) ?,
244
+ . with_context ( || anyhow ! ( "Missing tag in path: {}" , path. display( ) ) ) ?
245
+ . replace ( "__" , ":" ) ,
223
246
) ?;
224
247
225
248
Ok ( ImageName {
@@ -266,6 +289,10 @@ mod test {
266
289
"registry-1.docker.io/ubuntu/__20.04" . as_ref ( ) ,
267
290
) ?;
268
291
test_as_path ( "alpine" , "registry-1.docker.io/alpine/__latest" . as_ref ( ) ) ?;
292
+ test_as_path (
293
+ "quay.io/jitesoft/alpine:sha256:6755355f801f8e3694bffb1a925786813462cea16f1ce2b0290b6a48acf2500c" ,
294
+ "quay.io/jitesoft/alpine/__sha256__6755355f801f8e3694bffb1a925786813462cea16f1ce2b0290b6a48acf2500c" . as_ref ( )
295
+ ) ?;
269
296
Ok ( ( ) )
270
297
}
271
298
}
0 commit comments