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.
80 lines
1.9 KiB
80 lines
1.9 KiB
use std::error::Error;
|
|
use std::fmt;
|
|
use std::ops::Deref;
|
|
|
|
use command_runner::CommandRunner;
|
|
use resources::Resource;
|
|
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
|
|
|
|
pub struct Cron<'r, C, U, R: 'r + CommandRunner>
|
|
where
|
|
C: Deref<Target = str>,
|
|
U: Deref<Target = str>,
|
|
{
|
|
user: U,
|
|
content: C,
|
|
command_runner: &'r R,
|
|
}
|
|
|
|
impl<'r, U, R: 'r + CommandRunner> Cron<'r, String, U, R>
|
|
where
|
|
U: Deref<Target = str>,
|
|
{
|
|
pub fn new<C: Deref<Target = str>>(user: U, content: C, command_runner: &'r R) -> Self {
|
|
Cron {
|
|
user,
|
|
content: String::from(&*content) + "\n",
|
|
command_runner,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'r, C, U, R: 'r + CommandRunner> Symbol for Cron<'r, C, U, R>
|
|
where
|
|
C: Deref<Target = str>,
|
|
U: Deref<Target = str>,
|
|
{
|
|
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
|
|
let tab = self
|
|
.command_runner
|
|
.get_output("crontab", &["-l", "-u", &self.user])?;
|
|
Ok(tab == self.content.bytes().collect::<Vec<u8>>())
|
|
}
|
|
|
|
fn execute(&self) -> Result<(), Box<dyn Error>> {
|
|
let output = self.command_runner.run_with_args_and_stdin(
|
|
"crontab",
|
|
&["-u", &self.user, "-"],
|
|
&self.content,
|
|
)?;
|
|
if !output.status.success() {
|
|
return Err(String::from_utf8(output.stderr)?.into());
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn get_prerequisites(&self) -> Vec<Resource> {
|
|
vec![]
|
|
}
|
|
|
|
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<'r, C, U, R: 'r + CommandRunner> fmt::Display for Cron<'r, C, U, R>
|
|
where
|
|
C: Deref<Target = str>,
|
|
U: Deref<Target = str>,
|
|
{
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
write!(f, "Cron {}", &*self.user)
|
|
}
|
|
}
|