m/cup
1
0
mirror of https://github.com/sergi0g/cup.git synced 2025-11-17 01:23:39 -05:00

Add option to refresh all servers

This commit is contained in:
Sergio
2025-02-21 17:05:32 +02:00
parent 756462cd7c
commit 685219ea62
3 changed files with 35 additions and 12 deletions

View File

@@ -12,21 +12,38 @@ use crate::{
}; };
/// Fetches image data from other Cup instances /// Fetches image data from other Cup instances
async fn get_remote_updates(ctx: &Context, client: &Client) -> Vec<Update> { async fn get_remote_updates(ctx: &Context, client: &Client, refresh: bool) -> Vec<Update> {
let mut remote_images = Vec::new(); let mut remote_images = Vec::new();
let handles: Vec<_> = ctx.config.servers let handles: Vec<_> = ctx.config.servers
.iter() .iter()
.map(|(name, url)| async { .map(|(name, url)| async {
let url = if url.starts_with("http://") || url.starts_with("https://") { let base_url = if url.starts_with("http://") || url.starts_with("https://") {
format!("{}/api/v3/json", url.trim_end_matches('/')) format!("{}/api/v3/", url.trim_end_matches('/'))
} else { } else {
format!("https://{}/api/v3/json", url.trim_end_matches('/')) format!("https://{}/api/v3/", url.trim_end_matches('/'))
}; };
match client.get(&url, vec![], false).await { let json_url = base_url.clone() + "json";
if refresh {
let refresh_url = base_url + "refresh";
match client.get(&(&refresh_url), vec![], false).await {
Ok(response) => {
if response.status() != 200 {
ctx.logger.warn(format!("GET {}: Failed to refresh server. Server returned invalid response code: {}",refresh_url,response.status()));
return Vec::new();
}
},
Err(e) => {
ctx.logger.warn(format!("GET {}: Failed to refresh server. {}", refresh_url, e));
return Vec::new();
},
}
}
match client.get(&json_url, vec![], 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: {}",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);
@@ -46,7 +63,7 @@ async fn get_remote_updates(ctx: &Context, client: &Client) -> Vec<Update> {
Vec::new() Vec::new()
} }
Err(e) => { Err(e) => {
ctx.logger.warn(format!("Failed to fetch updates from server. {}", e)); ctx.logger.warn(format!("GET {}: Failed to fetch updates from server. {}", json_url, e));
Vec::new() Vec::new()
}, },
} }
@@ -61,7 +78,11 @@ async fn get_remote_updates(ctx: &Context, client: &Client) -> Vec<Update> {
} }
/// 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(references: &Option<Vec<String>>, ctx: &Context) -> Vec<Update> { pub async fn get_updates(
references: &Option<Vec<String>>,
refresh: bool,
ctx: &Context,
) -> Vec<Update> {
let client = Client::new(ctx); let client = Client::new(ctx);
// Get local images // Get local images
@@ -82,7 +103,7 @@ pub async fn get_updates(references: &Option<Vec<String>>, ctx: &Context) -> Vec
// Get remote images from other servers // Get remote images from other servers
let remote_updates = if !ctx.config.servers.is_empty() { let remote_updates = if !ctx.config.servers.is_empty() {
ctx.logger.debug("Fetching updates from remote servers"); ctx.logger.debug("Fetching updates from remote servers");
get_remote_updates(ctx, &client).await get_remote_updates(ctx, &client, refresh).await
} else { } else {
Vec::new() Vec::new()
}; };

View File

@@ -34,6 +34,8 @@ struct Cli {
command: Option<Commands>, command: Option<Commands>,
#[arg(short, long)] #[arg(short, long)]
debug: bool, debug: bool,
#[arg(long)]
refresh: bool,
} }
#[derive(Subcommand)] #[derive(Subcommand)]
@@ -98,12 +100,12 @@ async fn main() {
} }
match *raw || cli.debug { match *raw || cli.debug {
true => { true => {
let updates = get_updates(references, &ctx).await; let updates = get_updates(references, cli.refresh, &ctx).await;
print_raw_updates(&updates); print_raw_updates(&updates);
} }
false => { false => {
let spinner = Spinner::new(); let spinner = Spinner::new();
let updates = get_updates(references, &ctx).await; let updates = get_updates(references, cli.refresh, &ctx).await;
spinner.succeed(); spinner.succeed();
print_updates(&updates, icons); print_updates(&updates, icons);
ctx.logger.info(format!("✨ Checked {} images in {}ms", updates.len(), start.elapsed().unwrap().as_millis())); ctx.logger.info(format!("✨ Checked {} images in {}ms", updates.len(), start.elapsed().unwrap().as_millis()));

View File

@@ -173,7 +173,7 @@ impl ServerData {
if !self.raw_updates.is_empty() { if !self.raw_updates.is_empty() {
self.ctx.logger.info("Refreshing data"); self.ctx.logger.info("Refreshing data");
} }
let updates = sort_update_vec(&get_updates(&None, &self.ctx).await); let updates = sort_update_vec(&get_updates(&None, true, &self.ctx).await);
self.ctx.logger.info(format!( self.ctx.logger.info(format!(
"✨ Checked {} images in {}ms", "✨ Checked {} images in {}ms",
updates.len(), updates.len(),