Add uninstall command

feature/lookup-installed
trivernis 1 year ago
parent a79eda9387
commit 88fc9d4742
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -28,6 +28,10 @@ pub enum Command {
#[command()] #[command()]
Install(InstallArgs), Install(InstallArgs),
/// Uninstalls the given node version
#[command()]
Uninstall(UninstallArgs),
/// Sets the specified version as the global default /// Sets the specified version as the global default
#[command()] #[command()]
Default(DefaultArgs), Default(DefaultArgs),
@ -69,6 +73,12 @@ pub struct InstallArgs {
pub version: NodeVersion, pub version: NodeVersion,
} }
#[derive(Clone, Debug, Parser)]
pub struct UninstallArgs {
/// the version to install
pub version: NodeVersion,
}
#[derive(Clone, Debug, Parser)] #[derive(Clone, Debug, Parser)]
pub struct DefaultArgs { pub struct DefaultArgs {
/// The version to set as default /// The version to set as default

@ -39,6 +39,7 @@ async fn main() -> Result<()> {
match args.command { match args.command {
args::Command::Install(v) => nenv.install(v.version).await, args::Command::Install(v) => nenv.install(v.version).await,
args::Command::Uninstall(v) => nenv.uninstall(v.version).await,
args::Command::Default(v) => nenv.set_system_default(v.version).await, args::Command::Default(v) => nenv.set_system_default(v.version).await,
args::Command::Exec(args) => { args::Command::Exec(args) => {
let exit_code = nenv.exec(args.command, args.args).await?; let exit_code = nenv.exec(args.command, args.args).await?;

@ -62,6 +62,24 @@ impl Nenv {
} }
} }
#[tracing::instrument(skip(self))]
pub async fn uninstall(&mut self, version: NodeVersion) -> Result<()> {
if prompt(
false,
format!(
"Do you really want to uninstall node {}?",
version.to_string().bold()
),
) {
self.repo.uninstall(&version).await?;
println!("Node {} has been removed.", version.to_string().bold())
} else {
println!("Nothing changed.");
}
Ok(())
}
/// Sets the system-wide default version /// Sets the system-wide default version
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub async fn set_system_default(&mut self, version: NodeVersion) -> Result<()> { pub async fn set_system_default(&mut self, version: NodeVersion) -> Result<()> {

@ -13,7 +13,7 @@ use crate::{
versioning::{SimpleVersion, VersionMetadata}, versioning::{SimpleVersion, VersionMetadata},
}; };
use miette::{IntoDiagnostic, Result}; use miette::{Context, IntoDiagnostic, Result};
use self::{ use self::{
downloader::{versions::Versions, NodeDownloader}, downloader::{versions::Versions, NodeDownloader},
@ -175,6 +175,24 @@ impl Repository {
Ok(()) Ok(())
} }
/// Uninstalls the given node version by deleting the versions directory
#[tracing::instrument(level = "debug", skip(self))]
pub async fn uninstall(&mut self, version: &NodeVersion) -> Result<()> {
let info = self.lookup_version(version).await?;
let version_dir = NODE_VERSIONS_DIR.join(info.version.to_string());
if !version_dir.exists() {
return Err(VersionError::not_installed(version).into());
}
fs::remove_dir_all(version_dir)
.await
.into_diagnostic()
.context("Deleting node version")?;
Ok(())
}
/// Performs a lookup for the given node version /// Performs a lookup for the given node version
#[tracing::instrument(level = "debug", skip(self))] #[tracing::instrument(level = "debug", skip(self))]
pub async fn lookup_version(&mut self, version_req: &NodeVersion) -> Result<&VersionMetadata> { pub async fn lookup_version(&mut self, version_req: &NodeVersion) -> Result<&VersionMetadata> {

@ -3,7 +3,7 @@ use std::{
time::Duration, time::Duration,
}; };
use dialoguer::Confirm; use dialoguer::{theme::ColorfulTheme, Confirm};
use indicatif::{ProgressBar, ProgressStyle}; use indicatif::{ProgressBar, ProgressStyle};
pub fn progress_bar(total: u64) -> ProgressBar { pub fn progress_bar(total: u64) -> ProgressBar {
@ -32,7 +32,7 @@ pub fn progress_spinner() -> ProgressBar {
} }
pub fn prompt<S: ToString>(default: bool, prompt: S) -> bool { pub fn prompt<S: ToString>(default: bool, prompt: S) -> bool {
Confirm::new() Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(prompt.to_string()) .with_prompt(prompt.to_string())
.default(default) .default(default)
.interact() .interact()

Loading…
Cancel
Save