This commit is contained in:
nora 2024-01-18 20:47:46 +01:00
parent 43d792e148
commit 89b3e2df37
13 changed files with 211 additions and 40 deletions

View file

@ -2,20 +2,54 @@ use color_eyre::{
eyre::{bail, Context},
Result,
};
use std::process::Command;
use fs_extra::dir::CopyOptions;
use std::{path::Path, process::Command};
pub fn run_process(cmd: &mut Command) -> Result<String> {
let name = cmd.get_program().to_os_string();
let output = cmd
.output()
.wrap_err(format!("failed to spawn process {name:?}"))?;
fn run_process_inner(cmd: &mut Command) -> Result<String> {
let name = cmd.get_program().to_os_string();
let output = cmd
.output()
.wrap_err(format!("failed to spawn process {name:?}"))?;
if !output.status.success() {
bail!(
"command returned error: {}",
String::from_utf8(output.stderr).wrap_err("stderr is not UTF-8")?
);
if !output.status.success() {
bail!(
"command returned error: {}",
String::from_utf8(output.stderr).wrap_err("stderr is not UTF-8")?
);
}
Ok(String::from_utf8(output.stdout).wrap_err("stdout is not UTF-8")?)
}
Ok(String::from_utf8(output.stdout).wrap_err("stdout is not UTF-8")?)
run_process_inner(cmd).wrap_err(format!(
"{} {}",
cmd.get_program().to_str().unwrap(),
cmd.get_args()
.map(|arg| format!("'{}'", arg.to_str().unwrap()))
.collect::<Vec<_>>()
.join(" ")
))
}
pub fn create_dir_if_not_exist(p: &Path) -> Result<()> {
match std::fs::create_dir(p) {
Ok(()) => debug!(?p, "Created directory"),
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {}
e => return e.wrap_err("failed to create submodules"),
}
Ok(())
}
pub fn cp_r(from: &Path, to: &Path) -> Result<()> {
fs_extra::copy_items(
&[from],
to,
&CopyOptions {
overwrite: true,
copy_inside: true,
..CopyOptions::default()
},
)
.wrap_err(format!("copying to {}", to.display()))?;
Ok(())
}