m/cup
1
0
mirror of https://github.com/sergi0g/cup.git synced 2025-11-18 01:43:41 -05:00

Various small changes and optimizations related to timekeeping, logging and JSON handling. Binary size shrunk by 0.2 MB!

This commit is contained in:
Sergio
2025-01-02 12:48:35 +02:00
parent ded441cf75
commit 84609d5189
16 changed files with 195 additions and 202 deletions

View File

@@ -1,3 +1,5 @@
use std::time::SystemTime;
use itertools::Itertools;
use crate::{
@@ -10,10 +12,9 @@ use crate::{
},
utils::{
link::parse_link,
misc::timestamp,
request::{
get_protocol, get_response_body, parse_json, parse_www_authenticate, to_bearer_string,
},
}, time::{elapsed, now},
},
};
@@ -50,7 +51,7 @@ pub async fn get_latest_digest(
config.debug,
"Checking for digest update to {}", image.reference
);
let start = timestamp();
let start = SystemTime::now();
let protocol = get_protocol(&image.registry, &config.registries);
let url = format!(
"{}://{}/v2/{}/manifests/{}",
@@ -60,7 +61,7 @@ pub async fn get_latest_digest(
let headers = vec![("Accept", Some("application/vnd.docker.distribution.manifest.list.v2+json, application/vnd.docker.distribution.manifest.v2+json, application/vnd.oci.image.index.v1+json")), ("Authorization", authorization.as_deref())];
let response = client.head(&url, headers).await;
let time = timestamp() - start;
let time = start.elapsed().unwrap().as_millis() as u32;
debug!(
config.debug,
"Checked for digest update to {} in {}ms", image.reference, time
@@ -112,7 +113,7 @@ pub async fn get_token(
Ok(response) => parse_json(&get_response_body(response).await),
Err(_) => error!("GET {}: Request failed!", url),
};
response_json["token"].to_string()
response_json["token"].as_str().unwrap().to_string()
}
pub async fn get_latest_tag(
@@ -126,7 +127,7 @@ pub async fn get_latest_tag(
config.debug,
"Checking for tag update to {}", image.reference
);
let start = timestamp();
let start = now();
let protocol = get_protocol(&image.registry, &config.registries);
let url = format!(
"{}://{}/v2/{}/tags/list",
@@ -148,25 +149,31 @@ pub async fn get_latest_tag(
image.reference,
tags.len()
);
let (new_tags, next) =
match get_extra_tags(&next_url.unwrap(), headers.clone(), base, &image.version_info.as_ref().unwrap().format_str, client).await {
Ok(t) => t,
Err(message) => {
return Image {
error: Some(message),
time_ms: image.time_ms + (timestamp() - start),
..image.clone()
}
let (new_tags, next) = match get_extra_tags(
&next_url.unwrap(),
headers.clone(),
base,
&image.version_info.as_ref().unwrap().format_str,
client,
)
.await
{
Ok(t) => t,
Err(message) => {
return Image {
error: Some(message),
time_ms: image.time_ms + elapsed(start),
..image.clone()
}
};
}
};
tags.extend_from_slice(&new_tags);
next_url = next;
}
let tag = tags.iter().max();
let time = timestamp() - start;
debug!(
config.debug,
"Checked for tag update to {} in {}ms", image.reference, time
"Checked for tag update to {} in {}ms", image.reference, elapsed(start)
);
match tag {
Some(t) => {
@@ -178,7 +185,7 @@ pub async fn get_latest_tag(
latest_remote_tag: Some(t.clone()),
..image.version_info.as_ref().unwrap().clone()
}),
time_ms: image.time_ms + time,
time_ms: image.time_ms + elapsed(start),
..image.clone()
},
token,
@@ -192,7 +199,7 @@ pub async fn get_latest_tag(
latest_remote_tag: Some(t.clone()),
..image.version_info.as_ref().unwrap().clone()
}),
time_ms: image.time_ms + time,
time_ms: image.time_ms + elapsed(start),
..image.clone()
}
}
@@ -218,11 +225,14 @@ pub async fn get_extra_tags(
.map(|link| parse_link(link.to_str().unwrap(), url));
let response_json = parse_json(&get_response_body(res).await);
let result = response_json["tags"]
.members()
.filter_map(|tag| Version::from_tag(&tag.to_string()))
.as_array()
.unwrap()
.iter()
.filter_map(|tag| Version::from_tag(tag.as_str().unwrap()))
.filter(|(tag, format_string)| match (base.minor, tag.minor) {
(Some(_), Some(_)) | (None, None) => {
matches!((base.patch, tag.patch), (Some(_), Some(_)) | (None, None)) && format_str == *format_string
matches!((base.patch, tag.patch), (Some(_), Some(_)) | (None, None))
&& format_str == *format_string
}
_ => false,
})