From 8cf1e40547d9296013dda6922d32eb0b5730a98f Mon Sep 17 00:00:00 2001 From: "Sean P. Kelly" Date: Fri, 6 Sep 2024 08:27:28 +0000 Subject: [PATCH] twoliter: improve callsite clarity of ImageResolver Switches to a builder-style mechanism for disabling metadata fetching. Since Rust doesn't have named args, it can be hard to know what boolean function flags do at a callsite without going to the function signature. --- twoliter/src/lock/image.rs | 14 ++++++++++++-- twoliter/src/lock/mod.rs | 8 +++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/twoliter/src/lock/image.rs b/twoliter/src/lock/image.rs index 4210cf1c..0b3e6dcd 100644 --- a/twoliter/src/lock/image.rs +++ b/twoliter/src/lock/image.rs @@ -204,6 +204,7 @@ impl ImageResolverImpl for OverriddenImage { #[derive(Debug)] pub struct ImageResolver { image_resolver_impl: Box, + skip_metadata_retrieval: bool, } impl ImageResolver { @@ -244,6 +245,7 @@ impl ImageResolver { }, }) }, + skip_metadata_retrieval: false, }) } @@ -289,9 +291,18 @@ impl ImageResolver { }, }) }, + skip_metadata_retrieval: false, }) } + /// Skip metadata retrieval when resolving images. + /// + /// This is useful for SDKs, which don't store image metadata (no deps.) + pub(crate) fn skip_metadata_retrieval(mut self) -> Self { + self.skip_metadata_retrieval = true; + self + } + /// Calculate the digest of the locked image async fn calculate_digest(&self, image_tool: &ImageTool) -> Result { let image_uri = self.image_resolver_impl.uri(); @@ -317,7 +328,6 @@ impl ImageResolver { pub(crate) async fn resolve( &self, image_tool: &ImageTool, - skip_metadata: bool, ) -> Result<(LockedImage, Option)> { // First get the manifest list let uri = self.image_resolver_impl.uri(); @@ -336,7 +346,7 @@ impl ImageResolver { digest: self.calculate_digest(image_tool).await?, }; - if skip_metadata { + if self.skip_metadata_retrieval { return Ok((locked_image, None)); } diff --git a/twoliter/src/lock/mod.rs b/twoliter/src/lock/mod.rs index 24532899..afea27bc 100644 --- a/twoliter/src/lock/mod.rs +++ b/twoliter/src/lock/mod.rs @@ -100,7 +100,8 @@ impl LockedSDK { debug!(?sdk, "Resolving workspace SDK"); let image_tool = ImageTool::from_environment()?; ImageResolver::from_image(&sdk, project)? - .resolve(&image_tool, true) + .skip_metadata_retrieval() // SDKs don't have metadata + .resolve(&image_tool) .await .map(|(sdk, _)| Some(Self(sdk))) } @@ -284,7 +285,7 @@ impl Lock { image.version.clone(), ); let image_resolver = ImageResolver::from_image(image, project)?; - let (locked_image, metadata) = image_resolver.resolve(&image_tool, false).await?; + let (locked_image, metadata) = image_resolver.resolve(&image_tool).await?; let metadata = metadata.context(format!( "failed to validate kit image with name {} from vendor {}", locked_image.name, locked_image.vendor @@ -313,7 +314,8 @@ impl Lock { debug!(?sdk, "Resolving workspace SDK"); let (sdk, _metadata) = ImageResolver::from_image(sdk, project)? - .resolve(&image_tool, true) + .skip_metadata_retrieval() // SDKs don't have metadata + .resolve(&image_tool) .await?; Ok(Self {