proplate_integration/
git.rs

1use std::{
2  path::Path,
3  process::{Command, Stdio},
4};
5
6use proplate_errors::{ProplateError, ProplateErrorKind, ProplateResult};
7use proplate_tui::logger;
8
9pub fn exec_cmd<'a, I: IntoIterator<Item = &'a str> + Copy>(
10  cmd: I,
11  path: &Path,
12) -> ProplateResult<()> {
13  let subcmd = cmd.into_iter().next().unwrap();
14
15  let child = Command::new("git")
16    .args(cmd)
17    .stdout(Stdio::piped())
18    .stderr(Stdio::piped())
19    .current_dir(path)
20    .spawn()
21    .map_err(|e| {
22      ProplateError::create(ProplateErrorKind::Git {
23        cmd: subcmd.into(),
24        raw_stderr: e.to_string(),
25      })
26    })?;
27
28  let output = child.wait_with_output().map_err(|e| {
29    ProplateError::create(ProplateErrorKind::Git {
30      cmd: subcmd.into(),
31      raw_stderr: e.to_string(),
32    })
33  })?;
34
35  match output.status.success() {
36    true => {
37      if !output.stdout.is_empty() {
38        println!(
39          "{}",
40          logger::success(&String::from_utf8_lossy(&output.stdout))
41        );
42      }
43      Ok(())
44    }
45    _ => Err(ProplateError::create(ProplateErrorKind::Git {
46      cmd: subcmd.into(),
47      raw_stderr: String::from_utf8_lossy(&output.stderr).into(),
48    })),
49  }
50}