From 92aaadfa3d5279e6e0e6a4a36a71b56eab7baa99 Mon Sep 17 00:00:00 2001 From: Ningyuan Li Date: Sun, 1 Oct 2023 00:22:35 +0900 Subject: [PATCH] Print installation clean up status --- ares-install/src/install.rs | 30 +++++++++++++++------- ares-install/src/main.rs | 2 +- common/connection/src/luna/subscription.rs | 17 +++++++++--- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/ares-install/src/install.rs b/ares-install/src/install.rs index 599c06e..964a437 100644 --- a/ares-install/src/install.rs +++ b/ares-install/src/install.rs @@ -14,7 +14,7 @@ use ares_connection_lib::session::DeviceSession; use ares_connection_lib::transfer::{FileTransfer, TransferError}; pub(crate) trait InstallApp { - fn install_app>(&self, package: P) -> Result; + fn install_app>(&self, package: P) -> Result<(), InstallError>; } #[derive(Debug)] @@ -48,7 +48,7 @@ struct InstallResponseDetails { } impl InstallApp for DeviceSession { - fn install_app>(&self, package: P) -> Result { + fn install_app>(&self, package: P) -> Result<(), InstallError> { let mut file = File::open(&package)?; let file_size = file.metadata()?.len(); let checksum = sha256::try_digest(package.as_ref()).map_err(|e| { @@ -80,7 +80,7 @@ impl InstallApp for DeviceSession { }); pb.enable_steady_tick(Duration::from_millis(50)); pb.set_prefix("Uploading"); - pb.set_style(ProgressStyle::with_template("{prefix:.bold.dim} {spinner} {percent}% [{wide_bar}] {bytes}/{total_bytes} {eta} ETA") + pb.set_style(ProgressStyle::with_template("{prefix:10.bold.dim} {spinner} {percent:>3}% [{wide_bar}] {bytes}/{total_bytes} {eta} ETA") .unwrap()); self.put(&mut file, &ipk_path, |transferred| { @@ -96,7 +96,7 @@ impl InstallApp for DeviceSession { pb.set_prefix("Installing"); let spinner_style = - ProgressStyle::with_template("{prefix:.bold.dim} {spinner} {wide_msg}").unwrap(); + ProgressStyle::with_template("{prefix:10.bold.dim} {spinner} {wide_msg}").unwrap(); pb.set_style(spinner_style); let result = match self.subscribe( @@ -123,16 +123,28 @@ impl InstallApp for DeviceSession { }, ) }) - .next(), - Err(e) => Some(Err(e.into())), + .next() + .unwrap_or_else(|| Ok(String::new())), + Err(e) => Err(e.into()), }; - pb.finish_and_clear(); + + if let Ok(package_id) = &result { + pb.suspend(|| println!("Installed package {}!", package_id)); + } + pb.suspend(|| println!("Deleting uploaded package...")); + + pb.set_prefix("Cleanup"); + pb.set_message("Deleting uploaded package"); if let Err(e) = self.rm(&ipk_path) { - eprintln!("Failed to delete {}: {:?}", ipk_path, e); + pb.suspend(|| { + eprintln!("Failed to delete {}: {:?}", ipk_path, e); + }); } + pb.finish_and_clear(); - return result.unwrap(); + result?; + return Ok(()); } } diff --git a/ares-install/src/main.rs b/ares-install/src/main.rs index 71cb84f..bca1a02 100644 --- a/ares-install/src/main.rs +++ b/ares-install/src/main.rs @@ -65,7 +65,7 @@ fn main() { } } else if let Some(package) = cli.package { match session.install_app(package) { - Ok(package_id) => println!("{package_id} installed."), + Ok(_) => {} Err(e) => { eprintln!("Failed to install: {e:?}"); exit(1); diff --git a/common/connection/src/luna/subscription.rs b/common/connection/src/luna/subscription.rs index 0bd3ec5..6fec0ce 100644 --- a/common/connection/src/luna/subscription.rs +++ b/common/connection/src/luna/subscription.rs @@ -47,8 +47,19 @@ impl Iterator for Subscription { impl Drop for Subscription { fn drop(&mut self) { - self.ch.send_eof().unwrap_or(()); - self.ch.request_send_signal("TERM").unwrap_or(()); - self.ch.close().unwrap_or(()); + self.close().unwrap_or_else(|e| { + eprintln!("Failed to close subscription: {e:?}"); + return 0; + }); + } +} + +impl Subscription { + fn close(&mut self) -> Result { + self.ch.send_eof()?; + self.ch.request_send_signal("TERM")?; + let status = self.ch.get_exit_status(); + self.ch.close()?; + return Ok(status.unwrap_or(-1) as i32); } }