mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-05-04 19:20:03 +00:00
Split crates and clean up structure of codebase (#294)
Co-authored-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
bf700f9012
commit
4b00f5f143
88 changed files with 2989 additions and 1423 deletions
78
crates/bin/src/main.rs
Normal file
78
crates/bin/src/main.rs
Normal file
|
@ -0,0 +1,78 @@
|
|||
use std::{
|
||||
process::{ExitCode, Termination},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
use binstall::{
|
||||
errors::BinstallError,
|
||||
helpers::{
|
||||
jobserver_client::LazyJobserverClient, signal::cancel_on_user_sig_term,
|
||||
tasks::AutoAbortJoinHandle,
|
||||
},
|
||||
};
|
||||
use log::{debug, error, info};
|
||||
use tokio::runtime::Runtime;
|
||||
|
||||
mod args;
|
||||
mod entry;
|
||||
mod install_path;
|
||||
mod ui;
|
||||
|
||||
#[cfg(feature = "mimalloc")]
|
||||
#[global_allocator]
|
||||
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
||||
|
||||
fn main() -> MainExit {
|
||||
// This must be the very first thing to happen
|
||||
let jobserver_client = LazyJobserverClient::new();
|
||||
|
||||
let args = match args::parse() {
|
||||
Ok(args) => args,
|
||||
Err(err) => return MainExit::Error(err),
|
||||
};
|
||||
|
||||
ui::logging(&args);
|
||||
|
||||
let start = Instant::now();
|
||||
|
||||
let result = {
|
||||
let rt = Runtime::new().unwrap();
|
||||
let handle =
|
||||
AutoAbortJoinHandle::new(rt.spawn(entry::install_crates(args, jobserver_client)));
|
||||
rt.block_on(cancel_on_user_sig_term(handle))
|
||||
};
|
||||
|
||||
let done = start.elapsed();
|
||||
debug!("run time: {done:?}");
|
||||
|
||||
result.map_or_else(MainExit::Error, |res| {
|
||||
res.map(|()| MainExit::Success(done)).unwrap_or_else(|err| {
|
||||
err.downcast::<BinstallError>()
|
||||
.map(MainExit::Error)
|
||||
.unwrap_or_else(MainExit::Report)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
enum MainExit {
|
||||
Success(Duration),
|
||||
Error(BinstallError),
|
||||
Report(miette::Report),
|
||||
}
|
||||
|
||||
impl Termination for MainExit {
|
||||
fn report(self) -> ExitCode {
|
||||
match self {
|
||||
Self::Success(spent) => {
|
||||
info!("Done in {spent:?}");
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
Self::Error(err) => err.report(),
|
||||
Self::Report(err) => {
|
||||
error!("Fatal error:");
|
||||
eprintln!("{err:?}");
|
||||
ExitCode::from(16)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue