use std::fs::File;
use std::io::{self, BufRead};
use std::path::{Path, PathBuf};
pub fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
pub fn pkgdb_dir() -> Option<PathBuf> {
if let Ok(lines) = read_lines("/etc/mk.conf") {
let line = lines
.map_while(Result::ok)
.find(|l| l.starts_with("PKG_DBDIR"));
if let Some(pkg_dbdir) = line {
if let Some(value) = pkg_dbdir.split('=').nth(1) {
return Some(PathBuf::from(value.trim()));
}
};
}
Some(PathBuf::from("/usr/pkg/pkgdb"))
}
pub fn localbase_dir() -> Option<PathBuf> {
if let Ok(lines) = read_lines("/etc/mk.conf") {
let line = lines
.map_while(Result::ok)
.find(|l| l.starts_with("LOCALBASE"));
if let Some(pkg_dbdir) = line {
if let Some(value) = pkg_dbdir.split('=').nth(1) {
return Some(PathBuf::from(value.trim()));
}
};
}
Some(PathBuf::from("/usr/pkg"))
}