mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-05-03 02:30:02 +00:00

* feat: Impl new option `--continue-on-failure` Resolve #1548 Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Add new e2e-tests continue-on-failure Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Rm dup line ion `e2e-tests/live.sh` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Fix shellcheck Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Fix `BinstallError::crate_errors` if `errors.len()` is 1 In that case, it should return `Some(Self::CrateContext(_))` instead of `Some(Self::Errors(_))` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Add more tests to `e2e-tests/continue-on-failure.sh` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Propagate crate errors on `confirm()` err Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Test having two errors in `e2e-tests/continue-on-failure.sh` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> --------- Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
65 lines
1.7 KiB
Rust
65 lines
1.7 KiB
Rust
use std::{
|
|
process::{ExitCode, Termination},
|
|
time::Duration,
|
|
};
|
|
|
|
use binstalk::errors::BinstallError;
|
|
use binstalk::helpers::tasks::AutoAbortJoinHandle;
|
|
use miette::Result;
|
|
use tokio::runtime::Runtime;
|
|
use tracing::{error, info};
|
|
|
|
use crate::signal::cancel_on_user_sig_term;
|
|
|
|
pub enum MainExit {
|
|
Success(Option<Duration>),
|
|
Error(BinstallError),
|
|
Report(miette::Report),
|
|
}
|
|
|
|
impl Termination for MainExit {
|
|
fn report(self) -> ExitCode {
|
|
match self {
|
|
Self::Success(spent) => {
|
|
if let Some(spent) = spent {
|
|
info!("Done in {spent:?}");
|
|
}
|
|
ExitCode::SUCCESS
|
|
}
|
|
Self::Error(err) => err.report(),
|
|
Self::Report(err) => {
|
|
error!("Fatal error:\n{err:?}");
|
|
ExitCode::from(16)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl MainExit {
|
|
pub fn new(res: Result<()>, done: Duration) -> Self {
|
|
res.map(|()| MainExit::Success(Some(done)))
|
|
.unwrap_or_else(|err| {
|
|
err.downcast::<BinstallError>()
|
|
.map(MainExit::Error)
|
|
.unwrap_or_else(MainExit::Report)
|
|
})
|
|
}
|
|
}
|
|
|
|
/// This function would start a tokio multithreading runtime,
|
|
/// then `block_on` the task it returns.
|
|
///
|
|
/// It will cancel the future if user requested cancellation
|
|
/// via signal.
|
|
pub fn run_tokio_main(
|
|
f: impl FnOnce() -> Result<Option<AutoAbortJoinHandle<Result<()>>>>,
|
|
) -> Result<()> {
|
|
let rt = Runtime::new().map_err(BinstallError::from)?;
|
|
let _guard = rt.enter();
|
|
|
|
if let Some(handle) = f()? {
|
|
rt.block_on(cancel_on_user_sig_term(handle))?
|
|
} else {
|
|
Ok(())
|
|
}
|
|
}
|