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.

45 lines
1.2 KiB

use crate::symbols::Symbol;
use async_trait::async_trait;
use std::error::Error;
use std::fs::File as FsFile;
use std::io::{Read, Write};
use std::path::Path;
#[derive(Debug)]
pub struct File<D, C> {
path: D,
content: C,
}
impl<D, C> File<D, C> {
pub fn new(path: D, content: C) -> Self {
Self { path, content }
}
}
#[async_trait(?Send)]
impl<D: AsRef<Path>, C: AsRef<str>> Symbol for File<D, C> {
async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
if !self.path.as_ref().exists() {
return Ok(false);
}
let file = FsFile::open(self.path.as_ref())?;
// Check if content is the same
let mut file_content = file.bytes();
let mut target_content = self.content.as_ref().bytes();
loop {
match (file_content.next(), target_content.next()) {
(None, None) => return Ok(true),
(Some(Ok(a)), Some(b)) if a == b => {}
(Some(Err(e)), _) => return Err(Box::new(e)),
(_, _) => return Ok(false),
}
}
}
async fn execute(&self) -> Result<(), Box<dyn Error>> {
let mut file = FsFile::create(self.path.as_ref())?;
file.write_all(self.content.as_ref().as_bytes())?;
Ok(())
}
}