Compare commits

...

3 commits

4 changed files with 103 additions and 36 deletions

44
Cargo.lock generated
View file

@ -152,6 +152,19 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
[[package]]
name = "console"
version = "0.15.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "054ccb5b10f9f2cbf51eb355ca1d05c2d279ce1804688d0db74b4733a5aeafd8"
dependencies = [
"encode_unicode",
"libc",
"once_cell",
"unicode-width",
"windows-sys 0.59.0",
]
[[package]]
name = "cookie"
version = "0.18.1"
@ -209,6 +222,17 @@ dependencies = [
"powerfmt",
]
[[package]]
name = "dialoguer"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "658bce805d770f407bc62102fca7c2c64ceef2fbcb2b8bd19d2765ce093980de"
dependencies = [
"console",
"shell-words",
"thiserror",
]
[[package]]
name = "dirs-next"
version = "2.0.0"
@ -250,6 +274,12 @@ dependencies = [
"litrs",
]
[[package]]
name = "encode_unicode"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0"
[[package]]
name = "env_filter"
version = "0.1.3"
@ -766,6 +796,12 @@ dependencies = [
"serde",
]
[[package]]
name = "shell-words"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
[[package]]
name = "shlex"
version = "1.3.0"
@ -777,7 +813,9 @@ name = "shrupl"
version = "0.1.0"
dependencies = [
"clap",
"console",
"ctrlc",
"dialoguer",
"dirs-next",
"env_logger",
"log",
@ -899,6 +937,12 @@ version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "unicode-width"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd"
[[package]]
name = "untrusted"
version = "0.9.0"

View file

@ -6,7 +6,9 @@ description = "ShrUpl is a tool to upload files to a Sharry Instance through a p
[dependencies]
clap = { version = "4.5.38", features = ["derive"] }
console = { version = "0.15.11", default-features = false }
ctrlc = { version = "3.4.7", features = ["termination"] }
dialoguer = { version = "0.11.0", default-features = false }
dirs-next = "2.0.0"
env_logger = "0.11.8"
log = "0.4.27"

View file

@ -69,12 +69,11 @@ impl AppState {
.ok()
}
pub fn from_args(args: &Cli, http: &ureq::Agent) -> Result<Self, String> {
pub fn from_args(args: &Cli, http: &ureq::Agent) -> Result<Self, ureq::Error> {
let file_name = Self::cache_file(args);
let alias = args.get_alias();
let share = Share::create(http, &alias, args.get_share_request())
.map_err(|e| format!("could not create share: {e}"))?;
let share = Share::create(http, &alias, args.get_share_request())?;
let files: VecDeque<_> = args.files.clone().into_iter().map(FileState::C).collect();
@ -96,11 +95,7 @@ impl AppState {
.start_upload(http, &self.alias, &self.share)
.unwrap(),
Some(FileState::U(uploading)) => uploading,
None => {
self.share.notify(http, &self.alias).unwrap();
return Ok(None);
}
None => return Ok(None),
};
debug!("{uploading} chunk {chunk_size}");
@ -116,7 +111,9 @@ impl AppState {
}
ChunkState::Finished(path) => {
debug!("Finished {:?}!", path.display());
Ok(Some(()))
self.share.notify(http, &self.alias).unwrap();
Ok(self.files.front().map(drop))
}
}
}
@ -128,14 +125,14 @@ impl AppState {
let mut file = fs::File::create(&self.file_name)?;
file.write_all(json.as_bytes())?;
trace!("successfully saved AppState");
trace!("updated {:?}", self.file_name.display());
Ok(())
}
pub fn clear(self) -> io::Result<()> {
fs::remove_file(self.file_name)?;
fs::remove_file(&self.file_name)?;
trace!("successfully cleared AppState");
trace!("removed {:?}", self.file_name.display());
Ok(())
}
}

View file

@ -11,6 +11,8 @@ use std::{
};
use clap::Parser;
use console::style;
use dialoguer::{Confirm, theme::ColorfulTheme};
use log::{error, info};
use ureq::Agent;
@ -18,11 +20,14 @@ use appstate::AppState;
use cli::Cli;
fn main() {
let running = Arc::new(AtomicBool::new(true));
let r = running.clone();
let stop = Arc::new(AtomicBool::new(false));
ctrlc::set_handler(move || r.store(false, Ordering::SeqCst))
.expect("Error setting Ctrl-C handler");
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");
env_logger::init();
@ -35,35 +40,54 @@ fn main() {
.build()
.into();
let mut state = match AppState::try_resume(&args) {
Some(state) => {
info!("loaded state: {state:?}");
state
}
None => AppState::from_args(&args, &agent).unwrap_or_else(|e| {
error!("could not create new state: {e}");
exit(1);
}),
};
let mut state = AppState::try_resume(&args)
.and_then(|state| {
Confirm::with_theme(&ColorfulTheme::default())
.with_prompt("Previous operation found. Continue?")
.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) => {
match e {
ureq::Error::StatusCode(403) => println!(
"{} probably wrong: {}",
style("Error!").red(),
style("Alias ID").cyan()
),
ureq::Error::Io(e) => println!(
"{} probably wrong: {} {}",
style("Error!").red(),
style("URL").cyan(),
style(e).yellow()
),
_ => error!("unknown error: {e}"),
}
info!("could not create new state!");
exit(1);
}
});
info!("continuing with state: {state:?}");
state.save().unwrap();
loop {
match state.upload_chunk(&agent, args.chunk_size * 1024 * 1024) {
Ok(None) => break,
Err(e) => error!("error: {e:?}"),
Ok(None) => {
info!("all uploads done");
state.clear().unwrap();
break;
}
_ => (),
}
state.save().unwrap();
if !running.load(Ordering::SeqCst) {
info!("terminating ...");
exit(0);
}
stop.load(Ordering::SeqCst).then(|| exit(0));
}
info!("uploads done");
state.clear().unwrap();
}