Tidy command execution
This commit is contained in:
@@ -6,7 +6,12 @@ pub trait Filter {
|
|||||||
fn filter(&self, reader: Box<dyn Read>) -> Box<dyn Read>;
|
fn filter(&self, reader: Box<dyn Read>) -> Box<dyn Read>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn exec_command(command: &Vec<&str>) -> Result<String, String> {
|
// TODO: Make this support quoting for args with spaces (and escape nested quotes)
|
||||||
|
fn display_command(command: &Vec<&str>) -> String {
|
||||||
|
command.join(" ")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn exec(command: &Vec<&str>) -> Result<String, String> {
|
||||||
if command.is_empty() {
|
if command.is_empty() {
|
||||||
return Err("Command is empty".to_string());
|
return Err("Command is empty".to_string());
|
||||||
}
|
}
|
||||||
@@ -14,11 +19,13 @@ pub fn exec_command(command: &Vec<&str>) -> Result<String, String> {
|
|||||||
if command.len() > 1 {
|
if command.len() > 1 {
|
||||||
cmd.args(&command[1..]);
|
cmd.args(&command[1..]);
|
||||||
}
|
}
|
||||||
let output = cmd.output().map_err(|e| e.to_string())?;
|
let output = cmd
|
||||||
|
.output()
|
||||||
|
.map_err(|e| format!("Failed to run command {}: {}", command[0], e))?;
|
||||||
if !output.status.success() {
|
if !output.status.success() {
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
"Command {:?} failed with status {}: {}",
|
"Command '{}' failed with status {}: {}",
|
||||||
command,
|
display_command(command),
|
||||||
output.status,
|
output.status,
|
||||||
String::from_utf8_lossy(&output.stderr)
|
String::from_utf8_lossy(&output.stderr)
|
||||||
));
|
));
|
||||||
@@ -27,7 +34,10 @@ pub fn exec_command(command: &Vec<&str>) -> Result<String, String> {
|
|||||||
Ok(output_str)
|
Ok(output_str)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn exec_piped_commands(
|
/// Executes a pipeline of commands, where the output of the source command is passed through a
|
||||||
|
/// series of filters before being piped into the destination command. The filters are applied in
|
||||||
|
/// the order they are provided.
|
||||||
|
pub fn exec_pipe(
|
||||||
source: &Vec<&str>,
|
source: &Vec<&str>,
|
||||||
dest: &Vec<&str>,
|
dest: &Vec<&str>,
|
||||||
filters: Vec<Box<dyn Filter>>,
|
filters: Vec<Box<dyn Filter>>,
|
||||||
|
|||||||
16
src/job.rs
16
src/job.rs
@@ -57,7 +57,7 @@ impl Job {
|
|||||||
let name = snapshot.to_string();
|
let name = snapshot.to_string();
|
||||||
let mut cmd = self.get_side_command(side);
|
let mut cmd = self.get_side_command(side);
|
||||||
cmd.extend(["snapshot", &name]);
|
cmd.extend(["snapshot", &name]);
|
||||||
command::exec_command(&cmd)?;
|
command::exec(&cmd)?;
|
||||||
self.send_event(BackupEvent::SnapshotCreated(name.clone()));
|
self.send_event(BackupEvent::SnapshotCreated(name.clone()));
|
||||||
Ok(snapshot)
|
Ok(snapshot)
|
||||||
}
|
}
|
||||||
@@ -70,7 +70,7 @@ impl Job {
|
|||||||
}
|
}
|
||||||
cmd.extend(["-n", "-P"]);
|
cmd.extend(["-n", "-P"]);
|
||||||
cmd.push(source);
|
cmd.push(source);
|
||||||
let output = command::exec_command(&cmd)?;
|
let output = command::exec(&cmd)?;
|
||||||
let size = output
|
let size = output
|
||||||
.lines()
|
.lines()
|
||||||
.last()
|
.last()
|
||||||
@@ -107,7 +107,7 @@ impl Job {
|
|||||||
))],
|
))],
|
||||||
None => vec![],
|
None => vec![],
|
||||||
};
|
};
|
||||||
command::exec_piped_commands(&send_cmd, &receive_cmd, filters)?;
|
command::exec_pipe(&send_cmd, &receive_cmd, filters)?;
|
||||||
self.send_event(BackupEvent::DatasetCompleted(source.to_string()));
|
self.send_event(BackupEvent::DatasetCompleted(source.to_string()));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -115,7 +115,7 @@ impl Job {
|
|||||||
fn list_snapshot_ids(&self, source: &str, side: JobSide) -> Result<Vec<String>, String> {
|
fn list_snapshot_ids(&self, source: &str, side: JobSide) -> Result<Vec<String>, String> {
|
||||||
let mut cmd = self.get_side_command(side);
|
let mut cmd = self.get_side_command(side);
|
||||||
cmd.extend(["list", "-H", "-o", "name", "-t", "snapshot", source]);
|
cmd.extend(["list", "-H", "-o", "name", "-t", "snapshot", source]);
|
||||||
let output = command::exec_command(&cmd)?;
|
let output = command::exec(&cmd)?;
|
||||||
let snapshots: Vec<&str> = output
|
let snapshots: Vec<&str> = output
|
||||||
.split_whitespace()
|
.split_whitespace()
|
||||||
.map(|s| {
|
.map(|s| {
|
||||||
@@ -195,7 +195,7 @@ impl Job {
|
|||||||
let mut cmd = self.get_side_command(snapshot.side);
|
let mut cmd = self.get_side_command(snapshot.side);
|
||||||
let name = snapshot.to_string();
|
let name = snapshot.to_string();
|
||||||
cmd.extend(["destroy", &name]);
|
cmd.extend(["destroy", &name]);
|
||||||
command::exec_command(&cmd)?;
|
command::exec(&cmd)?;
|
||||||
self.send_event(BackupEvent::SnapshotDeleted(name.clone()));
|
self.send_event(BackupEvent::SnapshotDeleted(name.clone()));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -205,7 +205,7 @@ impl Job {
|
|||||||
// Check the source exists
|
// Check the source exists
|
||||||
let mut cmd = self.get_side_command(JobSide::Source);
|
let mut cmd = self.get_side_command(JobSide::Source);
|
||||||
cmd.extend(["list", "-H", "-o", "name", source]);
|
cmd.extend(["list", "-H", "-o", "name", source]);
|
||||||
let _ = command::exec_command(&cmd)?;
|
let _ = command::exec(&cmd)?;
|
||||||
|
|
||||||
// Check whether the destination exists
|
// Check whether the destination exists
|
||||||
// TODO: This will assume the destination doesn't exist if the
|
// TODO: This will assume the destination doesn't exist if the
|
||||||
@@ -213,7 +213,7 @@ impl Job {
|
|||||||
let dest = format!("{}/{}", self.target, source);
|
let dest = format!("{}/{}", self.target, source);
|
||||||
cmd = self.get_side_command(JobSide::Target);
|
cmd = self.get_side_command(JobSide::Target);
|
||||||
cmd.extend(["list", "-H", "-o", "name", &dest]);
|
cmd.extend(["list", "-H", "-o", "name", &dest]);
|
||||||
let dest_exists = command::exec_command(&cmd).is_ok();
|
let dest_exists = command::exec(&cmd).is_ok();
|
||||||
|
|
||||||
// Run backup
|
// Run backup
|
||||||
if dest_exists {
|
if dest_exists {
|
||||||
@@ -375,7 +375,7 @@ impl JobBuilder {
|
|||||||
|
|
||||||
let mut cmd: Vec<&str> = self.source_zfs_command.iter().map(|s| s.as_str()).collect();
|
let mut cmd: Vec<&str> = self.source_zfs_command.iter().map(|s| s.as_str()).collect();
|
||||||
cmd.extend(args);
|
cmd.extend(args);
|
||||||
let output = command::exec_command(&cmd)?;
|
let output = command::exec(&cmd)?;
|
||||||
datasets.extend(output.lines().map(str::to_string));
|
datasets.extend(output.lines().map(str::to_string));
|
||||||
}
|
}
|
||||||
if datasets.is_empty() {
|
if datasets.is_empty() {
|
||||||
|
|||||||
Reference in New Issue
Block a user