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

use crate::symbols::Symbol;
use async_trait::async_trait;
use std::error::Error;
use std::fs::{metadata, File};
use std::io::copy;
use std::marker::PhantomData;
use std::path::Path;
#[derive(Debug)]
pub struct Concat<S, D, I> {
target: D,
sources: S,
source_item: PhantomData<I>,
}
impl<S, D, I> Concat<S, D, I> {
pub fn new(sources: S, target: D) -> Self {
Self {
target,
sources,
source_item: PhantomData::default(),
}
}
}
#[async_trait(?Send)]
impl<S: AsRef<[I]>, D: AsRef<Path>, I: AsRef<Path>> Symbol for Concat<S, D, I> {
async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
let target = self.target.as_ref();
if !target.exists() {
return Ok(false);
}
let target_date = metadata(target)?.modified()?;
for source in self.sources.as_ref() {
if metadata(source)?.modified()? > target_date {
return Ok(false);
}
}
Ok(true)
}
async fn execute(&self) -> Result<(), Box<dyn Error>> {
let mut file = File::create(self.target.as_ref())?;
for source in self.sources.as_ref() {
copy(&mut File::open(source)?, &mut file)?;
}
Ok(())
}
}