-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(commands): Add option stdin_command to be used in CLI and config…
… file (#266) Adds the option `stdin_command` to `BackupOptions`. This options starts the given command and reads its output as stdin if `-` is specified as backup source. see rustic-rs/rustic#662 --------- Co-authored-by: simonsan <[email protected]>
- Loading branch information
Showing
9 changed files
with
277 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use std::{ | ||
iter::{once, Once}, | ||
path::PathBuf, | ||
process::{Child, ChildStdout, Command, Stdio}, | ||
sync::Mutex, | ||
}; | ||
|
||
use crate::{ | ||
backend::{ReadSource, ReadSourceEntry}, | ||
error::{RepositoryErrorKind, RusticResult}, | ||
CommandInput, | ||
}; | ||
|
||
/// The `ChildStdoutSource` is a `ReadSource` when spawning a child process and reading its stdout | ||
#[derive(Debug)] | ||
pub struct ChildStdoutSource { | ||
/// The path of the stdin entry. | ||
path: PathBuf, | ||
/// The child process | ||
/// | ||
/// # Note | ||
/// | ||
/// This is in a Mutex as we want to take out `ChildStdout` | ||
/// in the `entries` method - but this method only gets a | ||
/// reference of self. | ||
process: Mutex<Child>, | ||
/// the command which is called | ||
command: CommandInput, | ||
} | ||
|
||
impl ChildStdoutSource { | ||
/// Creates a new `ChildSource`. | ||
pub fn new(cmd: &CommandInput, path: PathBuf) -> RusticResult<Self> { | ||
let process = Command::new(cmd.command()) | ||
.args(cmd.args()) | ||
.stdout(Stdio::piped()) | ||
.spawn() | ||
.map_err(|err| { | ||
RepositoryErrorKind::CommandExecutionFailed( | ||
"stdin-command".into(), | ||
"call".into(), | ||
err, | ||
) | ||
.into() | ||
}); | ||
|
||
let process = cmd.on_failure().display_result(process)?; | ||
|
||
Ok(Self { | ||
path, | ||
process: Mutex::new(process), | ||
command: cmd.clone(), | ||
}) | ||
} | ||
|
||
/// Finishes the `ChildSource` | ||
pub fn finish(self) -> RusticResult<()> { | ||
let status = self.process.lock().unwrap().wait(); | ||
self.command | ||
.on_failure() | ||
.handle_status(status, "stdin-command", "call")?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl ReadSource for ChildStdoutSource { | ||
type Open = ChildStdout; | ||
type Iter = Once<RusticResult<ReadSourceEntry<ChildStdout>>>; | ||
|
||
fn size(&self) -> RusticResult<Option<u64>> { | ||
Ok(None) | ||
} | ||
|
||
fn entries(&self) -> Self::Iter { | ||
let open = self.process.lock().unwrap().stdout.take(); | ||
once(ReadSourceEntry::from_path(self.path.clone(), open)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.