Link command WIP

This commit is contained in:
2023-03-15 16:22:19 +01:00
parent 5fe8a29842
commit 1eaf759599
16 changed files with 500 additions and 0 deletions

18
src/utils/folder.rs Normal file
View File

@@ -0,0 +1,18 @@
use std::path::Path;
const APPLICATION_SUBFOLDER_NAME: &str = "alma";
pub fn get_config_home_path() -> String {
let config_home_path = std::env::var("XDG_CONFIG_HOME")
.map_or_else(
|_| {
let home_path = std::env::var("HOME").unwrap();
Path::new(&home_path).join(".config")
},
|c| Path::new(&c).to_path_buf(),
)
.join(APPLICATION_SUBFOLDER_NAME)
.display()
.to_string();
return config_home_path;
}

View File

@@ -0,0 +1,60 @@
use std::{
fs::File,
io::{BufRead, BufReader},
path::Path,
};
pub const OS_IDENTIFIER_DEFAULT: &str = "default";
pub const OS_IDENTIFIER_WINDOWS: &str = "windows";
pub const OS_IDENTIFIER_MAC: &str = "macos";
pub const OS_IDENTIFIER_LINUX: &str = "linux";
pub const OS_IDENTIFIER_FREEBSD: &str = "freebsd";
pub fn is_on_platform(current_os_identifier: &String, taget_os_identifier: &String) -> bool {
return taget_os_identifier == OS_IDENTIFIER_DEFAULT
|| current_os_identifier == taget_os_identifier;
}
pub fn get_os_identifier() -> String {
let base_os_identifier = match std::env::consts::OS {
"windows" => OS_IDENTIFIER_WINDOWS.to_string(),
"macos" => OS_IDENTIFIER_MAC.to_string(),
"linux" => get_linux_identifier(),
"freebsd" => OS_IDENTIFIER_FREEBSD.to_string(),
_ => String::from("unknown"),
};
let processor_architecture = std::env::consts::ARCH;
return base_os_identifier + "-" + processor_architecture;
}
fn get_linux_identifier() -> String {
let os_release_path = Path::new("/etc/os-release");
let distroname = if os_release_path.exists() {
get_os_release(os_release_path)
} else {
None
};
distroname.map_or(OS_IDENTIFIER_LINUX.to_string(), |s| {
OS_IDENTIFIER_LINUX.to_string() + "-" + &s
})
}
fn get_os_release(os_release_path: &Path) -> Option<String> {
let os_release_file = File::open(os_release_path).unwrap();
let reader = BufReader::new(os_release_file);
for line in reader.lines() {
if line.is_err() {
continue;
}
let line = line.unwrap();
if line.starts_with("ID=") {
let os_identifier = line.replace("ID=", "");
return Some(os_identifier);
}
}
return None;
}

81
src/utils/path.rs Normal file
View File

@@ -0,0 +1,81 @@
pub mod path {
use std::env::current_dir;
use crate::configuration::repository::read_repository_configuration;
pub fn get_repository_and_module_name(
args: &[std::string::String],
single_param_is_repo: bool,
) -> (Option<String>, Option<String>) {
//TODO: handle parameters
match args.len() {
0 => (None, None),
1 => {
return if single_param_is_repo {
(Some(args[0].clone()), None)
} else {
(None, Some(args[0].clone()))
};
}
_ => (Some(args[0].clone()), Some(args[1].clone())),
}
}
pub fn get_repo_source_and_target_dir_with_default(
repo_name: Option<String>,
) -> std::io::Result<(String, String)> {
if current_dir().is_err() {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"Could not get current directory!",
));
}
let source_dir_fallback = current_dir()?.display().to_string();
let mut target_dir_fallback = current_dir()?;
target_dir_fallback.push("..");
let target_dir_fallback = target_dir_fallback.display().to_string();
let (repo_source_dir, repo_target_dir) =
get_repo_source_and_target_dir(repo_name, source_dir_fallback, target_dir_fallback)?;
return Ok((repo_source_dir, repo_target_dir));
}
pub fn get_repo_source_and_target_dir(
repo_name: Option<String>,
fallback_source: String,
fallback_target: String,
) -> std::io::Result<(String, String)> {
if repo_name.is_none() {
return Ok((fallback_source, fallback_target));
}
let repo_name = repo_name.unwrap();
let repo_configurtaion = read_repository_configuration()?;
let repo_config = repo_configurtaion
.repositories
.iter()
.find(|x| x.name.clone().map_or(false, |s| s == repo_name));
if repo_config.is_none() {
println!(
"No repository configuration found for repository: {}",
repo_name
);
return Ok((fallback_source, fallback_target));
}
let repo_config = repo_config.unwrap();
let source_path = repo_config
.repository_path
.clone()
.unwrap_or(fallback_source);
let target_path = repo_config.link_path.clone().unwrap_or(fallback_target);
return Ok((source_path, target_path));
}
}