Skip to content

Commit

Permalink
twoliter: improve callsite clarity of ImageResolver
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cbgbt committed Sep 6, 2024
1 parent 87624fe commit 8cf1e40
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 12 additions & 2 deletions twoliter/src/lock/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ impl ImageResolverImpl for OverriddenImage {
#[derive(Debug)]
pub struct ImageResolver {
image_resolver_impl: Box<dyn ImageResolverImpl>,
skip_metadata_retrieval: bool,
}

impl ImageResolver {
Expand Down Expand Up @@ -244,6 +245,7 @@ impl ImageResolver {
},
})
},
skip_metadata_retrieval: false,
})
}

Expand Down Expand Up @@ -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<String> {
let image_uri = self.image_resolver_impl.uri();
Expand All @@ -317,7 +328,6 @@ impl ImageResolver {
pub(crate) async fn resolve(
&self,
image_tool: &ImageTool,
skip_metadata: bool,
) -> Result<(LockedImage, Option<ImageMetadata>)> {
// First get the manifest list
let uri = self.image_resolver_impl.uri();
Expand All @@ -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));
}

Expand Down
8 changes: 5 additions & 3 deletions twoliter/src/lock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8cf1e40

Please sign in to comment.