From a2a0eca892a213599396553ed63f622f41b87f18 Mon Sep 17 00:00:00 2001 From: raychu86 Date: Wed, 2 Sep 2020 17:20:11 -0700 Subject: [PATCH] Use updater for leo update cli --- leo/commands/update.rs | 46 +++--------------------------------------- leo/errors/cli.rs | 7 +++++++ 2 files changed, 10 insertions(+), 43 deletions(-) diff --git a/leo/commands/update.rs b/leo/commands/update.rs index 69fd44737e..27381f30c8 100644 --- a/leo/commands/update.rs +++ b/leo/commands/update.rs @@ -14,51 +14,11 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{cli::CLI, cli_types::*}; - -use self_update::{backends::github, cargo_crate_version, Status}; - -const LEO_BIN_NAME: &str = "leo"; -const LEO_REPO_OWNER: &str = "AleoHQ"; -const LEO_REPO_NAME: &str = "leo"; +use crate::{cli::CLI, cli_types::*, updater::Updater}; #[derive(Debug)] pub struct UpdateCommand; -// TODO Add logic for users to easily select release versions. -impl UpdateCommand { - /// Show all available releases for `leo` - pub fn show_available_releases() -> Result<(), self_update::errors::Error> { - let releases = github::ReleaseList::configure() - .repo_owner(LEO_REPO_OWNER) - .repo_name(LEO_REPO_NAME) - .build()? - .fetch()?; - - tracing::info!("List of available Leo's versions"); - for release in releases { - tracing::info!("* {}", release.version); - } - Ok(()) - } - - /// Update `leo` to the latest release - pub fn update_to_latest_release(show_output: bool) -> Result { - let status = github::Update::configure() - .repo_owner(LEO_REPO_OWNER) - .repo_name(LEO_REPO_NAME) - .bin_name(LEO_BIN_NAME) - .current_version(cargo_crate_version!()) - .show_download_progress(true) - .no_confirm(true) - .show_output(show_output) - .build()? - .update()?; - - Ok(status) - } -} - impl CLI for UpdateCommand { type Options = (bool,); type Output = (); @@ -81,14 +41,14 @@ impl CLI for UpdateCommand { let _enter = span.enter(); match options { - (true,) => match UpdateCommand::show_available_releases() { + (true,) => match Updater::show_available_releases() { Ok(_) => return Ok(()), Err(e) => { tracing::error!("Could not fetch that latest version of Leo"); tracing::error!("{}", e); } }, - (false,) => match UpdateCommand::update_to_latest_release(true) { + (false,) => match Updater::update_to_latest_release(true) { Ok(status) => { if status.uptodate() { tracing::info!("Leo is already on the latest version: {}", status.version()); diff --git a/leo/errors/cli.rs b/leo/errors/cli.rs index ca160a609f..6a2c3b4f40 100644 --- a/leo/errors/cli.rs +++ b/leo/errors/cli.rs @@ -152,6 +152,13 @@ impl_cli_error!( ZipFileError ); +impl From for CLIError { + fn from(error: clap::Error) -> Self { + tracing::error!("{}\n", error); + CLIError::Crate("clap", format!("{}", error)) + } +} + impl From for CLIError { fn from(error: leo_compiler::errors::CompilerError) -> Self { tracing::error!("{}\n", error);