From 4519c534a1bfa25ffc1f9504646228afdbf11b3f Mon Sep 17 00:00:00 2001 From: Sergio <77530549+sergi0g@users.noreply.github.com> Date: Sat, 16 Nov 2024 12:38:00 +0200 Subject: [PATCH] Move usage of get_images_from_docker_daemon to get_updates --- src/check.rs | 10 +++++++--- src/docker.rs | 29 ----------------------------- src/main.rs | 6 ++---- src/server.rs | 4 +--- 4 files changed, 10 insertions(+), 39 deletions(-) diff --git a/src/check.rs b/src/check.rs index c466d81..a42355a 100644 --- a/src/check.rs +++ b/src/check.rs @@ -4,13 +4,17 @@ use rustc_hash::FxHashMap; use crate::{ config::Config, + docker::get_images_from_docker_daemon, http::Client, registry::{check_auth, get_token}, structs::image::Image, }; /// Returns a list of updates for all images passed in. -pub async fn get_updates(images: &[Image], config: &Config) -> Vec { +pub async fn get_updates(references: &Option>, config: &Config) -> Vec { + // Get images + let images = get_images_from_docker_daemon(config, references).await; + // Get a list of unique registries our images belong to. We are unwrapping the registry because it's guaranteed to be there. let registries: Vec<&String> = images .iter() @@ -25,7 +29,7 @@ pub async fn get_updates(images: &[Image], config: &Config) -> Vec { // Create a map of images indexed by registry. This solution seems quite inefficient, since each iteration causes a key to be looked up. I can't find anything better at the moment. let mut image_map: FxHashMap<&String, Vec<&Image>> = FxHashMap::default(); - for image in images { + for image in &images { image_map.entry(&image.registry).or_default().push(image); } @@ -53,7 +57,7 @@ pub async fn get_updates(images: &[Image], config: &Config) -> Vec { // Create a Vec to store futures so we can await them all at once. let mut handles = Vec::new(); // Loop through images and get the latest digest for each - for image in images { + for image in &images { let token = tokens.get(image.registry.as_str()).unwrap(); let future = image.check(token.as_ref(), config, &client); handles.push(future); diff --git a/src/docker.rs b/src/docker.rs index e2a60c5..0321ded 100644 --- a/src/docker.rs +++ b/src/docker.rs @@ -29,35 +29,6 @@ pub async fn get_images_from_docker_daemon( references: &Option>, ) -> Vec { let client: Docker = create_docker_client(config.socket.clone()); - // If https://github.com/moby/moby/issues/48612 is fixed, this code should be faster (if it works, it may also be entirely stupid). 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 = match client - // .list_images::(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()); diff --git a/src/main.rs b/src/main.rs index 2954812..d4a01a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,6 @@ use check::get_updates; use clap::{Parser, Subcommand}; use config::Config; -use docker::get_images_from_docker_daemon; use formatting::spinner::Spinner; #[cfg(feature = "cli")] use formatting::{print_raw_updates, print_updates}; @@ -80,15 +79,14 @@ async fn main() { raw, }) => { let start = timestamp(); - let images = get_images_from_docker_daemon(&config, references).await; match raw { true => { - let updates = get_updates(&images, &config).await; + let updates = get_updates(references, &config).await; print_raw_updates(&updates); } false => { let spinner = Spinner::new(); - let updates = get_updates(&images, &config).await; + let updates = get_updates(references, &config).await; spinner.succeed(); let end = timestamp(); print_updates(&updates, icons); diff --git a/src/server.rs b/src/server.rs index a82066b..172e8d0 100644 --- a/src/server.rs +++ b/src/server.rs @@ -16,7 +16,6 @@ use xitca_web::{ use crate::{ check::get_updates, config::{Config, Theme}, - docker::get_images_from_docker_daemon, info, structs::image::Image, utils::{ @@ -137,8 +136,7 @@ impl ServerData { if !self.raw_updates.is_empty() { info!("Refreshing data"); } - let images = get_images_from_docker_daemon(&self.config, &None).await; - let updates = sort_image_vec(&get_updates(&images, &self.config).await); + let updates = sort_image_vec(&get_updates(&None, &self.config).await); let end = timestamp(); info!("✨ Checked {} images in {}ms", updates.len(), end - start); self.raw_updates = updates;