use dprint_cli_core::types::ErrBox;
use std::path::{Path, PathBuf};
pub fn ensure_system_path(directory_path: &Path) -> Result<bool, ErrBox> {
use std::fs::File;
use std::fs::OpenOptions;
use std::io::{self, Write};
let system_path_dirs = super::common::get_system_path_dirs();
return if system_path_dirs.contains(&directory_path.to_path_buf()) {
return Ok(false);
} else {
let mut profile = get_profile()?;
writeln!(profile, "export PATH=\"$PATH:{}\"", directory_path.to_string_lossy())?;
profile.flush()?;
Ok(true)
};
fn get_profile() -> io::Result<File> {
dirs::home_dir()
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "No home directory"))
.and_then(find_profile)
}
fn find_profile(mut profile: PathBuf) -> io::Result<File> {
profile.push(".bash_profile");
let mut oo = OpenOptions::new();
oo.append(true).create(false);
oo.open(profile.clone())
.or_else(|_| {
profile.pop();
profile.push(".bash_login");
oo.open(profile.clone())
})
.or_else(|_| {
profile.pop();
profile.push(".profile");
oo.open(profile.clone())
})
.or_else(|_| {
profile.pop();
profile.push(".bash_profile");
oo.create(true);
oo.open(profile.clone())
})
}
}