[WINDOWS, LINUX, MACOS] Create, Delete .autoconfig folder
This commit is contained in:
parent
98564956ac
commit
988c78b615
6 changed files with 54 additions and 15 deletions
31
src/common/utils/configure/common.rs
Normal file
31
src/common/utils/configure/common.rs
Normal 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(())
|
||||
}
|
|
@ -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"))]
|
||||
|
|
|
@ -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()?;
|
||||
|
|
|
@ -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(())
|
||||
}
|
|
@ -3,7 +3,8 @@ use std::{io, fs};
|
|||
|
||||
pub enum InstallType {
|
||||
Command,
|
||||
Config
|
||||
Config,
|
||||
Root
|
||||
}
|
||||
|
||||
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 {
|
||||
InstallType::Command => "bin",
|
||||
InstallType::Config => "configs"
|
||||
InstallType::Config => "configs",
|
||||
InstallType::Root => ""
|
||||
};
|
||||
|
||||
Ok(home_dir.join(super::super::INSTALL_FOLDER).join(subfolder))
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Reference in a new issue