-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Replace `ocipkg::Digest` by `oci_spec::image::Digest` - This is breaking change. Bump up to 0.4.0
- Loading branch information
Showing
16 changed files
with
123 additions
and
160 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
name = "ocipkg-cli" | ||
authors = ["Toshiki Teramura <[email protected]>"] | ||
license = "MIT OR Apache-2.0" | ||
version = "0.3.10" | ||
version = "0.4.0" | ||
edition = "2021" | ||
description = "CLI for ocipkg" | ||
documentation = "https://docs.rs/ocipkg-cli" | ||
|
@@ -25,7 +25,7 @@ tar.workspace = true | |
url.workspace = true | ||
|
||
[dependencies.ocipkg] | ||
version = "0.3.10" | ||
version = "0.4.0" | ||
path = "../ocipkg" | ||
|
||
[[bin]] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "ocipkg" | ||
version = "0.3.10" | ||
version = "0.4.0" | ||
authors = ["Toshiki Teramura <[email protected]>"] | ||
edition = "2021" | ||
license = "MIT OR Apache-2.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,20 @@ | ||
use anyhow::{bail, Result}; | ||
use regex::Regex; | ||
use serde::{Deserialize, Serialize}; | ||
use oci_spec::image::Digest; | ||
use sha2::{Digest as _, Sha256}; | ||
use std::{fmt, path::PathBuf}; | ||
use std::{path::PathBuf, str::FromStr}; | ||
|
||
/// Digest of contents | ||
/// | ||
/// Digest is defined in [OCI image spec](https://github.com/opencontainers/image-spec/blob/v1.0.1/descriptor.md#digests) | ||
/// as a string satisfies following EBNF: | ||
/// | ||
/// ```text | ||
/// digest ::= algorithm ":" encoded | ||
/// algorithm ::= algorithm-component (algorithm-separator algorithm-component)* | ||
/// algorithm-component ::= [a-z0-9]+ | ||
/// algorithm-separator ::= [+._-] | ||
/// encoded ::= [a-zA-Z0-9=_-]+ | ||
/// ``` | ||
#[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
pub struct Digest { | ||
pub algorithm: String, | ||
pub encoded: String, | ||
pub trait DigestExt { | ||
fn eval_sha256_digest(buf: &[u8]) -> Self; | ||
fn as_path(&self) -> PathBuf; | ||
} | ||
|
||
lazy_static::lazy_static! { | ||
static ref ENCODED_RE: Regex = Regex::new(r"[a-zA-Z0-9=_-]+").unwrap(); | ||
} | ||
|
||
impl fmt::Display for Digest { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}:{}", self.algorithm, self.encoded) | ||
} | ||
} | ||
|
||
impl Serialize for Digest { | ||
fn serialize<S>(&self, serializer: S) -> std::prelude::v1::Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
serializer.serialize_str(&self.to_string()) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for Digest { | ||
fn deserialize<D>(deserializer: D) -> std::prelude::v1::Result<Digest, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
let s = String::deserialize(deserializer)?; | ||
Digest::new(&s).map_err(serde::de::Error::custom) | ||
} | ||
} | ||
|
||
impl Digest { | ||
pub fn new(input: &str) -> Result<Self> { | ||
let mut iter = input.split(':'); | ||
match (iter.next(), iter.next(), iter.next()) { | ||
(Some(algorithm), Some(encoded), None) => { | ||
// FIXME: check algorithm part | ||
if ENCODED_RE.is_match(encoded) { | ||
Ok(Digest { | ||
algorithm: algorithm.to_string(), | ||
encoded: encoded.to_string(), | ||
}) | ||
} else { | ||
bail!("Invalid digest: {}", input); | ||
} | ||
} | ||
_ => bail!("Invalid digest: {}", input), | ||
} | ||
} | ||
|
||
pub fn from_descriptor(descriptor: &oci_spec::image::Descriptor) -> Result<Self> { | ||
Self::new(descriptor.digest()) | ||
} | ||
|
||
/// As a path used in oci-archive | ||
pub fn as_path(&self) -> PathBuf { | ||
PathBuf::from(format!("blobs/{}/{}", self.algorithm, self.encoded)) | ||
} | ||
|
||
/// Calc digest using SHA-256 algorithm | ||
pub fn from_buf_sha256(buf: &[u8]) -> Self { | ||
impl DigestExt for Digest { | ||
fn eval_sha256_digest(buf: &[u8]) -> Self { | ||
let hash = Sha256::digest(buf); | ||
let digest = base16ct::lower::encode_string(&hash); | ||
Self { | ||
algorithm: "sha256".to_string(), | ||
encoded: digest, | ||
} | ||
oci_spec::image::Digest::from_str(&format!("sha256:{}", digest)).unwrap() | ||
} | ||
|
||
fn as_path(&self) -> PathBuf { | ||
PathBuf::from(format!("blobs/{}/{}", self.algorithm(), self.digest())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.