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
9/// Gets a value from the users local git configuration, see example
10///
11/// # Arguments
12/// * `key` - the config key to look for -> e.g core.editor
13///
14/// # Returns
15/// * `Some(String)` with the value if the key is found
16/// * `None` on key retrieval failure
17///
18/// # Example
19/// ```
20/// let name = get_config("user.name");
21/// ```
22pub fn get_config(key: &str) -> Option<String> {
23 let handle = Command::new("git")
24 .arg("config")
25 .arg("list")
26 .output()
27 .ok()?;
28
29 let conf_out = String::from_utf8(handle.stdout).ok()?;
30
31 conf_out.split("\n").find_map(|line| {
32 let (cur_key, value) = line.split_once('=')?;
33 if cur_key == key {
34 Some(value.to_string())
35 } else {
36 None
37 }
38 })
39}
40
41/// Super generic run git func for general usecases git commands
42///
43/// # Arguments
44/// * `dir` - the directory to run the git command in
45/// * `args` - the list of arguments to pass to git -> e.g. `["clone", "[URL]" ]`
46///
47/// # Returns (based on git exit code)
48/// * `Ok(())` if the git command succeeds
49/// * `Err` if the command fails
50pub fn git(dir: &Path, args: &[&str]) -> Result<(), Box<dyn Error>> {
51 let status = Command::new("git").current_dir(dir).args(args).status()?;
52
53 if !status.success() {
54 return Err(format!("git {:?} failed", args).into());
55 }
56 Ok(())
57}