tcup 0.1.2

The one and only teacup client!
Documentation
use std::{ffi::OsStr, fs, path::PathBuf};

use walkdir::WalkDir;

use crate::errors::TeacupError;

pub fn to_absolute_path(path: &String) -> Result<String, TeacupError> {
    // Convert to absolute (also checks whether it exists)
    //
    let fullpath =
        fs::canonicalize(path).map_err(|e| TeacupError::LocalRepoError(e.to_string()))?;

    // Will pop if non-UTF8 pathname
    //
    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| {
            // Don't descend if parent directory contains ".git"
            // (unless we are ".git" ourselves ;))
            //
            if let Some(parent) = e.path().parent() {
                if parent.join(".git").exists() && e.file_name() != ".git" {
                    // println!("Ignoring {:?} (inside {:?})", e, parent);
                    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()
}