1
0
Fork 0

[UNIX] Add env configuration

This commit is contained in:
Florian RICHER (MrDev023) 2021-08-11 21:04:07 +02:00
parent 8ea07a9796
commit 3e9f4470c0
3 changed files with 63 additions and 6 deletions

View file

@ -1,6 +1,9 @@
#[cfg(target_os = "windows")] #[cfg(target_family = "windows")]
mod windows; mod windows;
#[cfg(target_family = "unix")]
mod unix;
mod common; mod common;
pub enum ConfigMode { pub enum ConfigMode {
@ -11,17 +14,19 @@ pub enum ConfigMode {
pub fn configure(mode: &ConfigMode) -> Result<(), String> { pub fn configure(mode: &ConfigMode) -> Result<(), String> {
common::configure_folder(&mode).ok_or(format!("Failed to configure folder"))?; 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"))?; windows::configure(&mode).ok_or(format!("Failed to configure environment"))?;
#[cfg(not(target_os = "windows"))] #[cfg(target_family = "unix")]
#[cfg(not(target_os = "linux"))] unix::configure(&mode).ok_or(format!("Failed to configure environment"))?;
#[cfg(not(target_os = "macos"))]
#[cfg(not(target_family = "windows"))]
#[cfg(not(target_family = "unix"))]
{ {
Err(format!("OS not supported")) 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(()) Ok(())
} }

View file

@ -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(())
}

View file

@ -0,0 +1,8 @@
mod env;
use super::ConfigMode;
pub fn configure(mode: &ConfigMode) -> Option<()> {
env::configure_env(mode)?;
Some(())
}