-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add interactive prompt to
default
and pin
commands
Allow to interactively select a version to pin or set as default Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>
- Loading branch information
Showing
6 changed files
with
160 additions
and
60 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,67 @@ | ||
use miette::IntoDiagnostic; | ||
|
||
pub mod index; | ||
pub mod package; | ||
|
||
pub struct InstalledToolchain { | ||
/// Whether the installed toolchain is marked as default | ||
pub default: bool, | ||
|
||
/// Whether this toolchain is installed as 'latest' | ||
pub latest: bool, | ||
|
||
/// The actual version of the installed toolchain | ||
pub version: String, | ||
} | ||
|
||
pub fn installed_toolchains() -> miette::Result<Vec<InstalledToolchain>> { | ||
let toolchains_dir = crate::moonup_home().join("toolchains"); | ||
let default_version = default_toolchain(); | ||
|
||
let toolchains = toolchains_dir | ||
.read_dir() | ||
.into_diagnostic()? | ||
.filter_map(std::io::Result::ok) | ||
.filter_map(|e| { | ||
let path = e.path(); | ||
let version = path.file_name().map(|n| { | ||
let n = n.to_ascii_lowercase(); | ||
let latest = n == "latest"; | ||
let default = match default_version.as_deref() { | ||
Some(v) => v == n, | ||
None => false, | ||
}; | ||
let version = match latest { | ||
false => n.to_string_lossy().to_string(), | ||
true => std::fs::read_to_string(path.join("version")) | ||
.ok() | ||
.map(|s| s.trim().to_string()) | ||
.unwrap_or_else(|| "latest".to_string()), | ||
}; | ||
|
||
InstalledToolchain { | ||
default, | ||
latest, | ||
version, | ||
} | ||
}); | ||
|
||
version | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
Ok(toolchains) | ||
} | ||
|
||
fn default_toolchain() -> Option<String> { | ||
let default_file = crate::moonup_home().join("default"); | ||
|
||
std::fs::read_to_string(default_file).ok().and_then(|s| { | ||
let v = s.trim().to_string(); | ||
if v.is_empty() { | ||
None | ||
} else { | ||
Some(v) | ||
} | ||
}) | ||
} |