"retry-limit" CLI param

This commit is contained in:
Jörn-Michael Miehe 2025-06-18 18:44:24 +00:00
parent a4bef827d1
commit 4eb0627a5f
3 changed files with 21 additions and 2 deletions

View file

@ -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

View file

@ -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)
}

View file

@ -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");