From 4aab9807357d6e522f2a979af82ea8fafbce027f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 11 Nov 2020 12:25:04 +0100 Subject: Move to have "source" subcommand Signed-off-by: Matthias Beyer --- src/cli.rs | 19 +++++++++------- src/commands/mod.rs | 4 ++-- src/commands/source.rs | 49 ++++++++++++++++++++++++++++++++++++++++++ src/commands/verify_sources.rs | 42 ------------------------------------ src/main.rs | 4 ++-- 5 files changed, 64 insertions(+), 54 deletions(-) create mode 100644 src/commands/source.rs delete mode 100644 src/commands/verify_sources.rs (limited to 'src') diff --git a/src/cli.rs b/src/cli.rs index 2299165..445f50c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -278,14 +278,17 @@ pub fn cli<'a>() -> App<'a> { .help("Do not use the fancy format, but simply ") ) ) - .subcommand(App::new("verify-sources") - .about("Hash-check all source files") - .arg(Arg::with_name("package_name") - .required(false) - .multiple(false) - .index(1) - .value_name("PKG") - .help("Verify the sources of this package (optional, if left out, all packages are checked)") + .subcommand(App::new("source") + .about("Handle package sources") + .subcommand(App::new("verify") + .about("Hash-check all source files") + .arg(Arg::with_name("package_name") + .required(false) + .multiple(false) + .index(1) + .value_name("PKG") + .help("Verify the sources of this package (optional, if left out, all packages are checked)") + ) ) ) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 6c8c0db..6dbd7ae 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -16,8 +16,8 @@ pub use dependencies_of::dependencies_of; mod what_depends; pub use what_depends::what_depends; -mod verify_sources; -pub use verify_sources::verify_sources; +mod source; +pub use source::source; mod versions_of; pub use versions_of::versions_of; diff --git a/src/commands/source.rs b/src/commands/source.rs new file mode 100644 index 0000000..9cb1f48 --- /dev/null +++ b/src/commands/source.rs @@ -0,0 +1,49 @@ +use std::io::Write; +use std::path::PathBuf; + +use anyhow::Result; +use anyhow::anyhow; +use clap_v3::ArgMatches; + +use crate::config::*; +use crate::package::PackageName; +use crate::repository::Repository; +use crate::source::*; + +pub async fn source<'a>(matches: &ArgMatches, config: &Configuration<'a>, repo: Repository) -> Result<()> { + match matches.subcommand() { + ("verify", Some(matches)) => verify(matches, config, repo).await, + (other, _) => return Err(anyhow!("Unknown subcommand: {}", other)), + } +} + +pub async fn verify<'a>(matches: &ArgMatches, config: &Configuration<'a>, repo: Repository) -> Result<()> { + use tokio::stream::StreamExt; + + let source_cache_root = PathBuf::from(config.source_cache_root()); + let sc = SourceCache::new(source_cache_root); + let pname = matches.value_of("package_name").map(String::from).map(PackageName::from); + + repo.packages() + .filter(|p| pname.as_ref().map(|n| p.name() == n).unwrap_or(true)) + .map(|p| { + let source = sc.source_for(p); + async move { + let out = std::io::stdout(); + if source.exists() { + if source.verify_hash().await? { + writeln!(out.lock(), "Ok: {}", source.path().display())?; + } else { + writeln!(out.lock(), "Hash Mismatch: {}", source.path().display())?; + } + } else { + writeln!(out.lock(), "Source missing: {}", source.path().display())?; + } + + Ok(()) + } + }) + .collect::>() + .collect::>() + .await +} diff --git a/src/commands/verify_sources.rs b/src/commands/verify_sources.rs deleted file mode 100644 index 3f7b9c8..0000000 --- a/src/commands/verify_sources.rs +++ /dev/null @@ -1,42 +0,0 @@ -use std::io::Write; -use std::path::PathBuf; - -use anyhow::Result; -use clap_v3::ArgMatches; - -use crate::config::*; -use crate::package::PackageName; -use crate::repository::Repository; -use crate::source::*; - -pub async fn verify_sources<'a>(matches: &ArgMatches, config: &Configuration<'a>, repo: Repository) -> Result<()> { - use tokio::stream::StreamExt; - - let source_cache_root = PathBuf::from(config.source_cache_root()); - let sc = SourceCache::new(source_cache_root); - let pname = matches.value_of("package_name").map(String::from).map(PackageName::from); - - repo.packages() - .filter(|p| pname.as_ref().map(|n| p.name() == n).unwrap_or(true)) - .map(|p| { - let source = sc.source_for(p); - async move { - let out = std::io::stdout(); - if source.exists() { - if source.verify_hash().await? { - writeln!(out.lock(), "Ok: {}", source.path().display())?; - } else { - writeln!(out.lock(), "Hash Mismatch: {}", source.path().display())?; - } - } else { - writeln!(out.lock(), "Source missing: {}", source.path().display())?; - } - - Ok(()) - } - }) - .collect::>() - .collect::>() - .await -} - diff --git a/src/main.rs b/src/main.rs index cef9b43..0940356 100644 --- a/src/main.rs +++ b/src/main.rs @@ -100,9 +100,9 @@ async fn main() -> Result<()> { crate::commands::find_pkg(matches, &config, repo).await? }, - ("verify-sources", Some(matches)) => { + ("source", Some(matches)) => { let repo = load_repo()?; - crate::commands::verify_sources(matches, &config, repo).await? + crate::commands::source(matches, &config, repo).await? } (other, _) => return Err(anyhow!("Unknown subcommand: {}", other)), -- cgit v1.2.3