mirror of
https://github.com/sergi0g/cup.git
synced 2025-11-17 17:43:37 -05:00
Refactor (#32)
This commit is contained in:
122
src/docker.rs
122
src/docker.rs
@@ -1,14 +1,8 @@
|
||||
use bollard::{secret::ImageSummary, ClientVersion, Docker};
|
||||
use bollard::{models::ImageInspect, ClientVersion, Docker};
|
||||
|
||||
#[cfg(feature = "cli")]
|
||||
use bollard::secret::ImageInspect;
|
||||
use futures::future::join_all;
|
||||
|
||||
use crate::{
|
||||
error,
|
||||
image::Image,
|
||||
utils::{split_image, CliConfig},
|
||||
};
|
||||
use crate::{error, image::Image, config::Config};
|
||||
|
||||
fn create_docker_client(socket: Option<String>) -> Docker {
|
||||
let client: Result<Docker, bollard::errors::Error> = match socket {
|
||||
@@ -29,47 +23,79 @@ fn create_docker_client(socket: Option<String>) -> Docker {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_images_from_docker_daemon(options: &CliConfig) -> Vec<Image> {
|
||||
let client: Docker = create_docker_client(options.socket.clone());
|
||||
let images: Vec<ImageSummary> = match client.list_images::<String>(None).await {
|
||||
Ok(images) => images,
|
||||
Err(e) => {
|
||||
error!("Failed to retrieve list of images available!\n{}", e)
|
||||
}
|
||||
};
|
||||
let mut handles = Vec::new();
|
||||
for image in images {
|
||||
handles.push(Image::from(image, options))
|
||||
}
|
||||
join_all(handles)
|
||||
.await
|
||||
.iter()
|
||||
.filter(|img| img.is_some())
|
||||
.map(|img| img.clone().unwrap())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(feature = "cli")]
|
||||
pub async fn get_image_from_docker_daemon(socket: Option<String>, name: &str) -> Image {
|
||||
let client: Docker = create_docker_client(socket);
|
||||
let image: ImageInspect = match client.inspect_image(name).await {
|
||||
Ok(i) => i,
|
||||
Err(e) => error!("Failed to retrieve image {} from daemon\n{}", name, e),
|
||||
};
|
||||
match image.repo_tags {
|
||||
Some(_) => (),
|
||||
None => error!("Image has no tags"), // I think this is actually unreachable
|
||||
}
|
||||
match image.repo_digests {
|
||||
Some(d) => {
|
||||
let (registry, repository, tag) = split_image(&image.repo_tags.unwrap()[0]);
|
||||
Image {
|
||||
registry,
|
||||
repository,
|
||||
tag,
|
||||
digest: Some(d[0].clone().split('@').collect::<Vec<&str>>()[1].to_string()),
|
||||
/// Retrieves images from Docker daemon. If `references` is Some, return only the images whose references match the ones specified.
|
||||
pub async fn get_images_from_docker_daemon(
|
||||
config: &Config,
|
||||
references: &Option<Vec<String>>,
|
||||
) -> Vec<Image> {
|
||||
let client: Docker = create_docker_client(config.socket.clone());
|
||||
// If https://github.com/moby/moby/issues/48612 is fixed, this code should be faster. For now a workaround will be used.
|
||||
// let mut filters = HashMap::with_capacity(1);
|
||||
// match references {
|
||||
// Some(refs) => {
|
||||
// filters.insert("reference".to_string(), refs.clone());
|
||||
// }
|
||||
// None => (),
|
||||
// }
|
||||
// let images: Vec<ImageSummary> = match client
|
||||
// .list_images::<String>(Some(ListImagesOptions {
|
||||
// filters,
|
||||
// ..Default::default()
|
||||
// }))
|
||||
// .await
|
||||
// {
|
||||
// Ok(images) => images,
|
||||
// Err(e) => {
|
||||
// error!("Failed to retrieve list of images available!\n{}", e)
|
||||
// }
|
||||
// };
|
||||
// let mut handles = Vec::new();
|
||||
// for image in images {
|
||||
// handles.push(Image::from(image, options))
|
||||
// }
|
||||
// join_all(handles)
|
||||
// .await
|
||||
// .iter()
|
||||
// .filter_map(|img| img.clone())
|
||||
// .collect()
|
||||
match references {
|
||||
Some(refs) => {
|
||||
let mut inspect_handles = Vec::with_capacity(refs.len());
|
||||
for reference in refs {
|
||||
inspect_handles.push(client.inspect_image(reference));
|
||||
}
|
||||
let inspects: Vec<ImageInspect> = join_all(inspect_handles)
|
||||
.await
|
||||
.iter()
|
||||
.filter(|inspect| inspect.is_ok())
|
||||
.map(|inspect| inspect.as_ref().unwrap().clone())
|
||||
.collect();
|
||||
let mut image_handles = Vec::with_capacity(inspects.len());
|
||||
for inspect in inspects {
|
||||
image_handles.push(Image::from_inspect(inspect.clone()));
|
||||
}
|
||||
join_all(image_handles)
|
||||
.await
|
||||
.iter()
|
||||
.filter_map(|img| img.clone())
|
||||
.collect()
|
||||
}
|
||||
None => {
|
||||
let images = match client.list_images::<String>(None).await {
|
||||
Ok(images) => images,
|
||||
Err(e) => {
|
||||
error!("Failed to retrieve list of images available!\n{}", e)
|
||||
}
|
||||
};
|
||||
let mut handles = Vec::new();
|
||||
for image in images {
|
||||
handles.push(Image::from_summary(image))
|
||||
}
|
||||
join_all(handles)
|
||||
.await
|
||||
.iter()
|
||||
.filter_map(|img| img.clone())
|
||||
.collect()
|
||||
}
|
||||
None => error!("No digests found for image {}", name),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user