m/cup
1
0
mirror of https://github.com/sergi0g/cup.git synced 2025-11-16 09:03:46 -05:00

Move usage of get_images_from_docker_daemon to get_updates

This commit is contained in:
Sergio
2024-11-16 12:38:00 +02:00
parent 6b83f51749
commit 4519c534a1
4 changed files with 10 additions and 39 deletions

View File

@@ -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<Image> {
pub async fn get_updates(references: &Option<Vec<String>>, config: &Config) -> Vec<Image> {
// 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<Image> {
// 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<Image> {
// 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);

View File

@@ -29,35 +29,6 @@ pub async fn get_images_from_docker_daemon(
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 (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<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());

View File

@@ -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);

View File

@@ -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;