Skip to content

Commit 12f931e

Browse files
authored
Sort version output of spacetime version list (#4250)
# Description of Changes The output of `spacetime version list` is sorted in alphabetical order, which is not the correct semver order. This fixes the order printed, by sorting correctly. Example before: ``` 1.11.0 1.9.0 1.3.0 1.3.1 1.1.2 1.12.0 (current) 1.0.0 ``` After: ``` 1.12.0 (current) 1.11.0 1.9.0 1.3.1 1.3.0 1.1.2 1.0.0 ``` # API and ABI breaking changes Not an API or ABI breaking change # Expected complexity level and risk 1 # Testing Ran `spacetime version list` and `spacetime version list --all` with the new code and confirmed that the result was correctly sorted. I additionally tested that if the latest available version was not installed that it would show with the upgrade hint command.
1 parent 8706349 commit 12f931e

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

crates/update/src/cli/list.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,37 @@ impl List {
3636
Some(file_name.to_string())
3737
}
3838
};
39+
3940
let versions = if self.all {
4041
let client = super::reqwest_client()?;
4142
super::tokio_block_on(super::install::available_releases(&client))??
4243
} else {
4344
paths.cli_bin_dir.installed_versions()?
4445
};
46+
47+
// Sort versions using semver ordering. Versions that fail to parse are
48+
// placed at the end in their original listed order.
49+
let mut parsed: Vec<(semver::Version, String)> = Vec::new();
50+
let mut unparsed: Vec<String> = Vec::new();
4551
for ver in versions {
46-
print!("{ver}");
47-
if Some(&ver) == current.as_ref() {
48-
print!(" (current)");
52+
match semver::Version::parse(&ver) {
53+
Ok(sv) => parsed.push((sv, ver)),
54+
Err(_) => unparsed.push(ver),
4955
}
50-
println!();
5156
}
57+
parsed.sort_by(|(a, _), (b, _)| b.cmp(a));
58+
59+
let sorted_versions: Vec<String> = parsed.into_iter().map(|(_, s)| s).chain(unparsed).collect();
60+
for ver in &sorted_versions {
61+
let is_current = Some(ver) == current.as_ref();
62+
63+
if is_current {
64+
println!("{} (current)", ver);
65+
} else {
66+
println!("{ver}");
67+
}
68+
}
69+
5270
Ok(())
5371
}
5472
}

0 commit comments

Comments
 (0)