From b95e020fd4367f5f91e728930c2eb0e8e4772598 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 1 Jul 2021 10:23:47 +0200 Subject: Late error reporting in release command Signed-off-by: Matthias Beyer --- src/commands/release.rs | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/commands/release.rs b/src/commands/release.rs index 50d3918..d4cd147 100644 --- a/src/commands/release.rs +++ b/src/commands/release.rs @@ -140,7 +140,7 @@ async fn new_release( let interactive = !matches.is_present("noninteractive"); let now = chrono::offset::Local::now().naive_local(); - arts.into_iter() + let (_oks, errors): (Vec<_>, Vec<_>) = arts.into_iter() .map(|art| async move { let art_path = staging_base.join(&art.path); let dest_path = config.releases_directory().join(release_store_name).join(&art.path); @@ -174,28 +174,42 @@ async fn new_release( } // else !dest_path.exists() - tokio::fs::copy(&art_path, &dest_path) + let dest_path = tokio::fs::copy(&art_path, &dest_path) .await .with_context(|| anyhow!("Copying {} to {}", art_path.display(), dest_path.display())) .map_err(Error::from) - .map(|_| (art, dest_path)) + .map(|_| dest_path); + + debug!("Updating {:?} to set released = true", art); + let rel = crate::db::models::Release::create(&conn, &art, &now, &release_store)?; + debug!("Release object = {:?}", rel); + Ok(dest_path) } }) .collect::>() - .collect::>>() - .await? + .collect::>>() + .await .into_iter() - .try_for_each(|(art, dest_path)| { - debug!("Updating {:?} to set released = true", art); - let rel = crate::db::models::Release::create(&conn, &art, &now, &release_store)?; - debug!("Release object = {:?}", rel); - + .and_then_ok(|dest_path| { if print_released_file_pathes { writeln!(std::io::stdout(), "{}", dest_path.display()).map_err(Error::from) } else { Ok(()) } }) + .partition(Result::is_ok); + + let mut any_err = false; + for error in errors { + any_err = true; + error!("Error: {}", error); + } + + if any_err { + Err(anyhow!("Releasing one or more artifacts failed")) + } else { + Ok(()) + } } pub async fn rm_release( -- cgit v1.2.3 From f7e57803eedd97b8fcc9f0dea29a8432d4486f9d Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 12 Oct 2021 09:15:36 +0200 Subject: Rewrite to be more idomatic Signed-off-by: Matthias Beyer --- src/commands/release.rs | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/commands/release.rs b/src/commands/release.rs index d4cd147..5231eb1 100644 --- a/src/commands/release.rs +++ b/src/commands/release.rs @@ -19,8 +19,9 @@ use anyhow::Error; use anyhow::Result; use clap::ArgMatches; use diesel::prelude::*; -use log::{debug, info, trace}; +use log::{debug, error, info, trace}; use tokio_stream::StreamExt; +use resiter::AndThen; use crate::config::Configuration; use crate::db::models as dbmodels; @@ -140,8 +141,9 @@ async fn new_release( let interactive = !matches.is_present("noninteractive"); let now = chrono::offset::Local::now().naive_local(); - let (_oks, errors): (Vec<_>, Vec<_>) = arts.into_iter() - .map(|art| async move { + let any_err = arts.into_iter() + .map(|art| async { + let art = art; // ensure it is moved let art_path = staging_base.join(&art.path); let dest_path = config.releases_directory().join(release_store_name).join(&art.path); debug!( @@ -174,16 +176,16 @@ async fn new_release( } // else !dest_path.exists() - let dest_path = tokio::fs::copy(&art_path, &dest_path) + tokio::fs::copy(&art_path, &dest_path) .await .with_context(|| anyhow!("Copying {} to {}", art_path.display(), dest_path.display())) .map_err(Error::from) - .map(|_| dest_path); - - debug!("Updating {:?} to set released = true", art); - let rel = crate::db::models::Release::create(&conn, &art, &now, &release_store)?; - debug!("Release object = {:?}", rel); - Ok(dest_path) + .and_then(|_| { + debug!("Updating {:?} to set released = true", art); + let rel = crate::db::models::Release::create(&conn, &art, &now, &release_store)?; + debug!("Release object = {:?}", rel); + Ok(dest_path) + }) } }) .collect::>() @@ -197,13 +199,10 @@ async fn new_release( Ok(()) } }) - .partition(Result::is_ok); - - let mut any_err = false; - for error in errors { - any_err = true; - error!("Error: {}", error); - } + .filter_map(Result::err) + .inspect(|err| error!("Error: {}", err.to_string())) + .last() + .is_some(); // consume iterator completely, if not empty, there was an error if any_err { Err(anyhow!("Releasing one or more artifacts failed")) -- cgit v1.2.3