Refactor Result handling.

This commit is contained in:
R-VdP 2023-03-14 16:40:01 +01:00
parent 95f9897766
commit 16b3a09558
No known key found for this signature in database
3 changed files with 18 additions and 13 deletions

View file

@ -34,15 +34,15 @@ fn install_nix_profile(
profile_name: &Path, profile_name: &Path,
) -> Result<process::ExitStatus> { ) -> Result<process::ExitStatus> {
DirBuilder::new().recursive(true).create(profile_dir)?; DirBuilder::new().recursive(true).create(profile_dir)?;
process::Command::new("nix-env") let status = process::Command::new("nix-env")
.arg("--profile") .arg("--profile")
.arg(profile_dir.join(profile_name)) .arg(profile_dir.join(profile_name))
.arg("--set") .arg("--set")
.arg(&store_path.store_path) .arg(&store_path.store_path)
.stdout(process::Stdio::inherit()) .stdout(process::Stdio::inherit())
.stderr(process::Stdio::inherit()) .stderr(process::Stdio::inherit())
.status() .status()?;
.map_err(anyhow::Error::from) Ok(status)
} }
fn create_gcroot(gcroot_path: &str, profile_path: &Path) -> Result<()> { fn create_gcroot(gcroot_path: &str, profile_path: &Path) -> Result<()> {
@ -92,7 +92,7 @@ fn parse_nix_build_output(output: String) -> Result<StorePath> {
} }
fn run_nix_build(flake_uri: &str, flake_attr: &str) -> Result<process::Output> { fn run_nix_build(flake_uri: &str, flake_attr: &str) -> Result<process::Output> {
process::Command::new("nix") let output = process::Command::new("nix")
.arg("build") .arg("build")
.arg(format!("{flake_uri}#{flake_attr}")) .arg(format!("{flake_uri}#{flake_attr}"))
.arg("--json") .arg("--json")
@ -100,6 +100,6 @@ fn run_nix_build(flake_uri: &str, flake_attr: &str) -> Result<process::Output> {
// so we inherit and output stderr directly to the terminal, but we // so we inherit and output stderr directly to the terminal, but we
// capture stdout as the result of this call // capture stdout as the result of this call
.stderr(process::Stdio::inherit()) .stderr(process::Stdio::inherit())
.output() .output()?;
.map_err(anyhow::Error::from) Ok(output)
} }

View file

@ -66,13 +66,15 @@ fn create_link(to: &Path, from: &Path) -> Result<()> {
anyhow::bail!("File exists and is no link!"); anyhow::bail!("File exists and is no link!");
} }
} }
unix::fs::symlink(to, from).map_err(anyhow::Error::from) unix::fs::symlink(to, from)?;
Ok(())
} }
fn remove_link(from: &Path) -> Result<()> { fn remove_link(from: &Path) -> Result<()> {
log::info!("Removing symlink: {}", from.display()); log::info!("Removing symlink: {}", from.display());
if from.is_symlink() { if from.is_symlink() {
fs::remove_file(from).map_err(anyhow::Error::from) fs::remove_file(from)?;
Ok(())
} else { } else {
anyhow::bail!("Not a symlink!"); anyhow::bail!("Not a symlink!");
} }
@ -80,12 +82,14 @@ fn remove_link(from: &Path) -> Result<()> {
fn remove_file(from: &Path) -> Result<()> { fn remove_file(from: &Path) -> Result<()> {
log::info!("Removing file: {}", from.display()); log::info!("Removing file: {}", from.display());
fs::remove_file(from).map_err(anyhow::Error::from) fs::remove_file(from)?;
Ok(())
} }
fn remove_dir(from: &Path) -> Result<()> { fn remove_dir(from: &Path) -> Result<()> {
log::info!("Removing directory: {}", from.display()); log::info!("Removing directory: {}", from.display());
fs::remove_dir(from).map_err(anyhow::Error::from) fs::remove_dir(from)?;
Ok(())
} }
pub fn compose<A, B, C, G, F>(f: F, g: G) -> impl Fn(A) -> C pub fn compose<A, B, C, G, F>(f: F, g: G) -> impl Fn(A) -> C

View file

@ -226,11 +226,12 @@ fn invoke_remote_script(
if use_remote_sudo { if use_remote_sudo {
cmd.arg("sudo"); cmd.arg("sudo");
} }
cmd.arg(format!("{store_path}/bin/{script_name}")) let status = cmd
.arg(format!("{store_path}/bin/{script_name}"))
.stdout(process::Stdio::inherit()) .stdout(process::Stdio::inherit())
.stderr(process::Stdio::inherit()) .stderr(process::Stdio::inherit())
.status() .status()?;
.map_err(anyhow::Error::from) Ok(status)
} }
fn check_root() -> Result<()> { fn check_root() -> Result<()> {