Compare commits

..

No commits in common. "2f4c6b73aaa5986e34464979c4d966229bb4a801" and "2dab3c9ecdb2669d2387248243ffc040b917bd14" have entirely different histories.

3 changed files with 25 additions and 34 deletions

View file

@ -1,30 +1,24 @@
use std::time::Duration; use std::path::PathBuf;
use clap::{Parser, builder::PossibleValuesParser}; use clap::{Parser, builder::PossibleValuesParser};
use super::sharry::File;
#[derive(Parser, Debug, Hash)] #[derive(Parser, Debug, Hash)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
pub struct Cli { pub struct Cli {
/// Timeout in seconds for HTTP actions (set 0 or invalid to disable) /// Timeout in seconds for HTTP actions
#[arg( #[arg(short, long, value_name = "SECS")]
short, long, pub timeout: Option<u64>,
default_value = "10", value_name = "SECS",
value_parser = parse_seconds,
)]
timeout: Duration,
/// Protocol for Sharry instance /// Protocol for Sharry instance
#[arg( #[arg(
short, long, short, long,
default_value = "https", value_name = "VARIANT", default_value = "https", value_name = "VARIANT",
value_parser = PossibleValuesParser::new(["http", "https"]), value_parser = PossibleValuesParser::new(["http", "https"])
)] )]
pub protocol: String, pub protocol: String,
/// Name of the new share /// 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, pub name: String,
/// Description of the new share /// Description of the new share
@ -32,7 +26,7 @@ pub struct Cli {
pub description: Option<String>, pub description: Option<String>,
/// Maximum number of views for the new share /// Maximum number of views for the new share
#[arg(short, long, default_value_t = 100, value_name = "N")] #[arg(short, long, default_value_t = 10, value_name = "N")]
pub max_views: u32, pub max_views: u32,
/// Chunk size for uploading, in MiB /// Chunk size for uploading, in MiB
@ -46,20 +40,6 @@ pub struct Cli {
pub alias: String, pub alias: String,
/// Files to upload to the new share /// Files to upload to the new share
#[arg(value_name = "FILE", required = true, value_parser = parse_sharry_file)] #[arg(value_name = "FILE")]
pub files: Vec<File>, pub files: Vec<PathBuf>,
}
fn parse_seconds(data: &str) -> Result<Duration, String> {
data.parse().or(Ok(0)).map(Duration::from_secs)
}
fn parse_sharry_file(data: &str) -> Result<File, String> {
File::new(data).map_err(|e| e.to_string())
}
impl Cli {
pub fn get_timeout(&self) -> Option<Duration> {
(!self.timeout.is_zero()).then(|| self.timeout)
}
} }

View file

@ -1,32 +1,43 @@
mod cli; mod cli;
mod sharry; mod sharry;
use std::time::Duration;
use clap::Parser; use clap::Parser;
use log::{error, info}; use log::{error, info};
use ureq::Agent; use ureq::Agent;
use cli::Cli; use cli::Cli;
use sharry::{Alias, NewShareRequest, Share, Uri}; use sharry::{Alias, File, NewShareRequest, Share, Uri};
fn main() { fn main() {
env_logger::init(); env_logger::init();
let args = Cli::parse(); let args = Cli::parse();
info!("args: {args:?}"); info!("args: {args:?}");
info!("timeout: {:?}", args.get_timeout());
let agent: Agent = Agent::config_builder() let agent: Agent = Agent::config_builder()
.timeout_global(args.get_timeout()) .timeout_global(args.timeout.map(Duration::from_secs))
.build() .build()
.into(); .into();
let files: Vec<File> = (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 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 = NewShareRequest::new(args.name, args.description, args.max_views);
let share = Share::create(&agent, &alias, share).unwrap(); let share = Share::create(&agent, &alias, share).unwrap();
info!("share: {share:?}"); info!("share: {share:?}");
for file in args.files { for file in files {
let file = file.create(&agent, &share).unwrap(); let file = file.create(&agent, &share).unwrap();
info!("file: {file:?}"); info!("file: {file:?}");

View file

@ -16,7 +16,7 @@ use super::{
}; };
pub use chunks::{Chunk, FileChunks}; pub use chunks::{Chunk, FileChunks};
#[derive(Debug, Clone, Hash)] #[derive(Debug)]
pub struct File { pub struct File {
path: PathBuf, path: PathBuf,
name: String, name: String,