Split crates and clean up structure of codebase (#294)

Co-authored-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Félix Saparelli 2022-08-20 23:24:12 +12:00 committed by GitHub
parent bf700f9012
commit 4b00f5f143
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
88 changed files with 2989 additions and 1423 deletions

78
crates/bin/src/main.rs Normal file
View 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)
}
}
}
}