Fix -V behavior (#488)

Fixed #485

* Make `Duration` in MainExit::Success` optional
* Fix arg parsing: Print version on `-V`

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-10-18 15:10:44 +11:00 committed by GitHub
parent 9ebcd1d426
commit f4f6e36984
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 13 deletions

View file

@ -11,7 +11,7 @@ use miette::Result;
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
pub enum MainExit { pub enum MainExit {
Success(Duration), Success(Option<Duration>),
Error(BinstallError), Error(BinstallError),
Report(miette::Report), Report(miette::Report),
} }
@ -20,7 +20,9 @@ impl Termination for MainExit {
fn report(self) -> ExitCode { fn report(self) -> ExitCode {
match self { match self {
Self::Success(spent) => { Self::Success(spent) => {
info!("Done in {spent:?}"); if let Some(spent) = spent {
info!("Done in {spent:?}");
}
ExitCode::SUCCESS ExitCode::SUCCESS
} }
Self::Error(err) => err.report(), Self::Error(err) => err.report(),
@ -35,11 +37,12 @@ impl Termination for MainExit {
impl MainExit { impl MainExit {
pub fn new(result: Result<Result<()>, BinstallError>, done: Duration) -> Self { pub fn new(result: Result<Result<()>, BinstallError>, done: Duration) -> Self {
result.map_or_else(MainExit::Error, |res| { result.map_or_else(MainExit::Error, |res| {
res.map(|()| MainExit::Success(done)).unwrap_or_else(|err| { res.map(|()| MainExit::Success(Some(done)))
err.downcast::<BinstallError>() .unwrap_or_else(|err| {
.map(MainExit::Error) err.downcast::<BinstallError>()
.unwrap_or_else(MainExit::Report) .map(MainExit::Error)
}) .unwrap_or_else(MainExit::Report)
})
}) })
} }
} }

View file

@ -22,14 +22,19 @@ fn main() -> MainExit {
Err(err) => return MainExit::Error(err), Err(err) => return MainExit::Error(err),
}; };
ui::logging(&args); if args.version {
println!("{}", env!("CARGO_PKG_VERSION"));
MainExit::Success(None)
} else {
ui::logging(&args);
let start = Instant::now(); let start = Instant::now();
let result = run_tokio_main(entry::install_crates(args, jobserver_client)); let result = run_tokio_main(entry::install_crates(args, jobserver_client));
let done = start.elapsed(); let done = start.elapsed();
debug!("run time: {done:?}"); debug!("run time: {done:?}");
MainExit::new(result, done) MainExit::new(result, done)
}
} }