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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. use crate::command_runner::{is_success, CommandRunner};
  2. use crate::symbols::Symbol;
  3. use async_trait::async_trait;
  4. use std::error::Error;
  5. #[derive(Debug)]
  6. pub struct Cron<'r, C, U, R> {
  7. user: U,
  8. content: C,
  9. command_runner: &'r R,
  10. }
  11. impl<'r, U, R> Cron<'r, String, U, R> {
  12. pub fn new<C: AsRef<str>>(user: U, content: C, command_runner: &'r R) -> Self {
  13. Self {
  14. user,
  15. content: String::from(content.as_ref()) + "\n",
  16. command_runner,
  17. }
  18. }
  19. }
  20. #[async_trait(?Send)]
  21. impl<C: AsRef<str>, U: AsRef<str>, R: CommandRunner> Symbol for Cron<'_, C, U, R> {
  22. async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  23. let tab = self
  24. .command_runner
  25. .get_output("crontab", args!["-l", "-u", self.user.as_ref()])
  26. .await?;
  27. Ok(tab == self.content.as_ref().as_bytes())
  28. }
  29. async fn execute(&self) -> Result<(), Box<dyn Error>> {
  30. is_success(
  31. self
  32. .command_runner
  33. .run(
  34. "crontab",
  35. args!["-u", self.user.as_ref(), "-",],
  36. self.content.as_ref(),
  37. )
  38. .await,
  39. )?;
  40. Ok(())
  41. }
  42. }