From 891c4ef3c97e543731a25c8cbe346fa26ccf6d72 Mon Sep 17 00:00:00 2001 From: Sebastian Ullrich Date: Mon, 17 Jun 2024 10:29:58 +0200 Subject: [PATCH] chore: remove obsolete update notification logic --- src/elan-cli/common.rs | 71 ++++++++------------------------------- src/elan-cli/elan_mode.rs | 24 ++----------- src/elan/install.rs | 8 ++--- src/elan/toolchain.rs | 36 +++++--------------- 4 files changed, 30 insertions(+), 109 deletions(-) diff --git a/src/elan-cli/common.rs b/src/elan-cli/common.rs index a7c181b..e47a443 100644 --- a/src/elan-cli/common.rs +++ b/src/elan-cli/common.rs @@ -1,6 +1,6 @@ //! Just a dumping ground for cli stuff -use elan::{self, Cfg, Notification, Toolchain, UpdateStatus}; +use elan::{Cfg, Notification, Toolchain}; use elan_dist::dist::ToolchainDesc; use elan_utils::notify::NotificationLevel; use elan_utils::utils; @@ -11,7 +11,6 @@ use std::path::Path; use std::process::{Command, Stdio}; use std::sync::Arc; use std::time::Duration; -use std::{cmp, iter}; use term2; use wait_timeout::ChildExt; @@ -133,67 +132,25 @@ pub fn set_globals(verbose: bool) -> Result { pub fn show_channel_update( cfg: &Cfg, - name: &ToolchainDesc, - updated: elan::Result, + desc: &ToolchainDesc, ) -> Result<()> { - show_channel_updates(cfg, vec![(name.clone(), updated)]) -} + let ref toolchain = cfg.get_toolchain(&desc, false).expect(""); + let version = lean_version(toolchain); + let name = desc.to_string(); -fn show_channel_updates( - cfg: &Cfg, - toolchains: Vec<(ToolchainDesc, elan::Result)>, -) -> Result<()> { - let data = toolchains.into_iter().map(|(desc, result)| { - let ref toolchain = cfg.get_toolchain(&desc, false).expect(""); - let version = lean_version(toolchain); - let name = desc.to_string(); - - let banner; - let color; - match result { - Ok(UpdateStatus::Installed) => { - banner = "installed"; - color = Some(term2::color::BRIGHT_GREEN); - } - Ok(UpdateStatus::Updated) => { - banner = "updated"; - color = Some(term2::color::BRIGHT_GREEN); - } - Ok(UpdateStatus::Unchanged) => { - banner = "unchanged"; - color = None; - } - Err(_) => { - banner = "update failed"; - color = Some(term2::color::BRIGHT_RED); - } - } - - let width = name.len() + 1 + banner.len(); - - (name, banner, width, color, version) - }); + let banner = "installed"; + let color = Some(term2::color::BRIGHT_GREEN); let mut t = term2::stdout(); - let data: Vec<_> = data.collect(); - let max_width = data - .iter() - .fold(0, |a, &(_, _, width, _, _)| cmp::max(a, width)); - - for (name, banner, width, color, version) in data { - let padding = max_width - width; - let padding: String = iter::repeat(' ').take(padding).collect(); - let _ = write!(t, " {}", padding); - let _ = t.attr(term2::Attr::Bold); - if let Some(color) = color { - let _ = t.fg(color); - } - let _ = write!(t, "{} ", name); - let _ = write!(t, "{}", banner); - let _ = t.reset(); - let _ = writeln!(t, " - {}", version); + let _ = t.attr(term2::Attr::Bold); + if let Some(color) = color { + let _ = t.fg(color); } + let _ = write!(t, "{} ", name); + let _ = write!(t, "{}", banner); + let _ = t.reset(); + let _ = writeln!(t, " - {}", version); let _ = writeln!(t, ""); Ok(()) diff --git a/src/elan-cli/elan_mode.rs b/src/elan-cli/elan_mode.rs index 485203f..d53ee56 100644 --- a/src/elan-cli/elan_mode.rs +++ b/src/elan-cli/elan_mode.rs @@ -245,15 +245,10 @@ fn install(cfg: &Cfg, m: &ArgMatches) -> Result<()> { let desc = lookup_toolchain_desc(cfg, name)?; let toolchain = cfg.get_toolchain(&desc, false)?; - let status = if !toolchain.exists() || !toolchain.is_custom() { - Some(toolchain.install_from_dist()?) - } else { - None - }; - - if let Some(status) = status { + if !toolchain.exists() || !toolchain.is_custom() { + toolchain.install_from_dist()?; println!(""); - common::show_channel_update(cfg, &toolchain.desc, Ok(status))?; + common::show_channel_update(cfg, &toolchain.desc)?; } } @@ -427,20 +422,7 @@ fn override_add(cfg: &Cfg, m: &ArgMatches) -> Result<()> { let ref toolchain = m.value_of("toolchain").expect(""); let desc = lookup_toolchain_desc(cfg, toolchain)?; let toolchain = cfg.get_toolchain(&desc, false)?; - - let status = if !toolchain.exists() || !toolchain.is_custom() { - Some(toolchain.install_from_dist_if_not_installed()?) - } else { - None - }; - toolchain.make_override(&utils::current_dir()?)?; - - if let Some(status) = status { - println!(""); - common::show_channel_update(cfg, &toolchain.desc, Ok(status))?; - } - Ok(()) } diff --git a/src/elan/install.rs b/src/elan/install.rs index 5119f3e..a48e117 100644 --- a/src/elan/install.rs +++ b/src/elan/install.rs @@ -44,7 +44,7 @@ pub enum InstallMethod<'a> { } impl<'a> InstallMethod<'a> { - pub fn run(self, path: &Path, notify_handler: &dyn Fn(Notification)) -> Result { + pub fn run(self, path: &Path, notify_handler: &dyn Fn(Notification)) -> Result<()> { if path.exists() { // Don't uninstall first for Dist method match self { @@ -58,11 +58,11 @@ impl<'a> InstallMethod<'a> { match self { InstallMethod::Copy(src) => { utils::copy_dir(src, path, &|n| notify_handler(n.into()))?; - Ok(true) + Ok(()) } InstallMethod::Link(src) => { utils::symlink_dir(src, &path, &|n| notify_handler(n.into()))?; - Ok(true) + Ok(()) } InstallMethod::Dist(toolchain, dl_cfg) => { if let Some(version) = check_self_update()? { @@ -76,7 +76,7 @@ impl<'a> InstallMethod<'a> { prefix, )?; - Ok(true) + Ok(()) } } } diff --git a/src/elan/toolchain.rs b/src/elan/toolchain.rs index 1314bd8..ec3be76 100644 --- a/src/elan/toolchain.rs +++ b/src/elan/toolchain.rs @@ -36,12 +36,6 @@ pub struct ComponentStatus { pub available: bool, } -pub enum UpdateStatus { - Installed, - Updated, - Unchanged, -} - pub fn lookup_toolchain_desc(cfg: &Cfg, name: &str) -> Result { let pattern = r"^(?:([a-zA-Z0-9-]+[/][a-zA-Z0-9-]+)[:])?([a-zA-Z0-9-.]+)$"; @@ -126,7 +120,7 @@ impl<'a> Toolchain<'a> { } Ok(result?) } - fn install(&self, install_method: InstallMethod) -> Result { + fn install(&self, install_method: InstallMethod) -> Result<()> { let exists = self.exists(); if exists { return Err(format!("'{}' is already installed", self.desc).into()) @@ -134,30 +128,18 @@ impl<'a> Toolchain<'a> { (self.cfg.notify_handler)(Notification::InstallingToolchain(&self.desc)); } (self.cfg.notify_handler)(Notification::ToolchainDirectory(&self.path, &self.desc)); - let updated = install_method.run(&self.path, &|n| (self.cfg.notify_handler)(n.into()))?; - - if !updated { - (self.cfg.notify_handler)(Notification::UpdateHashMatches); - } else { - (self.cfg.notify_handler)(Notification::InstalledToolchain(&self.desc)); - } + install_method.run(&self.path, &|n| (self.cfg.notify_handler)(n.into()))?; - let status = match (updated, exists) { - (true, false) => UpdateStatus::Installed, - (true, true) => UpdateStatus::Updated, - (false, true) => UpdateStatus::Unchanged, - (false, false) => UpdateStatus::Unchanged, - }; + (self.cfg.notify_handler)(Notification::InstalledToolchain(&self.desc)); - Ok(status) + Ok(()) } - fn install_if_not_installed(&self, install_method: InstallMethod) -> Result { + fn install_if_not_installed(&self, install_method: InstallMethod) -> Result<()> { (self.cfg.notify_handler)(Notification::LookingForToolchain(&self.desc)); if !self.exists() { - Ok(self.install(install_method)?) + self.install(install_method) } else { - (self.cfg.notify_handler)(Notification::UsingExistingToolchain(&self.desc)); - Ok(UpdateStatus::Unchanged) + Ok(()) } } @@ -168,14 +150,14 @@ impl<'a> Toolchain<'a> { } } - pub fn install_from_dist(&self) -> Result { + pub fn install_from_dist(&self) -> Result<()> { self.install(InstallMethod::Dist( &self.desc, self.download_cfg(), )) } - pub fn install_from_dist_if_not_installed(&self) -> Result { + pub fn install_from_dist_if_not_installed(&self) -> Result<()> { self.install_if_not_installed(InstallMethod::Dist( &self.desc, self.download_cfg(),