1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use std::{fs, path::PathBuf};

use git2::Repository;
use miette::{IntoDiagnostic, Result, WrapErr};

/// Find and read the correct toml config
///
/// # Errors
///
/// If we can't find a git repository, or if reading the toml file works
pub fn read_toml(path: PathBuf) -> Result<String> {
    let repository = Repository::discover(path)
        .into_diagnostic()
        .wrap_err("failed to work out location of repository")?;
    let path = repository.path();
    let bare = path.parent().unwrap_or(path).join(".git-mit.toml");
    let dist = path.parent().unwrap_or(path).join(".git-mit.toml.dist");

    if bare.exists() {
        return fs::read_to_string(bare).into_diagnostic();
    }

    if dist.exists() {
        return fs::read_to_string(dist).into_diagnostic();
    }

    Ok("".to_string())
}