Skip to content

Commit

Permalink
image-rs: Redefine the construction of ImageConfig
Browse files Browse the repository at this point in the history
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 <chengyu.zhu@intel.com>
  • Loading branch information
ChengyuZhu6 committed Dec 22, 2023
1 parent 4ddac38 commit aa6989f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
18 changes: 9 additions & 9 deletions image-rs/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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/";

Expand Down Expand Up @@ -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"))]
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
Expand Down
3 changes: 0 additions & 3 deletions image-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit aa6989f

Please sign in to comment.