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.

47 lines
1.1 KiB

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