From aa6989f92c9d2f68eb9843f98f00230d382dad4c Mon Sep 17 00:00:00 2001 From: ChengyuZhu6 Date: Fri, 22 Dec 2023 22:09:27 +0800 Subject: [PATCH] image-rs: Redefine the construction of ImageConfig ImageConfig represents the configuration of an image. Previously, we used the `CC_IMAGE_WORK_DIR` environment variable to set the image work directory, but this was not flexible or reliable enough for different use cases. Now, we provide a `new()` method that takes the image work directory as an argument, and returns an ImageConfig instance with that directory. This way, consumers can customize the image work directory as they need. Signed-off-by: ChengyuZhu6 --- image-rs/src/config.rs | 18 +++++++++--------- image-rs/src/lib.rs | 3 --- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/image-rs/src/config.rs b/image-rs/src/config.rs index 060ef3119..490db8881 100644 --- a/image-rs/src/config.rs +++ b/image-rs/src/config.rs @@ -9,7 +9,6 @@ use std::fs::File; use std::path::{Path, PathBuf}; use crate::snapshots::SnapshotType; -use crate::CC_IMAGE_WORK_DIR; const DEFAULT_WORK_DIR: &str = "/var/lib/image-rs/"; @@ -84,12 +83,8 @@ where impl Default for ImageConfig { // Construct a default instance of `ImageConfig` fn default() -> ImageConfig { - let work_dir = PathBuf::from( - std::env::var(CC_IMAGE_WORK_DIR).unwrap_or_else(|_| DEFAULT_WORK_DIR.to_string()), - ); - ImageConfig { - work_dir, + work_dir: PathBuf::from(DEFAULT_WORK_DIR.to_string()), #[cfg(feature = "snapshot-overlayfs")] default_snapshot: SnapshotType::Overlay, #[cfg(not(feature = "snapshot-overlayfs"))] @@ -131,6 +126,13 @@ impl TryFrom<&Path> for ImageConfig { } impl ImageConfig { + /// Construct an instance of `ImageConfig` with specific work directory. + pub fn new(image_work_dir: PathBuf) -> Self { + Self { + work_dir: image_work_dir, + ..Default::default() + } + } /// Validate the configuration object. pub fn validate(&self) -> bool { if let Some(nydus_cfg) = self.nydus_config.as_ref() { @@ -307,7 +309,6 @@ mod tests { #[test] fn test_image_config() { - std::env::remove_var(CC_IMAGE_WORK_DIR); let config = ImageConfig::default(); let work_dir = PathBuf::from(DEFAULT_WORK_DIR); @@ -319,8 +320,7 @@ mod tests { ); let env_work_dir = "/tmp"; - std::env::set_var(CC_IMAGE_WORK_DIR, env_work_dir); - let config = ImageConfig::default(); + let config = ImageConfig::new(PathBuf::from(env_work_dir)); let work_dir = PathBuf::from(env_work_dir); assert_eq!(config.work_dir, work_dir); } diff --git a/image-rs/src/lib.rs b/image-rs/src/lib.rs index 0cefd80f9..1996d2514 100644 --- a/image-rs/src/lib.rs +++ b/image-rs/src/lib.rs @@ -2,9 +2,6 @@ // // SPDX-License-Identifier: Apache-2.0 -/// Environment macro for `image-rs` work dir. -pub const CC_IMAGE_WORK_DIR: &str = "CC_IMAGE_WORK_DIR"; - pub const ERR_BAD_UNCOMPRESSED_DIGEST: &str = "unsupported uncompressed digest format"; pub mod auth;