diff --git a/src/bin/dlcrate.rs b/src/bin/dlcrate.rs new file mode 100644 index 0000000..6e47f3d --- /dev/null +++ b/src/bin/dlcrate.rs @@ -0,0 +1,36 @@ +use clap::Parser; +use color_eyre::eyre::eyre; +use color_eyre::eyre::Result; +use color_eyre::eyre::WrapErr; +use std::process::Command; + +use utils_rs::common::parsers; + +#[derive(Parser, Debug)] +struct Args { + /// name and version of the crate in the format : + #[arg(value_parser=parsers::parse_name_version)] + name_version: (String, String), +} + +fn download(name: &str, version: &str) -> Result<()> { + let url = format!("https://static.crates.io/crates/{name}/{name}-{version}.crate"); + let _ = Command::new("wget") + .arg(&url) + .arg("--output-document") + .arg(format!("{}-{}.tar.gz", name, version)) + .status() + .wrap_err_with(|| eyre!("Failed to download crate from {}", url))?; + Ok(()) +} + +fn main() -> Result<()> { + // setup color_eyre panic and error report handlers + color_eyre::install()?; + + // parse args + let args = Args::parse(); + + let (crate_name, version) = args.name_version; + download(&crate_name, &version) +} diff --git a/src/common/parsers.rs b/src/common/parsers.rs index 61c1cb4..f551977 100644 --- a/src/common/parsers.rs +++ b/src/common/parsers.rs @@ -1,16 +1,28 @@ -use std::error::Error; +// use std::error::Error; +use color_eyre::eyre::{eyre, Result}; use std::path::PathBuf; -// TODO: add tests for parse_dir // TODO: setup CI on gitlab to run tests on every commit +// TODO: add tests /// custom parser to validate that an arg is a directory #[allow(dead_code)] -pub fn parse_dir(s: &str) -> Result> { +pub fn parse_dir(s: &str) -> Result { let path = PathBuf::from(s); if path.is_dir() { Ok(path) } else { - Err(format!("{} is not a directory", s).into()) + Err(eyre!(format!("{} is not a directory", s))) } } + +// TODO: add tests +/// custom parser to parse a crate-version input of the form +/// `:` +#[allow(dead_code)] +pub fn parse_name_version(s: &str) -> Result<(String, String)> { + let parts = s + .split_once(':') + .ok_or_else(|| eyre!("invalid input (should be in the format :)"))?; + Ok((String::from(parts.0), String::from(parts.1))) +}