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.

72 lines
1.9 KiB

use std::error::Error;
use std::fmt;
use std::fs::File as FsFile;
use std::io::{Read, Write};
use std::path::Path;
use resources::Resource;
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
pub struct File<C: AsRef<str>, D: AsRef<Path>> {
path: D,
content: C,
}
impl<C: AsRef<str>, D: AsRef<Path>> File<C, D> {
pub fn new(path: D, content: C) -> Self {
Self { path, content }
}
}
impl<C: AsRef<str>, D: AsRef<Path>> Symbol for File<C, D> {
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),
}
}
}
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(())
}
fn get_prerequisites(&self) -> Vec<Resource> {
if let Some(parent) = self.path.as_ref().parent() {
vec![Resource::new("dir", parent.to_str().unwrap())]
} else {
vec![]
}
}
fn as_action<'a>(&'a self, runner: &'a dyn SymbolRunner) -> Box<dyn Action + 'a> {
Box::new(SymbolAction::new(runner, self))
}
fn into_action<'a>(self: Box<Self>, runner: &'a dyn SymbolRunner) -> Box<dyn Action + 'a>
where
Self: 'a,
{
Box::new(OwnedSymbolAction::new(runner, *self))
}
}
impl<C: AsRef<str>, D: AsRef<Path>> fmt::Display for File<C, D> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "File {}", self.path.as_ref().display())
}
}