mirror of
https://github.com/sergi0g/cup.git
synced 2025-11-17 01:23:39 -05:00
Move usage of get_images_from_docker_daemon to get_updates
This commit is contained in:
10
src/check.rs
10
src/check.rs
@@ -4,13 +4,17 @@ use rustc_hash::FxHashMap;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
config::Config,
|
config::Config,
|
||||||
|
docker::get_images_from_docker_daemon,
|
||||||
http::Client,
|
http::Client,
|
||||||
registry::{check_auth, get_token},
|
registry::{check_auth, get_token},
|
||||||
structs::image::Image,
|
structs::image::Image,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Returns a list of updates for all images passed in.
|
/// 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.
|
// 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
|
let registries: Vec<&String> = images
|
||||||
.iter()
|
.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.
|
// 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();
|
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);
|
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.
|
// Create a Vec to store futures so we can await them all at once.
|
||||||
let mut handles = Vec::new();
|
let mut handles = Vec::new();
|
||||||
// Loop through images and get the latest digest for each
|
// 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 token = tokens.get(image.registry.as_str()).unwrap();
|
||||||
let future = image.check(token.as_ref(), config, &client);
|
let future = image.check(token.as_ref(), config, &client);
|
||||||
handles.push(future);
|
handles.push(future);
|
||||||
|
|||||||
@@ -29,35 +29,6 @@ pub async fn get_images_from_docker_daemon(
|
|||||||
references: &Option<Vec<String>>,
|
references: &Option<Vec<String>>,
|
||||||
) -> Vec<Image> {
|
) -> Vec<Image> {
|
||||||
let client: Docker = create_docker_client(config.socket.clone());
|
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 {
|
match references {
|
||||||
Some(refs) => {
|
Some(refs) => {
|
||||||
let mut inspect_handles = Vec::with_capacity(refs.len());
|
let mut inspect_handles = Vec::with_capacity(refs.len());
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
use check::get_updates;
|
use check::get_updates;
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use config::Config;
|
use config::Config;
|
||||||
use docker::get_images_from_docker_daemon;
|
|
||||||
use formatting::spinner::Spinner;
|
use formatting::spinner::Spinner;
|
||||||
#[cfg(feature = "cli")]
|
#[cfg(feature = "cli")]
|
||||||
use formatting::{print_raw_updates, print_updates};
|
use formatting::{print_raw_updates, print_updates};
|
||||||
@@ -80,15 +79,14 @@ async fn main() {
|
|||||||
raw,
|
raw,
|
||||||
}) => {
|
}) => {
|
||||||
let start = timestamp();
|
let start = timestamp();
|
||||||
let images = get_images_from_docker_daemon(&config, references).await;
|
|
||||||
match raw {
|
match raw {
|
||||||
true => {
|
true => {
|
||||||
let updates = get_updates(&images, &config).await;
|
let updates = get_updates(references, &config).await;
|
||||||
print_raw_updates(&updates);
|
print_raw_updates(&updates);
|
||||||
}
|
}
|
||||||
false => {
|
false => {
|
||||||
let spinner = Spinner::new();
|
let spinner = Spinner::new();
|
||||||
let updates = get_updates(&images, &config).await;
|
let updates = get_updates(references, &config).await;
|
||||||
spinner.succeed();
|
spinner.succeed();
|
||||||
let end = timestamp();
|
let end = timestamp();
|
||||||
print_updates(&updates, icons);
|
print_updates(&updates, icons);
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ use xitca_web::{
|
|||||||
use crate::{
|
use crate::{
|
||||||
check::get_updates,
|
check::get_updates,
|
||||||
config::{Config, Theme},
|
config::{Config, Theme},
|
||||||
docker::get_images_from_docker_daemon,
|
|
||||||
info,
|
info,
|
||||||
structs::image::Image,
|
structs::image::Image,
|
||||||
utils::{
|
utils::{
|
||||||
@@ -137,8 +136,7 @@ impl ServerData {
|
|||||||
if !self.raw_updates.is_empty() {
|
if !self.raw_updates.is_empty() {
|
||||||
info!("Refreshing data");
|
info!("Refreshing data");
|
||||||
}
|
}
|
||||||
let images = get_images_from_docker_daemon(&self.config, &None).await;
|
let updates = sort_image_vec(&get_updates(&None, &self.config).await);
|
||||||
let updates = sort_image_vec(&get_updates(&images, &self.config).await);
|
|
||||||
let end = timestamp();
|
let end = timestamp();
|
||||||
info!("✨ Checked {} images in {}ms", updates.len(), end - start);
|
info!("✨ Checked {} images in {}ms", updates.len(), end - start);
|
||||||
self.raw_updates = updates;
|
self.raw_updates = updates;
|
||||||
|
|||||||
Reference in New Issue
Block a user