diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2020-12-09 12:59:21 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2020-12-09 14:52:09 +0100 |
commit | 92364867ff01c66ccb4bd7de6634ed721af5ab09 (patch) | |
tree | 13aa5dc05bfdc8af4657e64eeeeb2890cd5066d0 /src/commands | |
parent | 757638df78813c5f8c5872b2818221a7ed689f4b (diff) |
Fix command implementation for source verification to log errors properly
This patch fixes the verify_impl() to log the errors properly. Before
this patch, the first error was reported (which we did not mean to
implement this way, so it was a bug).
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/build.rs | 3 | ||||
-rw-r--r-- | src/commands/source.rs | 50 |
2 files changed, 33 insertions, 20 deletions
diff --git a/src/commands/build.rs b/src/commands/build.rs index 182653c..c0c2f59 100644 --- a/src/commands/build.rs +++ b/src/commands/build.rs @@ -179,8 +179,7 @@ pub async fn build(matches: &ArgMatches, if matches.is_present("no_verification") { warn!("No hash verification will be performed"); } else { - let mut out = std::io::stdout(); - crate::commands::source::verify_impl(tree.all_packages().into_iter(), &source_cache, &mut out) + crate::commands::source::verify_impl(tree.all_packages().into_iter(), &source_cache) .await?; } diff --git a/src/commands/source.rs b/src/commands/source.rs index 4fd1d91..c14d123 100644 --- a/src/commands/source.rs +++ b/src/commands/source.rs @@ -1,10 +1,10 @@ use std::io::Write; use std::path::PathBuf; +use anyhow::anyhow; use anyhow::Error; use anyhow::Context; use anyhow::Result; -use anyhow::anyhow; use clap::ArgMatches; use log::trace; use tokio::stream::StreamExt; @@ -38,35 +38,49 @@ pub async fn verify(matches: &ArgMatches, config: &Configuration, repo: Reposito .filter(|p| pname.as_ref().map(|n| p.name() == n).unwrap_or(true)) .filter(|p| pvers.as_ref().map(|v| v.matches(p.version())).unwrap_or(true)); - let mut out = std::io::stdout(); - verify_impl(packages, &sc, &mut out).await + verify_impl(packages, &sc).await } -pub (in crate::commands) async fn verify_impl<'a, I>(packages: I, sc: &SourceCache, output: &mut dyn Write) -> Result<()> +pub (in crate::commands) async fn verify_impl<'a, I>(packages: I, sc: &SourceCache) -> Result<()> where I: Iterator<Item = &'a Package> + 'a { - if let Err(_) = packages + let results = packages .map(|p| sc.sources_for(p).into_iter()) .flatten() .map(|source| async move { if source.path().exists() { - match source.verify_hash().await { - Ok(true) => Ok(format!("Ok: {}", source.path().display())), - Ok(false) => Err(format!("Hash Mismatch: {}", source.path().display())), - Err(e) => Err(format!("Hash verification failed: {}", e.to_string())), // TODO: make me nice - } + source.verify_hash() + .await + .with_context(|| anyhow!("Hash verification failed for: {}", source.path().display()))?; + + Ok(format!("Ok: {}", source.path().display())) } else { - Err(format!("Source missing: {}", source.path().display())) + Err(anyhow!("Source missing: {}", source.path().display())) } }) .collect::<futures::stream::FuturesUnordered<_>>() - .collect::<Vec<std::result::Result<String, String>>>() - .await - .into_iter() - .inspect(|r| { let _ = writeln!(output, "{}", match r { Ok(s) | Err(s) => s }); }) - .map(|r| r.map(|_| ()).map_err(|_| ())) - .collect::<std::result::Result<(), ()>>() - { + .collect::<Vec<Result<String>>>() + .await; + + let out = std::io::stdout(); + let mut any_error = false; + for result in results { + match result { + Err(e) => { + let mut outlock = out.lock(); + any_error = true; + for cause in e.chain() { + let _ = writeln!(outlock, "{}", cause); + } + let _ = writeln!(outlock, ""); + }, + Ok(s) => { + let _ = writeln!(out.lock(), "{}", s); + }, + } + } + + if any_error { Err(anyhow!("At least one package failed with source verification")) } else { Ok(()) |