Add pin and unpin command

main
trivernis 1 year ago
parent 82c8f579b0
commit 5f9be9d6df
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -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<OsString>,
}
#[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

@ -17,6 +17,7 @@ use crate::{
use self::value::Config;
mod value;
pub use value::*;
#[derive(Clone)]
pub struct ConfigAccess {

@ -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!(),
}?;

@ -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<()> {

Loading…
Cancel
Save