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

@ -39,6 +39,7 @@ async fn main() -> Result<()> {
match args.command {
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::Exec(args) => {
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
#[tracing::instrument(skip(self))]
pub async fn set_system_default(&mut self, version: NodeVersion) -> Result<()> {

@ -13,7 +13,7 @@ use crate::{
versioning::{SimpleVersion, VersionMetadata},
};
use miette::{IntoDiagnostic, Result};
use miette::{Context, IntoDiagnostic, Result};
use self::{
downloader::{versions::Versions, NodeDownloader},
@ -175,6 +175,24 @@ impl Repository {
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
#[tracing::instrument(level = "debug", skip(self))]
pub async fn lookup_version(&mut self, version_req: &NodeVersion) -> Result<&VersionMetadata> {

@ -3,7 +3,7 @@ use std::{
time::Duration,
};
use dialoguer::Confirm;
use dialoguer::{theme::ColorfulTheme, Confirm};
use indicatif::{ProgressBar, ProgressStyle};
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 {
Confirm::new()
Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(prompt.to_string())
.default(default)
.interact()

Loading…
Cancel
Save