Skip to content

Commit

Permalink
fix: Improve diagnose.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Jul 29, 2024
1 parent eda456c commit dbf7ef4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 38 additions & 17 deletions crates/cli/src/commands/diagnose.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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,
Expand All @@ -85,18 +88,19 @@ 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(
session
.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)),
),
);

Expand Down Expand Up @@ -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<Issue> {
async fn gather_errors(
session: &ProtoSession,
paths: &[PathBuf],
_tips: &mut [String],
) -> miette::Result<Vec<Issue>> {
let mut errors = vec![];
let mut has_shims_before_bins = false;
let mut found_shims = false;
Expand Down Expand Up @@ -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<String>,
) -> Vec<Issue> {
) -> miette::Result<Vec<Issue>> {
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!(
Expand Down Expand Up @@ -238,7 +259,7 @@ fn gather_warnings(
));
}

warnings
Ok(warnings)
}

fn print_issues(issues: &[Issue], printer: &mut Printer) {
Expand Down
3 changes: 2 additions & 1 deletion crates/cli/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_)
Expand Down

0 comments on commit dbf7ef4

Please sign in to comment.