core_dockpack/utils/
cache.rs

1//! Defines all the paths to cache directories and also handles the wiping of cache.
2use std::path::PathBuf;
3
4
5/// Processes the image name into a directory name.
6/// 
7/// # Arguments
8/// * `image_name` - the name of the image to be processed
9/// 
10/// # Returns
11/// * the converted image name string that can be used in a directory path.
12pub fn process_image_name(image_name: &String) -> String {
13    image_name.replace(":latest", "").replace("/", "_").replace(":", "_")
14}
15
16
17/// Wipes the cache directory and creates a new cache directory.
18///
19/// # Returns
20/// None
21pub fn wipe_and_create_cache(directory: &PathBuf) {
22    if directory.exists() {
23        std::fs::remove_dir_all(&*directory).expect(
24            "Failed to remove cache directory"
25        );
26    }
27    std::fs::create_dir_all(directory).expect(
28        "Failed to create nanoservices cache directory"
29    );
30    let tar_dir = directory.join("tar");
31    std::fs::create_dir_all(tar_dir).expect(
32        "Failed to create nanoservices tar cache directory"
33    );
34}
35
36
37#[cfg(test)]
38mod tests {
39
40    use super::*;
41
42    #[test]
43    fn test_process_image_name() {
44        let image_name = "surrealdb/surrealcs-client:latest".to_string();
45        // let expected = "docker.io_library_alpine".to_string();
46        println!("{}", process_image_name(&image_name));
47    }
48
49}