diff --git a/src/args.rs b/src/args.rs index b4cec68..10d34a6 100644 --- a/src/args.rs +++ b/src/args.rs @@ -20,6 +20,10 @@ pub enum Command { #[command(short_flag = 'v', aliases = &["--version"])] Version, + /// Initializes nenv directories and installs a default node version + #[command()] + Init, + /// Installs the given node version #[command()] Install(InstallArgs), @@ -43,9 +47,9 @@ pub enum Command { #[command()] Exec(ExecArgs), - /// Initializes nenv directories and installs a default node version + /// Clears the download cache #[command()] - Init, + ClearCache, } #[derive(Clone, Debug, Parser)] diff --git a/src/main.rs b/src/main.rs index 9baa40b..3d313d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,6 +48,7 @@ async fn main() -> Result<()> { args::Command::RemapBinaries => nenv.remap().await, args::Command::ListVersions => nenv.list_versions().await, args::Command::Init => nenv.init_nenv().await, + args::Command::ClearCache => nenv.clear_cache().await, _ => xkcd_unreachable!(), }?; diff --git a/src/nenv.rs b/src/nenv.rs index 74011c5..d59614e 100644 --- a/src/nenv.rs +++ b/src/nenv.rs @@ -2,7 +2,7 @@ use std::{ffi::OsString, str::FromStr}; use crate::{ config::ConfigAccess, - consts::{BIN_DIR, VERSION_FILE_PATH}, + consts::{BIN_DIR, CACHE_DIR, VERSION_FILE_PATH}, error::VersionError, mapper::Mapper, repository::{NodeVersion, Repository}, @@ -11,7 +11,7 @@ use crate::{ }; use crossterm::style::Stylize; use dialoguer::{theme::ColorfulTheme, Input, Select}; -use miette::{IntoDiagnostic, Result}; +use miette::{Context, IntoDiagnostic, Result}; use tokio::fs; pub struct Nenv { @@ -138,6 +138,7 @@ impl Nenv { } /// Initializes nenv and prompts for a default version. + #[tracing::instrument(skip(self))] pub async fn init_nenv(&mut self) -> Result<()> { let items = vec!["latest", "lts", "custom"]; let selection = Select::with_theme(&ColorfulTheme::default()) @@ -170,6 +171,22 @@ impl Nenv { Ok(()) } + /// Clears the download cache + #[tracing::instrument(skip(self))] + pub async fn clear_cache(&self) -> Result<()> { + fs::remove_dir_all(&*CACHE_DIR) + .await + .into_diagnostic() + .context("Removing cache directory")?; + fs::create_dir_all(&*CACHE_DIR) + .await + .into_diagnostic() + .context("Creating cache directory")?; + println!("Cleared download cache."); + + Ok(()) + } + /// Persits all changes made that aren't written to the disk yet #[tracing::instrument(level = "debug", skip(self))] pub async fn persist(&self) -> Result<()> {