use std::{
io::Read,
path::{Path, PathBuf},
};
use crate::{
error::{Error, Result},
Profile,
};
pub fn contains_path_separator(s: &str) -> bool {
s.contains('/') || s.contains('\\')
}
pub fn get_profile_filepath(name: &str) -> Result<PathBuf> {
match Path::new(name).exists() {
true => Ok(PathBuf::from(name)),
false => {
if !Profile::does_exist(name) {
return Err(Error::ProfileDoesNotExist(name.to_string()));
}
Ok(get_configdir()
.join("profiles")
.join(format!("{}.env", name)))
}
}
}
pub fn get_profile_content(name: &str) -> Result<Vec<u8>> {
let profile_file_path = get_profile_filepath(name)?;
let mut file = std::fs::OpenOptions::new()
.read(true)
.open(profile_file_path)?;
let mut encrypted_contents = Vec::new();
file.read_to_end(&mut encrypted_contents).unwrap();
Ok(encrypted_contents)
}
pub fn get_configdir() -> PathBuf {
let homedir = dirs::home_dir().unwrap();
homedir.join(".envio")
}
pub fn truncate_identity_bytes(encrypted_contents: &[u8]) -> Vec<u8> {
let mut truncated_contents = encrypted_contents.to_owned();
truncated_contents.truncate(encrypted_contents.len() - 28);
truncated_contents
}