Skip to content

Commit

Permalink
stash
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuany committed Jul 14, 2024
1 parent 4027196 commit 24484ce
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/snm_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
111 changes: 111 additions & 0 deletions crates/snm_node/src/snm_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 }
}
Expand Down

0 comments on commit 24484ce

Please sign in to comment.