use crate::error_mod::{Error, Result};
use crate::public_api_mod::{RED, RESET};
pub fn exit_if_not_run_in_rust_project_root_directory() {
if !(camino::Utf8Path::new("automation_tasks_rs").exists() && (camino::Utf8Path::new("Cargo.toml").exists())) {
eprintln!("{RED}Error: `automation_tasks_rs` must be run inside the Rust project in the dir that contains");
println!("`Cargo.toml` file and `automation_tasks_rs` directory. Exiting...{RESET}");
std::process::exit(1);
}
}
pub fn completion_return_one_or_more_sub_commands(sub_commands: Vec<&str>, word_being_completed: &str) {
let mut sub_found = false;
for sub_command in sub_commands.iter() {
if sub_command.starts_with(word_being_completed) {
println!("{sub_command}");
sub_found = true;
}
}
if !sub_found {
for sub_command in sub_commands.iter() {
println!("{sub_command}");
}
}
}
pub fn home_dir() -> Result<std::path::PathBuf> {
match home::home_dir() {
Some(path_buff) => {
if !path_buff.as_os_str().is_empty() {
Ok(path_buff)
} else {
Err(Error::ErrorFromStr("{RED}Unable to get your home dir!{RESET}"))
}
}
None => Err(Error::ErrorFromStr("{RED}Unable to get your home dir!{RESET}")),
}
}
pub fn tilde_expand_to_home_dir_utf8(path_str: &str) -> Result<camino::Utf8PathBuf> {
let mut expanded = String::new();
if path_str.starts_with("~") {
let base = home::home_dir().ok_or_else(|| Error::ErrorFromStr("Cannot find home_dir in this OS."))?;
let base = base.to_string_lossy();
expanded.push_str(&base);
expanded.push_str(path_str.trim_start_matches("~"));
use std::str::FromStr;
Ok(camino::Utf8PathBuf::from_str(&expanded)?)
} else {
use std::str::FromStr;
Ok(camino::Utf8PathBuf::from_str(path_str)?)
}
}