diff --git a/src/cli.rs b/src/cli.rs index dcbf5d2..eb31672 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,13 +1,15 @@ -use std::path::PathBuf; +use std::time::Duration; use clap::{Parser, builder::PossibleValuesParser}; +use super::sharry::File; + #[derive(Parser, Debug, Hash)] #[command(version, about, long_about = None)] pub struct Cli { /// Timeout in seconds for HTTP actions - #[arg(short, long, value_name = "SECS")] - pub timeout: Option, + #[arg(short, long, value_name = "SECS", value_parser = parse_seconds)] + pub timeout: Option, /// Protocol for Sharry instance #[arg( @@ -18,7 +20,7 @@ pub struct Cli { pub protocol: String, /// Name of the new share - #[arg(short, long, default_value = "shrupl upload", value_name = "TEXT")] + #[arg(short, long, default_value = "ShrUpl Upload", value_name = "TEXT")] pub name: String, /// Description of the new share @@ -26,7 +28,7 @@ pub struct Cli { pub description: Option, /// Maximum number of views for the new share - #[arg(short, long, default_value_t = 10, value_name = "N")] + #[arg(short, long, default_value_t = 100, value_name = "N")] pub max_views: u32, /// Chunk size for uploading, in MiB @@ -40,6 +42,16 @@ pub struct Cli { pub alias: String, /// Files to upload to the new share - #[arg(value_name = "FILE")] - pub files: Vec, + #[arg(value_name = "FILE", required = true, value_parser = parse_sharry_file)] + pub files: Vec, +} + +fn parse_seconds(data: &str) -> Result { + data.parse() + .map(Duration::from_secs) + .map_err(|e| e.to_string()) +} + +fn parse_sharry_file(data: &str) -> Result { + File::new(data).map_err(|e| e.to_string()) } diff --git a/src/main.rs b/src/main.rs index e94ed2f..67d032c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,12 @@ mod cli; mod sharry; -use std::time::Duration; - use clap::Parser; use log::{error, info}; use ureq::Agent; use cli::Cli; -use sharry::{Alias, File, NewShareRequest, Share, Uri}; +use sharry::{Alias, NewShareRequest, Share, Uri}; fn main() { env_logger::init(); @@ -17,27 +15,17 @@ fn main() { info!("args: {args:?}"); let agent: Agent = Agent::config_builder() - .timeout_global(args.timeout.map(Duration::from_secs)) + .timeout_global(args.timeout) .build() .into(); - let files: Vec = (args.files.iter()) - .filter(|&p| p.exists()) - .map(File::new) - .map(Result::unwrap) - .collect(); - - if files.is_empty() { - panic!("NO FILES GIVEN"); - } - let alias = Alias::new(Uri::with_protocol(args.protocol, args.url), args.alias); let share = NewShareRequest::new(args.name, args.description, args.max_views); let share = Share::create(&agent, &alias, share).unwrap(); info!("share: {share:?}"); - for file in files { + for file in args.files { let file = file.create(&agent, &share).unwrap(); info!("file: {file:?}"); diff --git a/src/sharry/file/mod.rs b/src/sharry/file/mod.rs index 1148405..09e08c0 100644 --- a/src/sharry/file/mod.rs +++ b/src/sharry/file/mod.rs @@ -16,7 +16,7 @@ use super::{ }; pub use chunks::{Chunk, FileChunks}; -#[derive(Debug)] +#[derive(Debug, Clone, Hash)] pub struct File { path: PathBuf, name: String,