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.

49 lines
1.1 KiB

  1. use crate::symbols::Symbol;
  2. use async_trait::async_trait;
  3. use std::error::Error;
  4. use std::fs::{metadata, File};
  5. use std::io::copy;
  6. use std::marker::PhantomData;
  7. use std::path::Path;
  8. #[derive(Debug)]
  9. pub struct Concat<S, D, I> {
  10. target: D,
  11. sources: S,
  12. source_item: PhantomData<I>,
  13. }
  14. impl<S, D, I> Concat<S, D, I> {
  15. pub fn new(sources: S, target: D) -> Self {
  16. Self {
  17. target,
  18. sources,
  19. source_item: PhantomData::default(),
  20. }
  21. }
  22. }
  23. #[async_trait(?Send)]
  24. impl<S: AsRef<[I]>, D: AsRef<Path>, I: AsRef<Path>> Symbol for Concat<S, D, I> {
  25. async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  26. let target = self.target.as_ref();
  27. if !target.exists() {
  28. return Ok(false);
  29. }
  30. let target_date = metadata(target)?.modified()?;
  31. for source in self.sources.as_ref() {
  32. if metadata(source)?.modified()? > target_date {
  33. return Ok(false);
  34. }
  35. }
  36. Ok(true)
  37. }
  38. async fn execute(&self) -> Result<(), Box<dyn Error>> {
  39. let mut file = File::create(self.target.as_ref())?;
  40. for source in self.sources.as_ref() {
  41. copy(&mut File::open(source)?, &mut file)?;
  42. }
  43. Ok(())
  44. }
  45. }