From dbf7ef499465ad764630ab5df871b3ccb56c6f48 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Mon, 29 Jul 2024 10:22:37 -0700 Subject: [PATCH] fix: Improve diagnose. --- CHANGELOG.md | 10 ++++++ crates/cli/src/commands/diagnose.rs | 55 ++++++++++++++++++++--------- crates/cli/src/session.rs | 3 +- 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b454be53..d38447ac2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,16 @@ - [Rust](https://github.com/moonrepo/tools/blob/master/tools/rust/CHANGELOG.md) - [TOML schema](https://github.com/moonrepo/tools/blob/master/tools/internal-schema/CHANGELOG.md) +## Unreleased + +#### 🚀 Updates + +- Updated `proto diagnose` to check the current proto version. + +#### 🐞 Fixes + +- Disabled the version check requests for `proto activate`. + ## 0.39.1 #### 🚀 Updates diff --git a/crates/cli/src/commands/diagnose.rs b/crates/cli/src/commands/diagnose.rs index 2f2ddea30..30327b656 100644 --- a/crates/cli/src/commands/diagnose.rs +++ b/crates/cli/src/commands/diagnose.rs @@ -1,6 +1,8 @@ +use crate::helpers::fetch_latest_version; use crate::printer::Printer; use crate::session::ProtoSession; use clap::Args; +use semver::Version; use serde::Serialize; use starbase::AppResult; use starbase_shell::ShellType; @@ -36,16 +38,10 @@ struct Diagnosis { #[tracing::instrument(skip_all)] pub async fn diagnose(session: ProtoSession, args: DiagnoseArgs) -> AppResult { - let shell = match args.shell { + let shell_type = match args.shell { Some(value) => value, None => ShellType::try_detect()?, }; - let shell_data = shell.build(); - let shell_path = session - .env - .store - .load_preferred_profile()? - .unwrap_or_else(|| shell_data.get_env_path(&session.env.home)); let paths = starbase_utils::env::paths(); @@ -55,15 +51,22 @@ pub async fn diagnose(session: ProtoSession, args: DiagnoseArgs) -> AppResult { } let mut tips = vec![]; - let errors = gather_errors(&session, &paths, &mut tips); - let warnings = gather_warnings(&session, &paths, &mut tips); + let errors = gather_errors(&session, &paths, &mut tips).await?; + let warnings = gather_warnings(&session, &paths, &mut tips).await?; if args.json { + let shell = shell_type.build(); + let shell_path = session + .env + .store + .load_preferred_profile()? + .unwrap_or_else(|| shell.get_env_path(&session.env.home)); + println!( "{}", json::format( &Diagnosis { - shell: shell.to_string(), + shell: shell_type.to_string(), shell_profile: shell_path, errors, warnings, @@ -85,10 +88,11 @@ pub async fn diagnose(session: ProtoSession, args: DiagnoseArgs) -> AppResult { return Ok(()); } + let shell = shell_type.build(); let mut printer = Printer::new(); printer.line(); - printer.entry("Shell", color::id(shell.to_string())); + printer.entry("Shell", color::id(shell_type.to_string())); printer.entry( "Shell profile", color::path( @@ -96,7 +100,7 @@ pub async fn diagnose(session: ProtoSession, args: DiagnoseArgs) -> AppResult { .env .store .load_preferred_profile()? - .unwrap_or_else(|| shell_data.get_env_path(&session.env.home)), + .unwrap_or_else(|| shell.get_env_path(&session.env.home)), ), ); @@ -133,7 +137,11 @@ pub async fn diagnose(session: ProtoSession, args: DiagnoseArgs) -> AppResult { Ok(()) } -fn gather_errors(session: &ProtoSession, paths: &[PathBuf], _tips: &mut [String]) -> Vec { +async fn gather_errors( + session: &ProtoSession, + paths: &[PathBuf], + _tips: &mut [String], +) -> miette::Result> { let mut errors = vec![]; let mut has_shims_before_bins = false; let mut found_shims = false; @@ -168,16 +176,29 @@ fn gather_errors(session: &ProtoSession, paths: &[PathBuf], _tips: &mut [String] }) } - errors + Ok(errors) } -fn gather_warnings( +async fn gather_warnings( session: &ProtoSession, paths: &[PathBuf], tips: &mut Vec, -) -> Vec { +) -> miette::Result> { let mut warnings = vec![]; + let current_version = &session.cli_version; + let latest_version = fetch_latest_version().await?; + + if Version::parse(current_version).unwrap() < Version::parse(&latest_version).unwrap() { + warnings.push(Issue { + issue: format!( + "Current proto version {current_version} is outdated, latest is {latest_version}", + ), + resolution: Some(format!("Run {} to update", color::shell("proto upgrade"))), + comment: None, + }); + } + if env::var("PROTO_HOME").is_err() { warnings.push(Issue { issue: format!( @@ -238,7 +259,7 @@ fn gather_warnings( )); } - warnings + Ok(warnings) } fn print_issues(issues: &[Issue], printer: &mut Printer) { diff --git a/crates/cli/src/session.rs b/crates/cli/src/session.rs index 0de3f2d90..81ed9e3b1 100644 --- a/crates/cli/src/session.rs +++ b/crates/cli/src/session.rs @@ -31,7 +31,8 @@ impl ProtoSession { pub fn should_check_for_new_version(&self) -> bool { !matches!( self.cli.command, - Commands::Bin(_) + Commands::Activate(_) + | Commands::Bin(_) | Commands::Completions(_) | Commands::Run(_) | Commands::Setup(_)