mirror of
https://github.com/sergi0g/cup.git
synced 2025-11-17 01:23:39 -05:00
Nearly complete versioning support. Fixed old bugs where not all tags were fetched.
This commit is contained in:
42
src/check.rs
42
src/check.rs
@@ -1,56 +1,36 @@
|
||||
use futures::future::join_all;
|
||||
use rustc_hash::{FxHashMap, FxHashSet};
|
||||
use rustc_hash::FxHashMap;
|
||||
use itertools::Itertools;
|
||||
|
||||
use crate::{
|
||||
config::Config,
|
||||
image::Image,
|
||||
http::Client,
|
||||
registry::{check_auth, get_token},
|
||||
utils::new_reqwest_client,
|
||||
structs::image::Image,
|
||||
};
|
||||
|
||||
/// Trait for a type that implements a function `unique` that removes any duplicates.
|
||||
/// In this case, it will be used for a Vec.
|
||||
pub trait Unique<T> {
|
||||
fn unique(&mut self) -> Vec<T>;
|
||||
}
|
||||
|
||||
impl<T> Unique<T> for Vec<T>
|
||||
where
|
||||
T: Clone + Eq + std::hash::Hash,
|
||||
{
|
||||
/// Remove duplicates from Vec
|
||||
fn unique(self: &mut Vec<T>) -> Self {
|
||||
let mut seen: FxHashSet<T> = FxHashSet::default();
|
||||
self.retain(|item| seen.insert(item.clone()));
|
||||
self.to_vec()
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a list of updates for all images passed in.
|
||||
pub async fn get_updates(images: &[Image], config: &Config) -> Vec<Image> {
|
||||
// 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()
|
||||
.map(|image| image.registry.as_ref().unwrap())
|
||||
.collect::<Vec<&String>>()
|
||||
.unique();
|
||||
.map(|image| &image.registry)
|
||||
.unique()
|
||||
.collect::<Vec<&String>>();
|
||||
|
||||
// Create request client. All network requests share the same client for better performance.
|
||||
// This client is also configured to retry a failed request up to 3 times with exponential backoff in between.
|
||||
let client = new_reqwest_client();
|
||||
let client = Client::new();
|
||||
|
||||
// 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 {
|
||||
image_map
|
||||
.entry(image.registry.as_ref().unwrap())
|
||||
.or_default()
|
||||
.push(image);
|
||||
image_map.entry(&image.registry).or_default().push(image);
|
||||
}
|
||||
|
||||
// Retrieve an authentication token (if required) for each registry.
|
||||
let mut tokens: FxHashMap<&String, Option<String>> = FxHashMap::default();
|
||||
let mut tokens: FxHashMap<&str, Option<String>> = FxHashMap::default();
|
||||
for registry in registries {
|
||||
let credentials = config.authentication.get(registry);
|
||||
match check_auth(registry, config, &client).await {
|
||||
@@ -74,7 +54,7 @@ pub async fn get_updates(images: &[Image], config: &Config) -> Vec<Image> {
|
||||
let mut handles = Vec::new();
|
||||
// Loop through images and get the latest digest for each
|
||||
for image in images {
|
||||
let token = tokens.get(&image.registry.as_ref().unwrap()).unwrap();
|
||||
let token = tokens.get(image.registry.as_str()).unwrap();
|
||||
let future = image.check(token.as_ref(), config, &client);
|
||||
handles.push(future);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user