From 86be5c4814815b1b5c56045b2ef373fa3b04c939 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sun, 19 Jul 2020 13:46:40 +0200 Subject: [PATCH] Wrap filesystem functions for readable errors Without having to repeat this all over the place. --- tools/yabridgectl/src/actions.rs | 28 ++++++---------------------- tools/yabridgectl/src/utils.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/tools/yabridgectl/src/actions.rs b/tools/yabridgectl/src/actions.rs index 38b79ac7..03bb7c92 100644 --- a/tools/yabridgectl/src/actions.rs +++ b/tools/yabridgectl/src/actions.rs @@ -18,13 +18,12 @@ use anyhow::{Context, Result}; use colored::Colorize; -use std::fs; -use std::os::unix::fs::symlink; use std::path::{Path, PathBuf}; use crate::config::{Config, InstallationMethod}; use crate::files; use crate::files::FoundFile; +use crate::utils; use crate::utils::{verify_path_setup, wrap}; /// Add a direcotry to the plugin locations. Duplicates get ignord because we're using ordered sets. @@ -60,8 +59,7 @@ pub fn remove_directory(config: &mut Config, path: &Path) -> Result<()> { ) { Ok(Some(answer)) if answer == "YES" => { for file in &orphan_files { - fs::remove_file(file.path()) - .with_context(|| format!("Could not remove '{}'", file.path().display()))?; + utils::remove_file(file.path())?; } println!("\nRemoved {} files", orphan_files.len()); @@ -177,28 +175,15 @@ pub fn do_sync(config: &Config, options: &SyncOptions) -> Result<()> { // mixing symlinks and regular files let target_path = plugin.with_extension("so"); if target_path.exists() { - fs::remove_file(&target_path) - .with_context(|| format!("Could not remove '{}'", target_path.display()))?; + utils::remove_file(&target_path)?; } match config.method { InstallationMethod::Copy => { - fs::copy(&libyabridge_path, &target_path).with_context(|| { - format!( - "Error copying '{}' to '{}'", - libyabridge_path.display(), - target_path.display() - ) - })?; + utils::copy(&libyabridge_path, &target_path)?; } InstallationMethod::Symlink => { - symlink(&libyabridge_path, &target_path).with_context(|| { - format!( - "Error symlinking '{}' to '{}'", - libyabridge_path.display(), - target_path.display() - ) - })?; + utils::symlink(&libyabridge_path, &target_path)?; } } @@ -238,8 +223,7 @@ pub fn do_sync(config: &Config, options: &SyncOptions) -> Result<()> { println!("- {}", path.display()); if options.prune { - fs::remove_file(path) - .with_context(|| format!("Could not remove '{}'", path.display()))?; + utils::remove_file(path)?; } } diff --git a/tools/yabridgectl/src/utils.rs b/tools/yabridgectl/src/utils.rs index d7a848bf..ba35bb5a 100644 --- a/tools/yabridgectl/src/utils.rs +++ b/tools/yabridgectl/src/utils.rs @@ -16,13 +16,44 @@ //! Small helper utilities. +use anyhow::{Context, Result}; use colored::Colorize; use std::env; +use std::fs; +use std::os::unix::fs as unix_fs; use std::os::unix::process::CommandExt; use std::path::Path; use std::process::{Command, Stdio}; use textwrap::Wrapper; +/// Wrapper around `std::fs::copy` with a human readable error message. +pub fn copy, Q: AsRef>(from: P, to: Q) -> Result { + fs::copy(&from, &to).with_context(|| { + format!( + "Error copying '{}' to '{}'", + from.as_ref().display(), + to.as_ref().display() + ) + }) +} + +/// Wrapper around `std::fs::remove_file` with a human readable error message. +pub fn remove_file>(path: P) -> Result<()> { + fs::remove_file(&path) + .with_context(|| format!("Could not remove '{}'", path.as_ref().display())) +} + +/// Wrapper around `std::os::unix::fs::symlink` with a human readable error message. +pub fn symlink, Q: AsRef>(src: P, dst: Q) -> Result<()> { + unix_fs::symlink(&src, &dst).with_context(|| { + format!( + "Error symlinking '{}' to '{}'", + src.as_ref().display(), + dst.as_ref().display() + ) + }) +} + /// Verify that `yabridge-host.exe` is accessible in a login shell. Returns unit if it is, or if we /// the login shell is set to an unknown shell. In the last case we'll just print a warning since we /// don't know how to invoke the shell as a login shell. This is needed when using copies to ensure