You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.7 KiB
60 lines
1.7 KiB
use std::error::Error;
|
|
use std::fmt;
|
|
|
|
use crate::command_runner::CommandRunner;
|
|
|
|
use crate::symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
|
|
|
|
pub struct Cron<'r, C: AsRef<str>, U: AsRef<str>, R: CommandRunner> {
|
|
user: U,
|
|
content: C,
|
|
command_runner: &'r R,
|
|
}
|
|
|
|
impl<'r, U: AsRef<str>, R: CommandRunner> Cron<'r, String, U, R> {
|
|
pub fn new<C: AsRef<str>>(user: U, content: C, command_runner: &'r R) -> Self {
|
|
Cron {
|
|
user,
|
|
content: String::from(content.as_ref()) + "\n",
|
|
command_runner,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<C: AsRef<str>, U: AsRef<str>, R: CommandRunner> Symbol for Cron<'_, C, U, R> {
|
|
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
|
|
let tab = self
|
|
.command_runner
|
|
.get_output("crontab", args!["-l", "-u", self.user.as_ref()])?;
|
|
Ok(tab == self.content.as_ref().as_bytes())
|
|
}
|
|
|
|
fn execute(&self) -> Result<(), Box<dyn Error>> {
|
|
let output = self.command_runner.run_with_args_and_stdin(
|
|
"crontab",
|
|
args!["-u", self.user.as_ref(), "-",],
|
|
self.content.as_ref(),
|
|
)?;
|
|
if !output.status.success() {
|
|
return Err(String::from_utf8(output.stderr)?.into());
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn as_action<'a>(&'a self, runner: &'a dyn SymbolRunner) -> Box<dyn Action + 'a> {
|
|
Box::new(SymbolAction::new(runner, self))
|
|
}
|
|
|
|
fn into_action<'a>(self: Box<Self>, runner: &'a dyn SymbolRunner) -> Box<dyn Action + 'a>
|
|
where
|
|
Self: 'a,
|
|
{
|
|
Box::new(OwnedSymbolAction::new(runner, *self))
|
|
}
|
|
}
|
|
|
|
impl<C: AsRef<str>, U: AsRef<str>, R: CommandRunner> fmt::Display for Cron<'_, C, U, R> {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
|
write!(f, "Cron {}", self.user.as_ref())
|
|
}
|
|
}
|