use crate::command_runner::CommandRunner; use crate::symbols::Symbol; use std::error::Error; #[derive(Debug)] pub struct Cron<'r, C, U, R> { user: U, content: C, command_runner: &'r R, } impl<'r, U, R> Cron<'r, String, U, R> { pub fn new>(user: U, content: C, command_runner: &'r R) -> Self { Self { user, content: String::from(content.as_ref()) + "\n", command_runner, } } } impl, U: AsRef, R: CommandRunner> Symbol for Cron<'_, C, U, R> { fn target_reached(&self) -> Result> { 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> { 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(()) } }