oseda_cli/
github.rs

1/*
2user.email=hatfield.69@wright.edu
3user.name=ReeseHatfield
4core.editor=/usr/bin/vim
5*/
6
7use std::{error::Error, path::Path, process::Command};
8
9pub fn get_config(key: &str) -> Option<String> {
10    let handle = Command::new("git")
11        .arg("config")
12        .arg("list")
13        .output()
14        .ok()?;
15
16    let conf_out = String::from_utf8(handle.stdout).ok()?;
17
18    conf_out.split("\n").find_map(|line| {
19        // restrict to only the first 2, shouldnt matter but good convention
20        let mut parts = line.splitn(2, '=');
21        // the ? will still do the early return from closure
22        let cur_key = parts.next()?;
23        let value = parts.next()?;
24        if cur_key == key {
25            Some(value.to_string())
26        } else {
27            None
28        }
29    })
30}
31
32// super generic run git func for general usecases
33pub fn git(dir: &Path, args: &[&str]) -> Result<(), Box<dyn Error>> {
34    let status = Command::new("git").current_dir(dir).args(args).status()?;
35
36    if !status.success() {
37        return Err(format!("git {:?} failed", args).into());
38    }
39    Ok(())
40}