1
0
Fork 0

[WINDOWS, LINUX, MACOS] Create, Delete .autoconfig folder

This commit is contained in:
Florian RICHER (MrDev023) 2021-07-24 23:06:02 +02:00
parent 98564956ac
commit 988c78b615
6 changed files with 54 additions and 15 deletions

View file

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

View file

@ -1,9 +1,18 @@
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
mod 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")] #[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 = "windows"))]
#[cfg(not(target_os = "linux"))] #[cfg(not(target_os = "linux"))]

View file

@ -1,11 +1,7 @@
use winreg::RegKey; use winreg::RegKey;
use winreg::enums::*; use winreg::enums::*;
use crate::common::utils::installer; use crate::common::utils::installer;
use super::ConfigMode;
pub enum ConfigEnvMode {
ADD,
REMOVE
}
fn env_exist(path_env: &str, binary_folder: &str) -> bool { fn env_exist(path_env: &str, binary_folder: &str) -> bool {
for env in path_env.split(';') { for env in path_env.split(';') {
@ -17,7 +13,7 @@ fn env_exist(path_env: &str, binary_folder: &str) -> bool {
false false
} }
pub fn configure_env(mode: ConfigEnvMode) -> Option<()> { pub fn configure_env(mode: &ConfigMode) -> Option<()> {
let hklm = RegKey::predef(HKEY_CURRENT_USER); let hklm = RegKey::predef(HKEY_CURRENT_USER);
let environment = hklm.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE).ok()?; let environment = hklm.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE).ok()?;
let mut reg_value : String = environment.get_value("PATH").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()?; let binary_folder_str = binary_folder_path.to_str()?;
match mode { match mode {
ConfigEnvMode::ADD => { ConfigMode::INSTALL => {
if !env_exist(reg_value.as_str(), binary_folder_str) { 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 reg_value.push_str(format!(";{}", binary_folder_str).as_str()); // Add binary folder to path
environment.set_value("PATH", &reg_value).ok()?; environment.set_value("PATH", &reg_value).ok()?;
println!("[INFO] Folder {} added to PATH", binary_folder_str); println!("[INFO] Folder {} added to PATH", binary_folder_str);
} }
}, },
ConfigEnvMode::REMOVE => { ConfigMode::UNINSTALL => {
if env_exist(reg_value.as_str(), binary_folder_str) { 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 let new_value = reg_value.replace(format!(";{}", binary_folder_str).as_str(), ""); // Remove binary folder to path
environment.set_value("PATH", &new_value).ok()?; environment.set_value("PATH", &new_value).ok()?;

View file

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

View file

@ -3,7 +3,8 @@ use std::{io, fs};
pub enum InstallType { pub enum InstallType {
Command, Command,
Config Config,
Root
} }
pub fn get_install_dir(install_type: InstallType) -> Result<PathBuf, String> { pub fn get_install_dir(install_type: InstallType) -> Result<PathBuf, String> {
@ -12,7 +13,8 @@ pub fn get_install_dir(install_type: InstallType) -> Result<PathBuf, String> {
let subfolder = match install_type { let subfolder = match install_type {
InstallType::Command => "bin", InstallType::Command => "bin",
InstallType::Config => "configs" InstallType::Config => "configs",
InstallType::Root => ""
}; };
Ok(home_dir.join(super::super::INSTALL_FOLDER).join(subfolder)) Ok(home_dir.join(super::super::INSTALL_FOLDER).join(subfolder))

View file

@ -10,7 +10,7 @@ mod macos;
pub mod common; pub mod common;
fn main() { 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); eprintln!("[ERROR] {}", err);
return; return;
} }