From 4eb0627a5f4d85c792fb10844ffd019937c50e28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Wed, 18 Jun 2025 18:44:24 +0000 Subject: [PATCH] "retry-limit" CLI param --- notes.md | 1 - src/cli.rs | 12 ++++++++++++ src/main.rs | 10 +++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/notes.md b/notes.md index 670ab4b..a131fa0 100644 --- a/notes.md +++ b/notes.md @@ -46,7 +46,6 @@ # Ideas - cli functions - - max retries => stop - "continue" and "new" flags to avoid user interaction - "quiet" flag to disable output entirely - "verbose" flag to adjust RUST_LOG for `shrupl` crate diff --git a/src/cli.rs b/src/cli.rs index 4742995..9117d38 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -36,6 +36,10 @@ pub struct Cli { )] protocol: String, + /// Number of times actions are retried + #[arg(short, long, default_value_t = 5, value_name = "N")] + retry_limit: u32, + /// Name of the new share #[arg(short, long, default_value = "ShrUpl Upload", value_name = "TEXT")] name: String, @@ -71,6 +75,7 @@ impl fmt::Debug for Cli { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Cli") .field("uri", &self.get_uri()) + .field("retry_limit", &self.retry_limit) .field("alias", &self.alias) .field("timeout", &self.get_timeout()) .field("chunk_size", &self.chunk_size) @@ -98,6 +103,13 @@ impl Cli { Uri::new(&self.protocol, &self.url) } + pub fn may_retry(&self, tries: u32) -> bool { + match self.retry_limit { + 0 => true, + limit => tries < limit, + } + } + pub fn get_share_request(&self) -> NewShareRequest { NewShareRequest::new(&self.name, self.description.as_ref(), self.max_views) } diff --git a/src/main.rs b/src/main.rs index 8db3d12..8925510 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,8 +73,13 @@ fn main() { println!("{} is uploading: {fns_magenta}", *SHRUPL); let mut buffer = vec![0; args.chunk_size * 1024 * 1024]; + let mut tries = 0; loop { + if !args.may_retry(tries) { + Log::error("Retry limit reached!"); + } + match state.upload_chunk(&mut buffer) { Err(e) => { // TODO better error handling (this will just retry endlessly) @@ -82,7 +87,9 @@ fn main() { Log::handle(&e); if let Some(s) = state.rewind() { - trace!("State rewound, retrying last chunk"); + tries += 1; + trace!("State rewound, retrying last chunk (tried: {tries})"); + state = s; } else { Log::error("Failed to retry chunk!"); @@ -90,6 +97,7 @@ fn main() { } Ok(false) => { trace!("chunk uploaded"); + tries = 0; } Ok(true) => { info!("all uploads done");