coliru

A minimal, flexible, dotfile installer
git clone https://git.ashermorgan.net/coliru/
Log | Files | Refs | README

commit 93f0823ddbb370d792ea3d55e4df824e47e1110e
parent e476da61668e5f65e322d46ef051604f41ff5e72
Author: Asher Morgan <59518073+ashermorgan@users.noreply.github.com>
Date:   Mon, 24 Jun 2024 14:17:51 -0700

Check for nonzero exit codes in run command

Diffstat:
Msrc/utils.rs | 33++++++++++++++++++++-------------
1 file changed, 20 insertions(+), 13 deletions(-)

diff --git a/src/utils.rs b/src/utils.rs @@ -1,17 +1,16 @@ use shellexpand::tilde; -use std::io::Result; +use std::io; use std::fs; #[cfg(target_family = "unix")] use std::os::unix::fs::symlink; use std::path::{PathBuf, absolute}; use std::process::Command; - /// Copies the contents of a local file to another local file. /// /// Tildes are expanded if present and the destination file is overwritten if /// necessary. -pub fn copy_file(src: &str, dst: &str) -> Result<()> { +pub fn copy_file(src: &str, dst: &str) -> io::Result<()> { let _dst = prepare_path(dst)?; fs::copy(src, _dst)?; Ok(()) @@ -22,13 +21,13 @@ pub fn copy_file(src: &str, dst: &str) -> Result<()> { /// Tildes are expanded if present and the destination file is overwritten if /// necessary. On non-Unix platforms, a hard link will be created instead. #[cfg(target_family = "unix")] -pub fn link_file(src: &str, dst: &str) -> Result<()> { +pub fn link_file(src: &str, dst: &str) -> io::Result<()> { let _dst = prepare_path(dst)?; symlink(fs::canonicalize(src)?, _dst)?; Ok(()) } #[cfg(not(target_family = "unix"))] -pub fn link_file(src: &str, dst: &str) -> Result<()> { +pub fn link_file(src: &str, dst: &str) -> io::Result<()> { let _dst = prepare_path(dst)?; fs::hard_link(src, _dst)?; Ok(()) @@ -36,7 +35,7 @@ pub fn link_file(src: &str, dst: &str) -> Result<()> { /// Create the parent directories of a path and return the path with tildes /// expanded. -fn prepare_path(path: &str) -> Result<PathBuf> { +fn prepare_path(path: &str) -> io::Result<PathBuf> { let _dst: PathBuf = (&tilde(path).to_mut()).into(); if let Some(_path) = _dst.parent() { fs::create_dir_all(_path)?; @@ -51,20 +50,28 @@ fn prepare_path(path: &str) -> Result<PathBuf> { /// Execute a local shell script, optionally with a command prefix or postfix. /// /// Uses sh on Unix and PowerShell on Windows. -pub fn run_script(path: &str, prefix: &str, postfix: &str) -> Result<()> { +pub fn run_script(path: &str, prefix: &str, postfix: &str) -> Result<(), String> +{ // Use absolute() to avoid incompatible "UNC" paths on Windows: // https://github.com/rust-lang/rust/issues/42869 - let _path = absolute(path)?; + let _path = absolute(path).map_err(|why| why.to_string())?; + let status; if cfg!(target_family = "unix") { - Command::new("sh") + status = Command::new("sh") .arg("-c") .arg(format!("{} {} {}", prefix, _path.display(), postfix)) - .status()?; + .status() + .map_err(|why| why.to_string())?; } else { - Command::new("powershell") + status = Command::new("powershell") .args(["-ExecutionPolicy", "Bypass", "-Command"]) .arg(format!("{} {} {}", prefix, _path.display(), postfix)) - .status()?; + .status() + .map_err(|why| why.to_string())?; + } + if status.success() { + Ok(()) + } else { + Err(format!("Process exited with {status}")) } - Ok(()) }