Compare commits

...

2 commits

Author SHA1 Message Date
2f4c6b73aa CLI ergonomy: timeout value 2025-05-28 13:42:31 +00:00
763c8f689e CLI ergonomy 2025-05-28 12:35:35 +00:00
3 changed files with 34 additions and 25 deletions

View file

@ -1,24 +1,30 @@
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<u64>,
/// Timeout in seconds for HTTP actions (set 0 or invalid to disable)
#[arg(
short, long,
default_value = "10", value_name = "SECS",
value_parser = parse_seconds,
)]
timeout: Duration,
/// Protocol for Sharry instance
#[arg(
short, long,
default_value = "https", value_name = "VARIANT",
value_parser = PossibleValuesParser::new(["http", "https"])
value_parser = PossibleValuesParser::new(["http", "https"]),
)]
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 +32,7 @@ pub struct Cli {
pub description: Option<String>,
/// 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 +46,20 @@ pub struct Cli {
pub alias: String,
/// Files to upload to the new share
#[arg(value_name = "FILE")]
pub files: Vec<PathBuf>,
#[arg(value_name = "FILE", required = true, value_parser = parse_sharry_file)]
pub files: Vec<File>,
}
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,43 +1,32 @@
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();
let args = Cli::parse();
info!("args: {args:?}");
info!("timeout: {:?}", args.get_timeout());
let agent: Agent = Agent::config_builder()
.timeout_global(args.timeout.map(Duration::from_secs))
.timeout_global(args.get_timeout())
.build()
.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 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:?}");

View file

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