shrupl/src/main.rs

94 lines
2.5 KiB
Rust
Raw Normal View History

2025-06-02 23:57:17 +00:00
mod appstate;
2025-05-28 00:07:59 +00:00
mod cli;
2025-05-22 17:34:44 +00:00
mod sharry;
2025-06-05 01:44:39 +00:00
use std::{
process::exit,
sync::{
Arc,
atomic::{AtomicBool, Ordering},
},
2025-06-05 01:34:12 +00:00
};
2025-05-27 14:01:09 +00:00
use clap::Parser;
use console::style;
2025-06-05 11:20:27 +00:00
use dialoguer::{Confirm, theme::ColorfulTheme};
2025-06-05 01:13:54 +00:00
use log::{error, info};
2025-05-22 17:34:44 +00:00
use ureq::Agent;
2025-06-02 23:57:17 +00:00
use appstate::AppState;
2025-05-28 00:07:59 +00:00
use cli::Cli;
2025-05-27 14:01:09 +00:00
2025-05-17 23:57:52 +00:00
fn main() {
2025-06-05 10:01:09 +00:00
let stop = Arc::new(AtomicBool::new(false));
2025-06-05 01:34:12 +00:00
2025-06-05 10:01:09 +00:00
let stop_ctrlc = stop.clone();
ctrlc::set_handler(move || {
stop_ctrlc.store(true, Ordering::SeqCst);
info!("stopping after chunk ...");
})
.expect("Error setting Ctrl-C handler");
2025-06-05 01:34:12 +00:00
2025-05-22 17:34:44 +00:00
env_logger::init();
2025-05-28 00:07:59 +00:00
let args = Cli::parse();
info!("args: {args:?}");
2025-05-28 13:42:31 +00:00
info!("timeout: {:?}", args.get_timeout());
2025-05-28 00:07:59 +00:00
2025-05-22 17:34:44 +00:00
let agent: Agent = Agent::config_builder()
2025-05-28 13:42:31 +00:00
.timeout_global(args.get_timeout())
2025-05-22 17:34:44 +00:00
.build()
.into();
2025-06-05 11:20:27 +00:00
let mut state = AppState::try_resume(&args)
.and_then(|state| {
Confirm::with_theme(&ColorfulTheme::default())
.with_prompt("Previous operation found. Continue?")
2025-06-05 11:20:27 +00:00
.default(true)
.interact()
.map_or(None, |b| b.then_some(state))
})
.unwrap_or_else(|| match AppState::from_args(&args, &agent) {
Ok(state) => {
state.save().unwrap();
state
}
Err(e) => {
if let Some(cause) = match e {
ureq::Error::StatusCode(403) => Some("Alias ID"),
ureq::Error::Io(_) => Some("URL"),
_ => None,
} {
println!(
"{} probably wrong: {} {:?}",
style("Error!").red(),
style(cause).cyan(),
style(e.to_string()).yellow()
);
} else {
error!("unknown error: {e}");
println!("{}", style("Unknown Error!").red());
}
2025-06-05 11:20:27 +00:00
exit(1);
}
});
2025-06-04 21:02:35 +00:00
info!("continuing with state: {state:?}");
2025-06-05 01:13:54 +00:00
loop {
match state.upload_chunk(&agent, args.chunk_size * 1024 * 1024) {
Err(e) => error!("error: {e:?}"),
2025-06-05 11:20:27 +00:00
Ok(None) => {
info!("all uploads done");
state.clear().unwrap();
break;
}
2025-06-05 01:13:54 +00:00
_ => (),
}
2025-06-05 01:13:54 +00:00
state.save().unwrap();
2025-06-05 10:01:09 +00:00
stop.load(Ordering::SeqCst).then(|| exit(0));
2025-05-26 20:31:22 +00:00
}
2025-05-17 23:57:52 +00:00
}