mirror of
https://github.com/robbert-vdh/yabridge.git
synced 2026-05-09 20:29:10 +02:00
Wrap filesystem functions for readable errors
Without having to repeat this all over the place.
This commit is contained in:
@@ -18,13 +18,12 @@
|
|||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use std::fs;
|
|
||||||
use std::os::unix::fs::symlink;
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use crate::config::{Config, InstallationMethod};
|
use crate::config::{Config, InstallationMethod};
|
||||||
use crate::files;
|
use crate::files;
|
||||||
use crate::files::FoundFile;
|
use crate::files::FoundFile;
|
||||||
|
use crate::utils;
|
||||||
use crate::utils::{verify_path_setup, wrap};
|
use crate::utils::{verify_path_setup, wrap};
|
||||||
|
|
||||||
/// Add a direcotry to the plugin locations. Duplicates get ignord because we're using ordered sets.
|
/// 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" => {
|
Ok(Some(answer)) if answer == "YES" => {
|
||||||
for file in &orphan_files {
|
for file in &orphan_files {
|
||||||
fs::remove_file(file.path())
|
utils::remove_file(file.path())?;
|
||||||
.with_context(|| format!("Could not remove '{}'", file.path().display()))?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("\nRemoved {} files", orphan_files.len());
|
println!("\nRemoved {} files", orphan_files.len());
|
||||||
@@ -177,28 +175,15 @@ pub fn do_sync(config: &Config, options: &SyncOptions) -> Result<()> {
|
|||||||
// mixing symlinks and regular files
|
// mixing symlinks and regular files
|
||||||
let target_path = plugin.with_extension("so");
|
let target_path = plugin.with_extension("so");
|
||||||
if target_path.exists() {
|
if target_path.exists() {
|
||||||
fs::remove_file(&target_path)
|
utils::remove_file(&target_path)?;
|
||||||
.with_context(|| format!("Could not remove '{}'", target_path.display()))?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
match config.method {
|
match config.method {
|
||||||
InstallationMethod::Copy => {
|
InstallationMethod::Copy => {
|
||||||
fs::copy(&libyabridge_path, &target_path).with_context(|| {
|
utils::copy(&libyabridge_path, &target_path)?;
|
||||||
format!(
|
|
||||||
"Error copying '{}' to '{}'",
|
|
||||||
libyabridge_path.display(),
|
|
||||||
target_path.display()
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
}
|
}
|
||||||
InstallationMethod::Symlink => {
|
InstallationMethod::Symlink => {
|
||||||
symlink(&libyabridge_path, &target_path).with_context(|| {
|
utils::symlink(&libyabridge_path, &target_path)?;
|
||||||
format!(
|
|
||||||
"Error symlinking '{}' to '{}'",
|
|
||||||
libyabridge_path.display(),
|
|
||||||
target_path.display()
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,8 +223,7 @@ pub fn do_sync(config: &Config, options: &SyncOptions) -> Result<()> {
|
|||||||
|
|
||||||
println!("- {}", path.display());
|
println!("- {}", path.display());
|
||||||
if options.prune {
|
if options.prune {
|
||||||
fs::remove_file(path)
|
utils::remove_file(path)?;
|
||||||
.with_context(|| format!("Could not remove '{}'", path.display()))?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,13 +16,44 @@
|
|||||||
|
|
||||||
//! Small helper utilities.
|
//! Small helper utilities.
|
||||||
|
|
||||||
|
use anyhow::{Context, Result};
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use std::fs;
|
||||||
|
use std::os::unix::fs as unix_fs;
|
||||||
use std::os::unix::process::CommandExt;
|
use std::os::unix::process::CommandExt;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::process::{Command, Stdio};
|
use std::process::{Command, Stdio};
|
||||||
use textwrap::Wrapper;
|
use textwrap::Wrapper;
|
||||||
|
|
||||||
|
/// Wrapper around `std::fs::copy` with a human readable error message.
|
||||||
|
pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<u64> {
|
||||||
|
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<P: AsRef<Path>>(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<P: AsRef<Path>, Q: AsRef<Path>>(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
|
/// 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
|
/// 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
|
/// don't know how to invoke the shell as a login shell. This is needed when using copies to ensure
|
||||||
|
|||||||
Reference in New Issue
Block a user