diff --git a/src/args.rs b/src/args.rs index db633f3..9512b24 100644 --- a/src/args.rs +++ b/src/args.rs @@ -58,6 +58,14 @@ pub enum Command { /// Clears the download cache #[command()] ClearCache, + + /// Pins binary to a specific node version + #[command()] + Pin(PinArgs), + + /// Unpins a command + #[command()] + Unpin(UnpinArgs), } #[derive(Clone, Debug, Parser)] @@ -71,6 +79,20 @@ pub struct ExecArgs { pub args: Vec, } +#[derive(Clone, Debug, Parser)] +pub struct PinArgs { + /// The command to pin + pub command: String, + /// The version to pin the command to + pub version: NodeVersion, +} + +#[derive(Clone, Debug, Parser)] +pub struct UnpinArgs { + /// The command to unpin + pub command: String, +} + #[derive(Clone, Debug, Parser)] pub struct InstallArgs { /// the version to install diff --git a/src/config/mod.rs b/src/config/mod.rs index db84418..1e82f3e 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -17,6 +17,7 @@ use crate::{ use self::value::Config; mod value; +pub use value::*; #[derive(Clone)] pub struct ConfigAccess { diff --git a/src/main.rs b/src/main.rs index 50efcc0..5c1f16c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::{env, process}; -use args::Args; +use args::{Args, PinArgs, UnpinArgs}; use clap::Parser; use nenv::Nenv; @@ -51,6 +51,10 @@ async fn main() -> Result<()> { args::Command::ListVersions => nenv.list_versions().await, args::Command::Init => nenv.init_nenv().await, args::Command::ClearCache => nenv.clear_cache().await, + args::Command::Pin(PinArgs { command, version }) => { + nenv.pin_command(command, version).await + } + args::Command::Unpin(UnpinArgs { command }) => nenv.unpin_command(command).await, _ => xkcd_unreachable!(), }?; diff --git a/src/nenv.rs b/src/nenv.rs index 5c6ed74..adadf83 100644 --- a/src/nenv.rs +++ b/src/nenv.rs @@ -1,7 +1,7 @@ use std::{ffi::OsString, str::FromStr}; use crate::{ - config::ConfigAccess, + config::{ConfigAccess, ExecutableConfig}, consts::{BIN_DIR, CACHE_DIR, VERSION_FILE_PATH}, error::VersionError, mapper::Mapper, @@ -224,6 +224,35 @@ impl Nenv { Ok(()) } + /// Pins a given command + #[tracing::instrument(skip(self))] + pub async fn pin_command(&self, command: String, version: NodeVersion) -> Result<()> { + let mut config = self.config.get_mut().await; + config.bins.insert( + command.clone(), + ExecutableConfig { + node_version: version.clone(), + }, + ); + println!( + "Pinned {} to {}", + command.bold(), + version.to_string().yellow().bold() + ); + + Ok(()) + } + + /// Unpins a given command + #[tracing::instrument(skip(self))] + pub async fn unpin_command(&self, command: String) -> Result<()> { + let mut config = self.config.get_mut().await; + config.bins.remove(&command); + println!("Unpinned {}", command.bold()); + + 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<()> {