1use crate::board;
2use std::env::current_dir;
3use std::path::PathBuf;
4use std::process;
5use std::str::from_utf8;
6
7pub fn get_current_board() -> board::model::Board {
8 let current_working_directory =
9 current_dir().expect("Could not retrieve current working directory.");
10
11 let path_to_git_repository = get_root_path_if_git_repository(¤t_working_directory)
12 .expect("Could not retrieve path to git repository.");
13
14 board::data::get_by_location(&path_to_git_repository)
15 .expect("Boards cannot be accessed from outside git repository.")
16}
17
18pub fn get_root_path_if_git_repository(location: &PathBuf) -> Result<String, String> {
19 let output = process::Command::new("git")
22 .args(["rev-parse", "--show-toplevel"])
23 .current_dir(location)
24 .output();
25
26 match output {
27 Ok(value) => {
28 if value.stdout.is_empty() {
29 Err("Boards must be initialized inside of a git repository.".to_string())
30 } else {
31 let root_as_string = from_utf8(&value.stdout)
32 .expect("Could not parse path to git repository from stdout.");
33 Ok(String::from(root_as_string.trim()))
34 }
35 }
36 Err(_) => Err("Unreachable?".to_string()),
37 }
38}
39
40pub fn get_git_repository_name(location: &str) -> String {
41 let mut output: Vec<&str> = location.split('/').collect();
44 output
45 .pop()
46 .expect("Could not parse git repository name.")
47 .to_string()
48}