Add list-versions command

feature/lookup-installed
trivernis 2 years ago
parent c446898c3d
commit b5f98784f7
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,6 +1,6 @@
[package]
name = "nenv"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
[lib]
@ -14,6 +14,7 @@ path = "src/main.rs"
[dependencies]
clap = { version = "4.1.1", features = ["derive"] }
color-eyre = "0.6.2"
crossterm = "0.25.0"
dialoguer = "0.10.3"
dirs = "4.0.0"
@ -22,7 +23,7 @@ futures-util = "0.3.25"
indicatif = "0.17.3"
lazy_static = "1.4.0"
libflate = "1.2.0"
miette = { version = "5.5.0", features = ["fancy"] }
miette = "5.5.0"
reqwest = { version = "0.11.14", features = ["json", "stream"] }
semver = { version = "1.0.16", features = ["std", "serde"] }
serde = { version = "1.0.152", features = ["derive"] }

@ -28,6 +28,10 @@ pub enum Command {
#[command()]
Refresh,
/// Lists all available versions
#[command(name = "list-versions")]
ListVersions,
/// Executes the given version specific node executable
#[command()]
Exec(ExecArgs),

@ -15,6 +15,8 @@ use dialoguer::Confirm;
use error::Result;
use tokio::fs;
use crate::error::VersionError;
pub async fn install_version(version: NodeVersion) -> Result<()> {
if VERSION_FILE_PATH.exists() {
fs::remove_file(&*VERSION_FILE_PATH).await?;
@ -78,6 +80,37 @@ pub async fn refresh() -> Result<()> {
Ok(())
}
pub async fn list_versions() -> Result<()> {
let mapper = get_mapper().await?;
let versions = mapper.repository().installed_versions().await?;
let active_version = mapper
.repository()
.lookup_version(mapper.active_version())?;
println!("{}", "Installed versions:".bold());
for version in versions {
let info = mapper
.repository()
.all_versions()
.get(&version)
.ok_or_else(|| VersionError::UnkownVersion(version.to_string()))?;
let lts = info
.lts
.as_ref()
.map(|l| format!(" ({})", l.to_owned().green()))
.unwrap_or_default();
if version == active_version.version {
println!(" {}{} [current]", version.to_string().blue().bold(), lts)
} else {
println!(" {}{}", version.to_string().blue(), lts)
}
}
Ok(())
}
async fn get_repository() -> Result<Repository> {
Repository::init(Config::load().await?).await
}

@ -19,6 +19,7 @@ async fn main() -> miette::Result<()> {
process::exit(exit_code);
}
args::Command::Refresh => nenv::refresh().await,
args::Command::ListVersions => nenv::list_versions().await,
}?;
Ok(())

@ -118,7 +118,7 @@ impl Repository {
/// Returns the path for the given node version
pub fn get_version_path(&self, version: &NodeVersion) -> LibResult<Option<NodePath>> {
let info = self.parse_req(&version)?;
let info = self.lookup_version(&version)?;
let path = build_version_path(&info.version);
Ok(if path.exists() {
@ -144,14 +144,37 @@ impl Repository {
/// Returns if the given version is installed
pub fn is_installed(&self, version: &NodeVersion) -> LibResult<bool> {
let info = self.parse_req(version)?;
let info = self.lookup_version(version)?;
Ok(build_version_path(&info.version).exists())
}
/// Performs a lookup for the given node version
pub fn lookup_version(&self, version_req: &NodeVersion) -> Result<&VersionInfo, VersionError> {
let version = match version_req {
NodeVersion::Latest => self.versions.latest(),
NodeVersion::LatestLts => self.versions.latest_lts(),
NodeVersion::Lts(lts) => self
.versions
.get_lts(&lts)
.ok_or_else(|| VersionError::UnkownVersion(lts.to_owned()))?,
NodeVersion::Req(req) => self
.versions
.get_fulfilling(&req)
.ok_or_else(|| VersionError::Unfulfillable(req.to_owned()))?,
};
Ok(version)
}
/// Returns the reference to all known versions
pub fn all_versions(&self) -> &Versions {
&self.versions
}
/// Installs a specified node version
pub async fn install_version(&self, version_req: &NodeVersion) -> LibResult<()> {
let info = self.parse_req(&version_req)?;
let info = self.lookup_version(&version_req)?;
let archive_path = self.download_version(&info.version).await?;
self.extract_archive(info, &archive_path)?;
@ -178,23 +201,6 @@ impl Repository {
Ok(())
}
fn parse_req(&self, version_req: &NodeVersion) -> Result<&VersionInfo, VersionError> {
let version = match version_req {
NodeVersion::Latest => self.versions.latest(),
NodeVersion::LatestLts => self.versions.latest_lts(),
NodeVersion::Lts(lts) => self
.versions
.get_lts(&lts)
.ok_or_else(|| VersionError::UnkownVersion(lts.to_owned()))?,
NodeVersion::Req(req) => self
.versions
.get_fulfilling(&req)
.ok_or_else(|| VersionError::Unfulfillable(req.to_owned()))?,
};
Ok(version)
}
}
#[inline]

@ -95,4 +95,9 @@ impl Versions {
self.versions.get(fulfilling_versions.last()?)
}
/// Returns the info for the given version
pub fn get(&self, version: &Version) -> Option<&VersionInfo> {
self.versions.get(version)
}
}

Loading…
Cancel
Save