From 24484ce192f8dbab9e658ba877c91030c9989e0a Mon Sep 17 00:00:00 2001 From: ityuany <519495771@qq.com> Date: Mon, 15 Jul 2024 02:11:28 +0800 Subject: [PATCH] stash --- crates/snm_node/Cargo.toml | 1 + crates/snm_node/src/snm_node.rs | 111 ++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) diff --git a/crates/snm_node/Cargo.toml b/crates/snm_node/Cargo.toml index 1efe821d..e70f8e0f 100644 --- a/crates/snm_node/Cargo.toml +++ b/crates/snm_node/Cargo.toml @@ -25,6 +25,7 @@ snm_utils = { path = "../snm_utils" } snm_config = { path = "../snm_config" } snm_node_version = { path = "../snm_node_version" } snm_tarball = { path = "../snm_tarball" } +snm_download_builder = { path = "../snm_download_builder" } xz2 = {version = "0.1.7" , features = ["static"]} tar = "0.4" num-format = "0.4.0" diff --git a/crates/snm_node/src/snm_node.rs b/crates/snm_node/src/snm_node.rs index 3814e952..1286d503 100644 --- a/crates/snm_node/src/snm_node.rs +++ b/crates/snm_node/src/snm_node.rs @@ -16,10 +16,13 @@ use sha2::Sha256; use snm_config::InstallStrategy; use snm_config::SnmConfig; use snm_core::traits::atom::AtomTrait; +use snm_download_builder::{DownloadBuilder, WriteStrategy}; use snm_tarball::decompress; use snm_utils::snm_error::SnmError; use snm_utils::to_ok::ToOk; use std::collections::HashMap; +use std::fs; +use std::ops::Not; use std::pin::Pin; use std::{ fs::File, @@ -32,6 +35,114 @@ pub struct SnmNode { } impl SnmNode { + async fn download(&self, version: &str) -> Result<(), SnmError> { + let download_url = self.get_download_url(version); + let downloaded_file_path_buf = self.get_downloaded_file_path_buf(version)?; + + DownloadBuilder::new() + .retries(3) + .write_strategy(WriteStrategy::Nothing) + .download(&download_url, &downloaded_file_path_buf) + .await?; + + let runtime = self.get_runtime_dir_path_buf(version)?; + + if runtime.exists() { + fs::remove_dir_all(&runtime)?; + } + + self.decompress_download_file(&downloaded_file_path_buf, &runtime)?; + + fs::remove_file(&downloaded_file_path_buf)?; + + Ok(()) + } + + pub async fn set_default(&self, version: &str) -> Result<(), SnmError> { + if self.get_anchor_file_path_buf(version)?.exists().not() { + let msg = format!( + "🤔 v{} is not installed, do you want to install it ?", + version + ); + if Confirm::new().with_prompt(msg).interact()? { + self.install(version).await?; + } + } else { + let default_dir = self.get_runtime_dir_for_default_path_buf()?; + if default_dir.exists() { + fs::remove_dir_all(&default_dir)?; + } + + let from_dir = self.get_runtime_dir_path_buf(version)?; + + #[cfg(unix)] + { + std::os::unix::fs::symlink(&from_dir, &default_dir)?; + } + #[cfg(windows)] + { + std::os::windows::fs::symlink_dir(&version_dir, &default_dir)?; + } + } + + Ok(()) + } + + pub async fn un_install(&self, version: &str) -> Result<(), SnmError> { + let default_dir = self.get_runtime_dir_for_default_path_buf()?; + let version_dir = self.get_runtime_dir_path_buf(&version)?; + if fs::read_link(&default_dir)?.eq(&version_dir) { + let msg = format!( + "🤔 {} is default instance, do you want to uninstall it ?", + version + ); + if Confirm::new().with_prompt(msg).interact()? { + fs::remove_file(&default_dir)?; + fs::remove_dir_all(version_dir)?; + } + } else { + fs::remove_dir_all(version_dir)?; + } + Ok(()) + } + + pub async fn install(&self, version: &str) -> Result<(), SnmError> { + let anchor_file = self.get_anchor_file_path_buf(&version)?; + let version_dir = self.get_runtime_dir_path_buf(&version)?; + + if anchor_file.exists().not() { + self.download(version).await?; + } else { + let confirm = Confirm::new() + .with_prompt(format!( + "🤔 v{} is already installed, do you want to reinstall it ?", + &version + )) + .interact() + .expect("install Confirm error"); + + if confirm { + fs::remove_dir_all(&version_dir)?; + self.download(version).await?; + } + + let default_dir = self.get_runtime_dir_for_default_path_buf()?; + + if default_dir.exists().not() { + #[cfg(unix)] + { + std::os::unix::fs::symlink(&version_dir, &default_dir)?; + } + #[cfg(windows)] + { + std::os::windows::fs::symlink_dir(&version_dir, &default_dir)?; + } + } + } + + Ok(()) + } + pub fn new(snm_config: SnmConfig) -> Self { Self { snm_config } }