CLI ergonomy

This commit is contained in:
Jörn-Michael Miehe 2025-05-28 12:35:35 +00:00
parent 2dab3c9ecd
commit 763c8f689e
3 changed files with 23 additions and 23 deletions

View file

@ -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<u64>,
#[arg(short, long, value_name = "SECS", value_parser = parse_seconds)]
pub timeout: Option<Duration>,
/// 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<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 +42,16 @@ 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()
.map(Duration::from_secs)
.map_err(|e| e.to_string())
}
fn parse_sharry_file(data: &str) -> Result<File, String> {
File::new(data).map_err(|e| e.to_string())
}

View file

@ -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<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,