use std::{ffi::OsStr, fs, path::PathBuf};
use walkdir::WalkDir;
use crate::errors::TeacupError;
pub fn to_absolute_path(path: &String) -> Result<String, TeacupError> {
let fullpath =
fs::canonicalize(path).map_err(|e| TeacupError::LocalRepoError(e.to_string()))?;
fullpath
.to_str()
.map(|s| String::from(s))
.ok_or(TeacupError::LocalRepoError(format!(
"Unable to build local filepath from {:?}",
fullpath
)))
}
pub fn find_git_repos(base_path: &String) -> Vec<String> {
WalkDir::new(base_path)
.into_iter()
.filter_entry(|e| {
if let Some(parent) = e.path().parent() {
if parent.join(".git").exists() && e.file_name() != ".git" {
return false;
}
}
true
})
.filter_map(|e| e.ok())
.filter(|e| e.file_name() == ".git" && e.file_type().is_dir())
.filter_map(|e| e.path().parent().map(|p| p.to_path_buf()))
.map(|path| String::from(path.to_str().unwrap()))
.collect()
}