[O] Encapsulate run_cmd function
This commit is contained in:
@@ -1,9 +1,8 @@
|
|||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::process::Command;
|
use anyhow::{Result};
|
||||||
use anyhow::{bail, Result};
|
use crate::utils::run_cmd;
|
||||||
use shlex::Shlex;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Thumbnailer {
|
pub struct Thumbnailer {
|
||||||
@@ -45,19 +44,11 @@ impl Thumbnailer {
|
|||||||
|
|
||||||
/// Generate thumbnail
|
/// Generate thumbnail
|
||||||
pub fn gen(&self, orig: &str, new: &str, pixels: i32) -> Result<()> {
|
pub fn gen(&self, orig: &str, new: &str, pixels: i32) -> Result<()> {
|
||||||
let cmd = self.exec
|
run_cmd(&*self.exec
|
||||||
.replace("%s", &*format!("'{pixels}'"))
|
.replace("%s", &*format!("'{pixels}'"))
|
||||||
.replace("%u", &shlex::quote(orig))
|
.replace("%u", &shlex::quote(orig))
|
||||||
.replace("%i", &shlex::quote(orig))
|
.replace("%i", &shlex::quote(orig))
|
||||||
.replace("%o", &shlex::quote(new));
|
.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);
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
use std::{fs, io};
|
use std::{fs, io};
|
||||||
use std::path::{PathBuf};
|
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<()> {
|
pub fn write_sf<C: AsRef<[u8]>>(path: &PathBuf, contents: C) -> io::Result<()> {
|
||||||
// Create parent if it has parent
|
// 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)
|
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)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user