A library for writing host-specific, single-binary configuration management and deployment tools
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.

46 lines
1.1 KiB

use crate::command_runner::{is_success, CommandRunner};
use crate::symbols::Symbol;
use async_trait::async_trait;
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<C: AsRef<str>>(user: U, content: C, command_runner: &'r R) -> Self {
Self {
user,
content: String::from(content.as_ref()) + "\n",
command_runner,
}
}
}
#[async_trait(?Send)]
impl<C: AsRef<str>, U: AsRef<str>, R: CommandRunner> Symbol for Cron<'_, C, U, R> {
async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
let tab = self
.command_runner
.get_output("crontab", args!["-l", "-u", self.user.as_ref()])
.await?;
Ok(tab == self.content.as_ref().as_bytes())
}
async fn execute(&self) -> Result<(), Box<dyn Error>> {
is_success(
self
.command_runner
.run(
"crontab",
args!["-u", self.user.as_ref(), "-",],
self.content.as_ref(),
)
.await,
)?;
Ok(())
}
}