diff --git a/src/common/utils/configure/common.rs b/src/common/utils/configure/common.rs new file mode 100644 index 0000000..89caf9d --- /dev/null +++ b/src/common/utils/configure/common.rs @@ -0,0 +1,31 @@ +use std::path::Path; +use std::fs::{create_dir_all, remove_dir_all}; + +use super::ConfigMode; +use super::super::installer; + +fn folder_exist(folder: &str) -> bool { + Path::new(folder).is_dir() +} + +pub fn configure_folder(mode: &ConfigMode) -> Option<()> { + let binary_folder_path = installer::get_install_dir(installer::InstallType::Root).ok()?; + let binary_folder_str = binary_folder_path.to_str()?; + + match mode { + ConfigMode::INSTALL => { + if !folder_exist(binary_folder_str) { + println!("[INFO] Create {} folder", binary_folder_str); + create_dir_all(binary_folder_path).ok()?; + } + }, + ConfigMode::UNINSTALL => { + if folder_exist(binary_folder_str) { + println!("[INFO] Remove {} folder", binary_folder_str); + remove_dir_all(binary_folder_path).ok()?; + } + } + } + + Some(()) +} \ No newline at end of file diff --git a/src/common/utils/configure/mod.rs b/src/common/utils/configure/mod.rs index ea2f705..06536ab 100644 --- a/src/common/utils/configure/mod.rs +++ b/src/common/utils/configure/mod.rs @@ -1,9 +1,18 @@ #[cfg(target_os = "windows")] mod windows; -pub fn configure() -> Result<(), String> { +mod common; + +pub enum ConfigMode { + INSTALL, + UNINSTALL +} + +pub fn configure(mode: &ConfigMode) -> Result<(), String> { + common::configure_folder(&mode).ok_or(format!("Failed to configure folder"))?; + #[cfg(target_os = "windows")] - windows::configure().ok_or(format!("Failed to configure environment"))?; + windows::configure(&mode).ok_or(format!("Failed to configure environment"))?; #[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "linux"))] diff --git a/src/common/utils/configure/windows/env.rs b/src/common/utils/configure/windows/env.rs index 04ab6ff..b0e4267 100644 --- a/src/common/utils/configure/windows/env.rs +++ b/src/common/utils/configure/windows/env.rs @@ -1,11 +1,7 @@ use winreg::RegKey; use winreg::enums::*; use crate::common::utils::installer; - -pub enum ConfigEnvMode { - ADD, - REMOVE -} +use super::ConfigMode; fn env_exist(path_env: &str, binary_folder: &str) -> bool { for env in path_env.split(';') { @@ -17,7 +13,7 @@ fn env_exist(path_env: &str, binary_folder: &str) -> bool { false } -pub fn configure_env(mode: ConfigEnvMode) -> Option<()> { +pub fn configure_env(mode: &ConfigMode) -> Option<()> { let hklm = RegKey::predef(HKEY_CURRENT_USER); let environment = hklm.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE).ok()?; let mut reg_value : String = environment.get_value("PATH").ok()?; @@ -25,14 +21,14 @@ pub fn configure_env(mode: ConfigEnvMode) -> Option<()> { let binary_folder_str = binary_folder_path.to_str()?; match mode { - ConfigEnvMode::ADD => { + ConfigMode::INSTALL => { if !env_exist(reg_value.as_str(), binary_folder_str) { reg_value.push_str(format!(";{}", binary_folder_str).as_str()); // Add binary folder to path environment.set_value("PATH", ®_value).ok()?; println!("[INFO] Folder {} added to PATH", binary_folder_str); } }, - ConfigEnvMode::REMOVE => { + ConfigMode::UNINSTALL => { if env_exist(reg_value.as_str(), binary_folder_str) { let new_value = reg_value.replace(format!(";{}", binary_folder_str).as_str(), ""); // Remove binary folder to path environment.set_value("PATH", &new_value).ok()?; diff --git a/src/common/utils/configure/windows/mod.rs b/src/common/utils/configure/windows/mod.rs index 920fc20..771c35a 100644 --- a/src/common/utils/configure/windows/mod.rs +++ b/src/common/utils/configure/windows/mod.rs @@ -1,7 +1,8 @@ mod env; +use super::ConfigMode; -pub fn configure() -> Option<()> { - env::configure_env(env::ConfigEnvMode::ADD)?; +pub fn configure(mode: &ConfigMode) -> Option<()> { + env::configure_env(mode)?; Some(()) } \ No newline at end of file diff --git a/src/common/utils/installer.rs b/src/common/utils/installer.rs index 28afe57..7b236e3 100644 --- a/src/common/utils/installer.rs +++ b/src/common/utils/installer.rs @@ -3,7 +3,8 @@ use std::{io, fs}; pub enum InstallType { Command, - Config + Config, + Root } pub fn get_install_dir(install_type: InstallType) -> Result { @@ -12,7 +13,8 @@ pub fn get_install_dir(install_type: InstallType) -> Result { let subfolder = match install_type { InstallType::Command => "bin", - InstallType::Config => "configs" + InstallType::Config => "configs", + InstallType::Root => "" }; Ok(home_dir.join(super::super::INSTALL_FOLDER).join(subfolder)) diff --git a/src/main.rs b/src/main.rs index 5eddeed..7d02c8f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ mod macos; pub mod common; fn main() { - if let Err(err) = common::utils::configure::configure() { + if let Err(err) = common::utils::configure::configure(&common::utils::configure::ConfigMode::INSTALL) { eprintln!("[ERROR] {}", err); return; }