diff --git a/src/installer/utils/configure/mod.rs b/src/installer/utils/configure/mod.rs index 06536ab..8261ca7 100644 --- a/src/installer/utils/configure/mod.rs +++ b/src/installer/utils/configure/mod.rs @@ -1,6 +1,9 @@ -#[cfg(target_os = "windows")] +#[cfg(target_family = "windows")] mod windows; +#[cfg(target_family = "unix")] +mod unix; + mod common; pub enum ConfigMode { @@ -11,17 +14,19 @@ pub enum ConfigMode { pub fn configure(mode: &ConfigMode) -> Result<(), String> { common::configure_folder(&mode).ok_or(format!("Failed to configure folder"))?; - #[cfg(target_os = "windows")] + #[cfg(target_family = "windows")] windows::configure(&mode).ok_or(format!("Failed to configure environment"))?; - #[cfg(not(target_os = "windows"))] - #[cfg(not(target_os = "linux"))] - #[cfg(not(target_os = "macos"))] + #[cfg(target_family = "unix")] + unix::configure(&mode).ok_or(format!("Failed to configure environment"))?; + + #[cfg(not(target_family = "windows"))] + #[cfg(not(target_family = "unix"))] { Err(format!("OS not supported")) } - #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))] + #[cfg(any(target_family = "windows", target_family = "unix"))] { Ok(()) } diff --git a/src/installer/utils/configure/unix/env.rs b/src/installer/utils/configure/unix/env.rs new file mode 100644 index 0000000..e391ae6 --- /dev/null +++ b/src/installer/utils/configure/unix/env.rs @@ -0,0 +1,44 @@ +use std::io::{Write, Read}; + +use super::ConfigMode; +use super::super::super::file_utils; + +pub fn configure_env(mode: &ConfigMode) -> Option<()> { + let binary_folder_path = file_utils::get_install_dir(&file_utils::InstallType::Command).ok()?; + let binary_folder_str = binary_folder_path.to_str()?; + let bash_file = dirs::home_dir()?.join(".bashrc"); + let env_path_str = format!("export PATH=\"$PATH:{}\"\n", binary_folder_str); + + let mut file = std::fs::OpenOptions::new() + .create_new(!bash_file.exists()) + .write(true) + .read(true) + .append(true) + .open(bash_file.clone()).ok()?; + + let mut content = String::new(); + file.read_to_string(&mut content).ok()?; + let configured = content.contains(env_path_str.as_str()); + + match mode { + ConfigMode::INSTALL => { + if !configured { + file.write_fmt(format_args!("{}", env_path_str)).ok()?; + println!("[INFO] Folder {} added to PATH", binary_folder_str); + } + }, + ConfigMode::UNINSTALL => { + if configured { + let final_string = content.replace(env_path_str.as_str(), ""); + let mut file = std::fs::OpenOptions::new() + .write(true) + .truncate(true) + .open(bash_file).ok()?; + file.write_all(final_string.as_bytes()).ok()?; + println!("[INFO] Folder {} removed to PATH", binary_folder_str) + } + }, + } + + Some(()) +} \ No newline at end of file diff --git a/src/installer/utils/configure/unix/mod.rs b/src/installer/utils/configure/unix/mod.rs new file mode 100644 index 0000000..771c35a --- /dev/null +++ b/src/installer/utils/configure/unix/mod.rs @@ -0,0 +1,8 @@ +mod env; +use super::ConfigMode; + +pub fn configure(mode: &ConfigMode) -> Option<()> { + env::configure_env(mode)?; + + Some(()) +} \ No newline at end of file