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.

60 lines
1.7 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
5 years ago
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 std::error::Error;
  2. use std::fmt;
  3. use crate::command_runner::CommandRunner;
  4. use crate::symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  5. pub struct Cron<'r, C: AsRef<str>, U: AsRef<str>, R: CommandRunner> {
  6. user: U,
  7. content: C,
  8. command_runner: &'r R,
  9. }
  10. impl<'r, U: AsRef<str>, R: CommandRunner> Cron<'r, String, U, R> {
  11. pub fn new<C: AsRef<str>>(user: U, content: C, command_runner: &'r R) -> Self {
  12. Cron {
  13. user,
  14. content: String::from(content.as_ref()) + "\n",
  15. command_runner,
  16. }
  17. }
  18. }
  19. impl<C: AsRef<str>, U: AsRef<str>, R: CommandRunner> Symbol for Cron<'_, C, U, R> {
  20. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  21. let tab = self
  22. .command_runner
  23. .get_output("crontab", args!["-l", "-u", self.user.as_ref()])?;
  24. Ok(tab == self.content.as_ref().as_bytes())
  25. }
  26. fn execute(&self) -> Result<(), Box<dyn Error>> {
  27. let output = self.command_runner.run_with_args_and_stdin(
  28. "crontab",
  29. args!["-u", self.user.as_ref(), "-",],
  30. self.content.as_ref(),
  31. )?;
  32. if !output.status.success() {
  33. return Err(String::from_utf8(output.stderr)?.into());
  34. }
  35. Ok(())
  36. }
  37. fn as_action<'a>(&'a self, runner: &'a dyn SymbolRunner) -> Box<dyn Action + 'a> {
  38. Box::new(SymbolAction::new(runner, self))
  39. }
  40. fn into_action<'a>(self: Box<Self>, runner: &'a dyn SymbolRunner) -> Box<dyn Action + 'a>
  41. where
  42. Self: 'a,
  43. {
  44. Box::new(OwnedSymbolAction::new(runner, *self))
  45. }
  46. }
  47. impl<C: AsRef<str>, U: AsRef<str>, R: CommandRunner> fmt::Display for Cron<'_, C, U, R> {
  48. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
  49. write!(f, "Cron {}", self.user.as_ref())
  50. }
  51. }