From 257cfff3896905e3211ebdf62889c0411c2b232c Mon Sep 17 00:00:00 2001 From: ityuany <519495771@qq.com> Date: Fri, 12 Jul 2024 02:07:16 +0800 Subject: [PATCH] Use the node bin directory as a backing --- crates/snm_core/src/traits/atom.rs | 6 ++-- crates/snm_shim/src/lib.rs | 48 ++++++++++++++---------------- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/crates/snm_core/src/traits/atom.rs b/crates/snm_core/src/traits/atom.rs index 23e8f7ff..749be1b4 100644 --- a/crates/snm_core/src/traits/atom.rs +++ b/crates/snm_core/src/traits/atom.rs @@ -99,12 +99,12 @@ pub trait AtomTrait { Ok((dir_name_vec, default_dir)) } - fn get_default_version(&self) -> Result { + fn get_default_version(&self) -> Result, SnmError> { if self.get_snm_config().get_strict() { - return Err(SnmError::NotFoundValidVersion); + return Ok(None); } else { let (_, default_v) = self.read_runtime_dir_name_vec()?; - default_v.ok_or(SnmError::NotFoundValidVersion) + Ok(default_v) } } } diff --git a/crates/snm_shim/src/lib.rs b/crates/snm_shim/src/lib.rs index c1c36d2a..bd39a4e2 100644 --- a/crates/snm_shim/src/lib.rs +++ b/crates/snm_shim/src/lib.rs @@ -7,12 +7,9 @@ use snm_core::traits::atom::AtomTrait; use snm_node::snm_node::SnmNode; use snm_package_manager::snm_package_manager::SnmPackageManager; use snm_utils::{exec::exec_cli, snm_error::SnmError}; -use std::{ - env::{self, current_dir}, - ops::Not, -}; +use std::env::{self, current_dir}; -pub async fn load_package_manage_shim(prefix: &str, bin_name: &str) -> Result { +pub async fn load_package_manage_shim(prefix: &str, bin_name: &str) -> Result<(), SnmError> { env_logger::init(); let args_all: Vec = env::args().collect(); @@ -34,20 +31,8 @@ pub async fn load_package_manage_shim(prefix: &str, bin_name: &str) -> Result package_manager.version, + Some(package_manager) if package_manager.name == prefix => Some(package_manager.version), Some(package_manager) if restricted_list.contains(&command.as_str()) => { return Err(SnmError::NotMatchPackageManagerError { raw_command: args_all.join(" "), @@ -58,13 +43,16 @@ pub async fn load_package_manage_shim(prefix: &str, bin_name: &str) -> Result snm_package_manage.get_default_version()?, }; - let binary_dir_string = ensure_binary_path(snm_package_manage, &version).await?; + let binary_dir_string = match version { + Some(v) => ensure_binary_path(snm_package_manage, &v).await?, + None => "".to_string(), + }; let node_dir = get_node_bin_dir().await?; exec_cli(vec![binary_dir_string, node_dir], bin_name, &args)?; - Ok(version) + Ok(()) } pub async fn get_node_bin_dir() -> Result { @@ -75,11 +63,14 @@ pub async fn get_node_bin_dir() -> Result { let snm_node: &dyn AtomTrait = &SnmNode::new(snm_config.clone()); let version = match snm_config.get_runtime_node_version() { - Some(node_version) => node_version, + Some(node_version) => Some(node_version), None => snm_node.get_default_version()?, }; - let binary_dir_string = ensure_binary_path(snm_node, &version).await?; + let binary_dir_string = match version { + Some(v) => ensure_binary_path(snm_node, &v).await?, + None => return Err(SnmError::NotFoundValidVersion), + }; Ok(binary_dir_string) } @@ -96,13 +87,20 @@ pub async fn load_node_shim(bin_name: &str) -> Result<(), SnmError> { let snm_node: &dyn AtomTrait = &SnmNode::new(snm_config.clone()); let version = match snm_config.get_runtime_node_version() { - Some(node_version) => node_version, + Some(node_version) => Some(node_version), None => snm_node.get_default_version()?, }; - let binary_dir_string = ensure_binary_path(snm_node, &version).await?; + match version { + Some(v) => { + let binary_dir_string = ensure_binary_path(snm_node, &v).await?; - exec_cli(vec![binary_dir_string], bin_name, &args)?; + exec_cli(vec![binary_dir_string], bin_name, &args)?; + } + None => { + return Err(SnmError::NotFoundValidVersion); + } + } Ok(()) }