Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Anchor CLI ignore non semver releases to avoid panic #3432

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,16 +556,16 @@ fn override_toolchain(cfg_override: &ConfigOverride) -> Result<RestoreToolchainC

let cfg = Config::discover(cfg_override)?;
if let Some(cfg) = cfg {
fn parse_version(text: &str) -> String {
Regex::new(r"(\d+\.\d+\.\S+)")
.unwrap()
.captures_iter(text)
.next()
.unwrap()
.get(0)
.unwrap()
.as_str()
.to_string()
fn parse_version(text: &str) -> Option<String> {
Some(
Regex::new(r"(\d+\.\d+\.\S+)")
.unwrap()
.captures_iter(text)
.next()?
.get(0)?
.as_str()
.to_string(),
)
}

fn get_current_version(cmd_name: &str) -> Result<String> {
Expand All @@ -577,7 +577,7 @@ fn override_toolchain(cfg_override: &ConfigOverride) -> Result<RestoreToolchainC
}

let output_version = std::str::from_utf8(&output.stdout)?;
let version = parse_version(output_version);
let version = parse_version(output_version).unwrap();
Ok(version)
Comment on lines +580 to 581
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this has a potential to fail, it would be better to avoid panicking here:

Suggested change
let version = parse_version(output_version).unwrap();
Ok(version)
parse_version(output_version)
.ok_or_else(|| anyhow!("Failed to parse the version of `{cmd_name}`"))

}

Expand Down Expand Up @@ -633,9 +633,11 @@ fn override_toolchain(cfg_override: &ConfigOverride) -> Result<RestoreToolchainC
}

// Hide the installation progress if the version is already installed
let is_installed = std::str::from_utf8(&output.stdout)?
.lines()
.any(|line| parse_version(line) == version);
let is_installed = std::str::from_utf8(&output.stdout)?.lines().any(|line| {
parse_version(line)
.map(|line_version| line_version == version)
.unwrap_or(false)
});
Comment on lines +636 to +640
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: avoid nesting:

Suggested change
let is_installed = std::str::from_utf8(&output.stdout)?.lines().any(|line| {
parse_version(line)
.map(|line_version| line_version == version)
.unwrap_or(false)
});
let is_installed = std::str::from_utf8(&output.stdout)?
.lines()
.filter_map(parse_version)
.any(|v| v == version);

let (stderr, stdout) = if is_installed {
(Stdio::null(), Stdio::null())
} else {
Expand Down
Loading