[O] Encapsulate run_cmd function

This commit is contained in:
Azalea Gui
2023-02-24 10:44:35 -05:00
parent 4f17cc7d4f
commit 18a7018a83
2 changed files with 19 additions and 13 deletions
+4 -13
View File
@@ -1,9 +1,8 @@
use std::collections::HashSet;
use std::fs;
use std::path::Path;
use std::process::Command;
use anyhow::{bail, Result};
use shlex::Shlex;
use anyhow::{Result};
use crate::utils::run_cmd;
#[derive(Debug)]
pub struct Thumbnailer {
@@ -45,19 +44,11 @@ impl Thumbnailer {
/// Generate thumbnail
pub fn gen(&self, orig: &str, new: &str, pixels: i32) -> Result<()> {
let cmd = self.exec
run_cmd(&*self.exec
.replace("%s", &*format!("'{pixels}'"))
.replace("%u", &shlex::quote(orig))
.replace("%i", &shlex::quote(orig))
.replace("%o", &shlex::quote(new));
let args: Vec<String> = Shlex::new(&*cmd).collect();
let out = Command::new(args[0].to_owned()).args(&args[1..]).output()?;
if !out.status.success() {
error!("Command failed: {cmd}");
error!("Command output: {:?}", out);
bail!(String::from_utf8(out.stderr)?);
}
debug!("Command output: {:?}", out);
.replace("%o", &shlex::quote(new)))?;
Ok(())
}
+15
View File
@@ -1,5 +1,8 @@
use std::{fs, io};
use std::path::{PathBuf};
use std::process::{Command, Output};
use anyhow::{Result, bail};
use shlex::Shlex;
pub fn write_sf<C: AsRef<[u8]>>(path: &PathBuf, contents: C) -> io::Result<()> {
// Create parent if it has parent
@@ -9,3 +12,15 @@ pub fn write_sf<C: AsRef<[u8]>>(path: &PathBuf, contents: C) -> io::Result<()> {
fs::write(path, contents)
}
pub fn run_cmd(cmd: &str) -> Result<Output> {
let args: Vec<String> = Shlex::new(&*cmd).collect();
let out = Command::new(args[0].to_owned()).args(&args[1..]).output()?;
if !out.status.success() {
error!("Command failed: {cmd}");
error!("Command output: {:?}", out);
bail!(String::from_utf8(out.stderr)?);
}
debug!("Command output: {:?}", out);
Ok(out)
}