mod appstate; mod cli; mod file; mod sharry; use std::{ process::exit, sync::{ Arc, atomic::{AtomicBool, Ordering}, }, }; use clap::Parser; use console::style; use dialoguer::{Confirm, theme::ColorfulTheme}; use log::{error, info}; use ureq::Agent; use appstate::AppState; use cli::Cli; use sharry::ClientError; fn main() { println!( "{} to {}!", style("Welcome").magenta().bold(), style("ShrUpl").yellow().bold(), ); let stop = Arc::new(AtomicBool::new(false)); let stop_ctrlc = stop.clone(); ctrlc::set_handler(move || { stop_ctrlc.store(true, Ordering::SeqCst); info!("stopping as soon as possible ..."); }) .expect("Error setting Ctrl-C handler"); env_logger::init(); let args = Cli::parse(); info!("args: {args:?}"); info!("timeout: {:?}", args.get_timeout()); let agent: Agent = Agent::config_builder() .timeout_global(args.get_timeout()) .build() .into(); let mut state = AppState::try_resume(&args) .and_then(|state| { Confirm::with_theme(&ColorfulTheme::default()) .with_prompt("Continue previously stopped operation?") .default(true) .interact() .map_or(None, |b| b.then_some(state)) }) .unwrap_or_else(|| { stop.load(Ordering::SeqCst).then(|| exit(0)); match AppState::from_args(&args, &agent) { Ok(state) => { state.save().unwrap(); // HACK unwrap state } Err(e) => { if let Some(cause) = match e { ClientError::ResponseStatus { actual: _, expected: 403, } => Some("Alias ID"), // ClientError::FileIO(_) => Some("URL"), _ => None, } { info!("handling error: {e:?}"); println!( "{} probably wrong: {} – {:?}", style("Error!").red().bold(), style(cause).cyan().italic(), style(e.to_string()).yellow() ); } else { error!("unknown error: {e} – {e:?}"); println!("{}", style("Unknown Error!").red().bold()); } exit(1); } } }); println!( "{} uploading: {}", style("ShrUpl").yellow().bold(), style(state.file_names().join(", ")).magenta(), ); info!("continuing with state: {state:?}"); loop { match state.upload_chunk(&agent, args.chunk_size * 1024 * 1024) { Err(e) => error!("error: {e:?}"), Ok(None) => { info!("all uploads done"); state.clear().unwrap(); // HACK unwrap break; } _ => (), } state.save().unwrap(); // HACK unwrap stop.load(Ordering::SeqCst).then(|| exit(0)); } }