m/cup
1
0
mirror of https://github.com/sergi0g/cup.git synced 2025-11-13 23:53:48 -05:00

refactor: use array slices instead of vectors wherever possible

This commit is contained in:
Sergio
2025-03-19 19:20:29 +02:00
parent ccf825df24
commit 77a07013a9
3 changed files with 22 additions and 19 deletions

View File

@@ -26,10 +26,10 @@ async fn get_remote_updates(ctx: &Context, client: &Client, refresh: bool) -> Ve
let json_url = base_url.clone() + "json"; let json_url = base_url.clone() + "json";
if refresh { if refresh {
let refresh_url = base_url + "refresh"; let refresh_url = base_url + "refresh";
match client.get(&(&refresh_url), vec![], false).await { match client.get(&refresh_url, &[], false).await {
Ok(response) => { Ok(response) => {
if response.status() != 200 { if response.status() != 200 {
ctx.logger.warn(format!("GET {}: Failed to refresh server. Server returned invalid response code: {}",refresh_url,response.status())); ctx.logger.warn(format!("GET {}: Failed to refresh server. Server returned invalid response code: {}", refresh_url, response.status()));
return Vec::new(); return Vec::new();
} }
}, },
@@ -40,10 +40,10 @@ async fn get_remote_updates(ctx: &Context, client: &Client, refresh: bool) -> Ve
} }
} }
match client.get(&json_url, vec![], false).await { match client.get(&json_url, &[], false).await {
Ok(response) => { Ok(response) => {
if response.status() != 200 { if response.status() != 200 {
ctx.logger.warn(format!("GET {}: Failed to fetch updates from server. Server returned invalid response code: {}",json_url,response.status())); ctx.logger.warn(format!("GET {}: Failed to fetch updates from server. Server returned invalid response code: {}", json_url, response.status()));
return Vec::new(); return Vec::new();
} }
let json = parse_json(&get_response_body(response).await); let json = parse_json(&get_response_body(response).await);

View File

@@ -42,7 +42,7 @@ impl Client {
&self, &self,
url: &str, url: &str,
method: RequestMethod, method: RequestMethod,
headers: Vec<(&str, Option<&str>)>, headers: &[(&str, Option<&str>)],
ignore_401: bool, ignore_401: bool,
) -> Result<Response, String> { ) -> Result<Response, String> {
let mut request = match method { let mut request = match method {
@@ -51,7 +51,7 @@ impl Client {
}; };
for (name, value) in headers { for (name, value) in headers {
if let Some(v) = value { if let Some(v) = value {
request = request.header(name, v) request = request.header(*name, *v)
} }
} }
match request.send().await { match request.send().await {
@@ -114,7 +114,7 @@ impl Client {
pub async fn get( pub async fn get(
&self, &self,
url: &str, url: &str,
headers: Vec<(&str, Option<&str>)>, headers: &[(&str, Option<&str>)],
ignore_401: bool, ignore_401: bool,
) -> Result<Response, String> { ) -> Result<Response, String> {
self.request(url, RequestMethod::GET, headers, ignore_401) self.request(url, RequestMethod::GET, headers, ignore_401)
@@ -124,7 +124,7 @@ impl Client {
pub async fn head( pub async fn head(
&self, &self,
url: &str, url: &str,
headers: Vec<(&str, Option<&str>)>, headers: &[(&str, Option<&str>)],
) -> Result<Response, String> { ) -> Result<Response, String> {
self.request(url, RequestMethod::HEAD, headers, false).await self.request(url, RequestMethod::HEAD, headers, false).await
} }

View File

@@ -22,7 +22,7 @@ use crate::{
pub async fn check_auth(registry: &str, ctx: &Context, client: &Client) -> Option<String> { pub async fn check_auth(registry: &str, ctx: &Context, client: &Client) -> Option<String> {
let protocol = get_protocol(registry, &ctx.config.registries); let protocol = get_protocol(registry, &ctx.config.registries);
let url = format!("{}://{}/v2/", protocol, registry); let url = format!("{}://{}/v2/", protocol, registry);
let response = client.get(&url, Vec::new(), true).await; let response = client.get(&url, &[], true).await;
match response { match response {
Ok(response) => { Ok(response) => {
let status = response.status(); let status = response.status();
@@ -57,9 +57,9 @@ pub async fn get_latest_digest(
protocol, &image.parts.registry, &image.parts.repository, &image.parts.tag protocol, &image.parts.registry, &image.parts.repository, &image.parts.tag
); );
let authorization = to_bearer_string(&token); let authorization = to_bearer_string(&token);
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 headers = [("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 response = client.head(&url, &headers).await;
let time = start.elapsed().unwrap().as_millis() as u32; let time = start.elapsed().unwrap().as_millis() as u32;
ctx.logger.debug(format!( ctx.logger.debug(format!(
"Checked for digest update to {} in {}ms", "Checked for digest update to {} in {}ms",
@@ -95,7 +95,7 @@ pub async fn get_latest_digest(
} }
pub async fn get_token( pub async fn get_token(
images: &Vec<&Image>, images: &[&Image],
auth_url: &str, auth_url: &str,
credentials: &Option<String>, credentials: &Option<String>,
client: &Client, client: &Client,
@@ -105,9 +105,9 @@ pub async fn get_token(
url = format!("{}&scope=repository:{}:pull", url, image.parts.repository); url = format!("{}&scope=repository:{}:pull", url, image.parts.repository);
} }
let authorization = credentials.as_ref().map(|creds| format!("Basic {}", creds)); let authorization = credentials.as_ref().map(|creds| format!("Basic {}", creds));
let headers = vec![("Authorization", authorization.as_deref())]; let headers = [("Authorization", authorization.as_deref())];
let response = client.get(&url, headers, false).await; let response = client.get(&url, &headers, false).await;
let response_json = match response { let response_json = match response {
Ok(response) => parse_json(&get_response_body(response).await), Ok(response) => parse_json(&get_response_body(response).await),
Err(_) => error!("GET {}: Request failed!", url), Err(_) => error!("GET {}: Request failed!", url),
@@ -131,7 +131,7 @@ pub async fn get_latest_tag(
protocol, &image.parts.registry, &image.parts.repository, protocol, &image.parts.registry, &image.parts.repository,
); );
let authorization = to_bearer_string(&token); let authorization = to_bearer_string(&token);
let headers = vec![ let headers = [
("Accept", Some("application/json")), ("Accept", Some("application/json")),
("Authorization", authorization.as_deref()), ("Authorization", authorization.as_deref()),
]; ];
@@ -147,7 +147,7 @@ pub async fn get_latest_tag(
)); ));
let (new_tags, next) = match get_extra_tags( let (new_tags, next) = match get_extra_tags(
&next_url.unwrap(), &next_url.unwrap(),
headers.clone(), &headers,
base, base,
&image.version_info.as_ref().unwrap().format_str, &image.version_info.as_ref().unwrap().format_str,
client, client,
@@ -205,18 +205,21 @@ pub async fn get_latest_tag(
} }
} }
} }
None => error!("Image {} has no remote version tags! Local tag: {}", image.reference, image.parts.tag), None => error!(
"Image {} has no remote version tags! Local tag: {}",
image.reference, image.parts.tag
),
} }
} }
pub async fn get_extra_tags( pub async fn get_extra_tags(
url: &str, url: &str,
headers: Vec<(&str, Option<&str>)>, headers: &[(&str, Option<&str>)],
base: &Version, base: &Version,
format_str: &str, format_str: &str,
client: &Client, client: &Client,
) -> Result<(Vec<Version>, Option<String>), String> { ) -> Result<(Vec<Version>, Option<String>), String> {
let response = client.get(url, headers, false).await; let response = client.get(url, &headers, false).await;
match response { match response {
Ok(res) => { Ok(res) => {