mirror of https://github.com/Trivernis/nenv
Add use command and mapper
parent
a39df138c9
commit
1bd3e79cde
@ -1,16 +1,60 @@
|
||||
use crossterm::style::Stylize;
|
||||
use mapper::Mapper;
|
||||
use repository::{config::Config, NodeVersion, Repository};
|
||||
|
||||
mod consts;
|
||||
pub mod error;
|
||||
pub mod mapper;
|
||||
pub mod repository;
|
||||
mod utils;
|
||||
mod web_api;
|
||||
use dialoguer::Confirm;
|
||||
use error::Result;
|
||||
|
||||
pub async fn install_version(version: NodeVersion) -> Result<()> {
|
||||
get_repository().await?.install_version(version).await
|
||||
let repo = get_repository().await?;
|
||||
|
||||
if repo.is_installed(&version).await? {
|
||||
if !Confirm::new()
|
||||
.with_prompt("The version {version} is already installed. Reinstall?")
|
||||
.default(false)
|
||||
.interact()
|
||||
.unwrap()
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
repo.install_version(&version).await?;
|
||||
println!("Installed {}", version.to_string().bold());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn use_version(version: NodeVersion) -> Result<()> {
|
||||
let mut mapper = get_mapper().await?;
|
||||
|
||||
if !mapper.repository().is_installed(&version).await?
|
||||
&& Confirm::new()
|
||||
.with_prompt(format!(
|
||||
"The version {version} is not installed. Do you want to install it?"
|
||||
))
|
||||
.default(false)
|
||||
.interact()
|
||||
.unwrap()
|
||||
{
|
||||
mapper.repository().install_version(&version).await?;
|
||||
}
|
||||
|
||||
mapper.use_version(&version).await?;
|
||||
println!("Now using {}", version.to_string().bold());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_repository() -> Result<Repository> {
|
||||
Repository::init(Config::load().await?).await
|
||||
}
|
||||
|
||||
async fn get_mapper() -> Result<Mapper> {
|
||||
Ok(Mapper::new(get_repository().await?))
|
||||
}
|
||||
|
@ -0,0 +1,17 @@
|
||||
use miette::Diagnostic;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::repository::config::ConfigError;
|
||||
|
||||
pub type MapperResult<T> = Result<T, MapperError>;
|
||||
|
||||
#[derive(Error, Diagnostic, Debug)]
|
||||
pub enum MapperError {
|
||||
#[error("Config error: {0}")]
|
||||
Config(
|
||||
#[from]
|
||||
#[source]
|
||||
#[diagnostic_source]
|
||||
ConfigError,
|
||||
),
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
use std::{env, str::FromStr};
|
||||
|
||||
use crate::repository::{NodeVersion, Repository};
|
||||
|
||||
use self::error::MapperResult;
|
||||
|
||||
pub mod error;
|
||||
/// Responsible for mapping to node executables
|
||||
/// and managing node versions
|
||||
pub struct Mapper {
|
||||
repo: Repository,
|
||||
active_version: NodeVersion,
|
||||
}
|
||||
|
||||
impl Mapper {
|
||||
pub fn new(repository: Repository) -> Self {
|
||||
let version =
|
||||
Self::get_version().unwrap_or_else(|| repository.config.default_version.to_owned());
|
||||
Self {
|
||||
repo: repository,
|
||||
active_version: version,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn repository(&self) -> &Repository {
|
||||
&self.repo
|
||||
}
|
||||
|
||||
/// Sets the given version as the default one
|
||||
pub async fn use_version(&mut self, version: &NodeVersion) -> MapperResult<()> {
|
||||
self.repo
|
||||
.config
|
||||
.set_default_version(version.clone())
|
||||
.await?;
|
||||
self.active_version = version.clone();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn active_version(&self) -> &NodeVersion {
|
||||
&self.active_version
|
||||
}
|
||||
|
||||
fn get_version() -> Option<NodeVersion> {
|
||||
env::var("NODE_VERSION")
|
||||
.ok()
|
||||
.and_then(|v| NodeVersion::from_str(&v).ok())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue