use crate::board;
use std::env::current_dir;
use std::path::PathBuf;
use std::process;
use std::str::from_utf8;
pub fn get_current_board() -> board::model::Board {
let current_working_directory =
current_dir().expect("Could not retrieve current working directory.");
let path_to_git_repository = get_root_path_if_git_repository(¤t_working_directory)
.expect("Could not retrieve path to git repository.");
board::data::get_by_location(&path_to_git_repository)
.expect("Boards cannot be accessed from outside git repository.")
}
pub fn get_root_path_if_git_repository(location: &PathBuf) -> Result<String, String> {
let output = process::Command::new("git")
.args(["rev-parse", "--show-toplevel"])
.current_dir(location)
.output();
match output {
Ok(value) => {
if value.stdout.is_empty() {
Err("Boards must be initialized inside of a git repository.".to_string())
} else {
let root_as_string = from_utf8(&value.stdout)
.expect("Could not parse path to git repository from stdout.");
Ok(String::from(root_as_string.trim()))
}
}
Err(_) => Err("Unreachable?".to_string()),
}
}
pub fn get_git_repository_name(location: &str) -> String {
let mut output: Vec<&str> = location.split('/').collect();
output
.pop()
.expect("Could not parse git repository name.")
.to_string()
}