CLI ergonomy
This commit is contained in:
parent
2dab3c9ecd
commit
763c8f689e
3 changed files with 23 additions and 23 deletions
26
src/cli.rs
26
src/cli.rs
|
|
@ -1,13 +1,15 @@
|
||||||
use std::path::PathBuf;
|
use std::time::Duration;
|
||||||
|
|
||||||
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
|
/// Timeout in seconds for HTTP actions
|
||||||
#[arg(short, long, value_name = "SECS")]
|
#[arg(short, long, value_name = "SECS", value_parser = parse_seconds)]
|
||||||
pub timeout: Option<u64>,
|
pub timeout: Option<Duration>,
|
||||||
|
|
||||||
/// Protocol for Sharry instance
|
/// Protocol for Sharry instance
|
||||||
#[arg(
|
#[arg(
|
||||||
|
|
@ -18,7 +20,7 @@ pub struct Cli {
|
||||||
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
|
||||||
|
|
@ -26,7 +28,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 = 10, value_name = "N")]
|
#[arg(short, long, default_value_t = 100, value_name = "N")]
|
||||||
pub max_views: u32,
|
pub max_views: u32,
|
||||||
|
|
||||||
/// Chunk size for uploading, in MiB
|
/// Chunk size for uploading, in MiB
|
||||||
|
|
@ -40,6 +42,16 @@ 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")]
|
#[arg(value_name = "FILE", required = true, value_parser = parse_sharry_file)]
|
||||||
pub files: Vec<PathBuf>,
|
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())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
18
src/main.rs
18
src/main.rs
|
|
@ -1,14 +1,12 @@
|
||||||
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, File, NewShareRequest, Share, Uri};
|
use sharry::{Alias, NewShareRequest, Share, Uri};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
@ -17,27 +15,17 @@ fn main() {
|
||||||
info!("args: {args:?}");
|
info!("args: {args:?}");
|
||||||
|
|
||||||
let agent: Agent = Agent::config_builder()
|
let agent: Agent = Agent::config_builder()
|
||||||
.timeout_global(args.timeout.map(Duration::from_secs))
|
.timeout_global(args.timeout)
|
||||||
.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 files {
|
for file in args.files {
|
||||||
let file = file.create(&agent, &share).unwrap();
|
let file = file.create(&agent, &share).unwrap();
|
||||||
info!("file: {file:?}");
|
info!("file: {file:?}");
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ use super::{
|
||||||
};
|
};
|
||||||
pub use chunks::{Chunk, FileChunks};
|
pub use chunks::{Chunk, FileChunks};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone, Hash)]
|
||||||
pub struct File {
|
pub struct File {
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
name: String,
|
name: String,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue