Skip to content

Commit

Permalink
apply smart guess to rustup update/uninstall self
Browse files Browse the repository at this point in the history
Use original error instead of rewriting

avoid duplicate `.to_string()`

remove unnecessary `.as_str()`
  • Loading branch information
Xerxes-2 committed Jun 26, 2024
1 parent 20a1c81 commit 4689949
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::ExitStatus;
use std::str::FromStr;

use anyhow::{anyhow, Error, Result};
use clap::{builder::PossibleValue, Args, CommandFactory, Parser, Subcommand, ValueEnum};
Expand Down Expand Up @@ -135,7 +136,7 @@ enum RustupSubcmd {
)]
Update {
/// Toolchain name, such as 'stable', 'nightly', or '1.8.0'. For more information see `rustup help toolchain`
#[arg(num_args = 1..)]
#[arg(num_args = 1.., value_parser = update_toolchain_value_parser)]
toolchain: Vec<PartialToolchainDesc>,

/// Don't perform self update when running the `rustup update` command
Expand Down Expand Up @@ -258,6 +259,14 @@ enum RustupSubcmd {
},
}

fn update_toolchain_value_parser(s: &str) -> Result<PartialToolchainDesc> {
PartialToolchainDesc::from_str(s).inspect_err(|_| {
if s == "self" {
info!("if you meant to update rustup itself, use `rustup self update`");
}
})
}

#[derive(Debug, Subcommand)]
enum ShowSubcmd {
/// Show the active toolchain
Expand Down
6 changes: 6 additions & 0 deletions src/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,13 @@ impl<'a> Toolchain<'a> {
fs::remove_dir_all(&path)?;
true
} else {
let name = name.to_string();
info!("no toolchain installed for '{name}'");
if name == "self" {
info!(
"if you meant to uninstall rustup itself, use `rustup self uninstall`"
);
}
false
}
}
Expand Down

0 comments on commit 4689949

Please sign in to comment.