use std::path::PathBuf;
use clap::{ArgAction, Args, Parser, Subcommand};
use shaul::{
Auth, Checksum, Error, Header, ParseOptions, RequestOptions, Result, TaskOptions, VerifyMode,
};
use crate::support::{parse_basic_auth, parse_checksum, parse_headers, read_header_file};
#[derive(Parser, Debug)]
#[command(
name = "shaul",
version,
about = "A unified, resumable download engine for HTTP, BitTorrent and model hubs",
disable_help_subcommand = true
)]
pub struct Cli {
#[arg(long, global = true, value_name = "PATH")]
pub config: Option<PathBuf>,
#[arg(long, global = true)]
pub json: bool,
#[arg(long, short = 'q', global = true)]
pub quiet: bool,
#[arg(long, short = 'v', global = true, action = ArgAction::Count)]
pub verbose: u8,
#[command(subcommand)]
pub command: Commands,
}
#[derive(Subcommand, Debug)]
pub enum Commands {
Download(DownloadArgs),
Add(DownloadArgs),
Run(RunArgs),
List,
Status(IdArgs),
Pause(IdArgs),
Resume(IdArgs),
Cancel(IdArgs),
Info(InfoArgs),
Config(ConfigArgs),
Completions { shell: clap_complete::Shell },
}
#[derive(Args, Debug, Clone, Default)]
pub struct RequestArgs {
#[arg(long = "header", short = 'H', value_name = "NAME: VALUE")]
pub header: Vec<String>,
#[arg(long, value_name = "PATH")]
pub header_file: Option<PathBuf>,
#[arg(long, value_name = "TOKEN")]
pub bearer_token: Option<String>,
#[arg(long, value_name = "USER:PASSWORD")]
pub basic: Option<String>,
#[arg(long)]
pub allow_cross_origin_auth: bool,
}
#[derive(Args, Debug)]
pub struct DownloadArgs {
#[arg(value_name = "LINK", required = true)]
pub links: Vec<String>,
#[arg(long, short = 'd', value_name = "DIR")]
pub dir: Option<PathBuf>,
#[arg(long, short = 'o', value_name = "PATH")]
pub output: Option<PathBuf>,
#[arg(long, short = 'n', value_name = "N")]
pub connections: Option<usize>,
#[arg(long, conflicts_with = "no_resume")]
pub resume: bool,
#[arg(long)]
pub no_resume: bool,
#[arg(long, conflicts_with = "no_verify")]
pub verify: bool,
#[arg(long)]
pub no_verify: bool,
#[arg(long, conflicts_with = "checksum_file", value_name = "ALG:HEX")]
pub checksum: Option<String>,
#[arg(long, value_name = "PATH")]
pub checksum_file: Option<PathBuf>,
#[arg(long, conflicts_with = "no_extract")]
pub extract: bool,
#[arg(long)]
pub no_extract: bool,
#[arg(long, value_name = "DIR")]
pub extract_dir: Option<PathBuf>,
#[arg(long)]
pub delete_archive: bool,
#[arg(long, conflicts_with = "no_cache")]
pub cache: bool,
#[arg(long)]
pub no_cache: bool,
#[arg(long, conflicts_with = "rename")]
pub overwrite: bool,
#[arg(long)]
pub rename: bool,
#[arg(long)]
pub force: bool,
#[arg(long)]
pub revision: Option<String>,
#[arg(long)]
pub include: Vec<String>,
#[arg(long)]
pub exclude: Vec<String>,
#[command(flatten)]
pub request: RequestArgs,
}
#[derive(Args, Debug)]
pub struct InfoArgs {
#[arg(value_name = "LINK")]
pub link: String,
#[arg(long, value_name = "ALG:HEX")]
pub checksum: Option<String>,
#[arg(long)]
pub revision: Option<String>,
#[arg(long)]
pub include: Vec<String>,
#[arg(long)]
pub exclude: Vec<String>,
#[command(flatten)]
pub request: RequestArgs,
}
#[derive(Args, Debug)]
pub struct RunArgs {
#[arg(long, short = 'j', default_value_t = 1, value_name = "N")]
pub jobs: usize,
#[arg(long)]
pub once: bool,
}
#[derive(Args, Debug)]
pub struct IdArgs {
pub id: String,
}
#[derive(Args, Debug)]
pub struct ConfigArgs {
#[command(subcommand)]
pub command: ConfigCmd,
}
#[derive(Subcommand, Debug)]
pub enum ConfigCmd {
Path,
Show,
Get { key: String },
Set { key: String, value: String },
Init {
#[arg(long)]
force: bool,
},
Edit,
}
impl RequestArgs {
pub fn to_settings(&self) -> Result<RequestOptions> {
let mut headers = parse_headers(&self.header)?;
if let Some(path) = &self.header_file {
headers.extend(read_header_file(path)?);
}
let auth = match (&self.bearer_token, &self.basic) {
(Some(_), Some(_)) => {
return Err(Error::Config(
"use either --bearer-token or --basic, not both".to_string(),
));
}
(Some(token), None) => Some(Auth::bearer(token.clone())),
(None, Some(value)) => Some(parse_basic_auth(value)?),
(None, None) => None,
};
Ok(RequestOptions {
headers,
auth,
allow_cross_origin_auth: self.allow_cross_origin_auth,
})
}
}
impl DownloadArgs {
pub fn to_task_options(&self) -> Result<TaskOptions> {
let resume = if self.no_resume {
Some(false)
} else if self.resume {
Some(true)
} else {
None
};
let verify = if self.no_verify {
Some(VerifyMode::Disabled)
} else if self.verify {
Some(VerifyMode::Require)
} else {
None
};
let extract = if self.no_extract {
Some(false)
} else if self.extract {
Some(true)
} else {
None
};
let checksum = parse_checksum(&self.checksum, &self.checksum_file)?;
let cache = if self.no_cache {
Some(false)
} else if self.cache {
Some(true)
} else {
None
};
Ok(TaskOptions {
output: self.output.clone(),
connections: self.connections,
resume,
verify,
checksum,
extract,
extract_dir: self.extract_dir.clone(),
delete_archive: self.delete_archive.then_some(true),
cache,
request: self.request.to_settings()?,
revision: self.revision.clone(),
include: self.include.clone(),
exclude: self.exclude.clone(),
overwrite: self.overwrite || self.force,
rename_on_conflict: self.rename,
force: self.force,
})
}
}
impl InfoArgs {
pub fn to_parse_options(&self) -> Result<ParseOptions> {
Ok(ParseOptions {
request: self.request.to_settings()?,
checksum: parse_checksum(&self.checksum, &None)?,
revision: self.revision.clone(),
include: self.include.clone(),
exclude: self.exclude.clone(),
})
}
}
#[allow(dead_code)]
fn _assert_header(_: Header, _: Checksum) {}