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.

154 lines
3.7 KiB

use std::borrow::{Borrow, Cow};
use std::error::Error;
use std::fmt;
use std::fs;
use std::io;
use std::path::Path;
use std::str::FromStr;
use command_runner::CommandRunner;
use resources::Resource;
use storage::Storage;
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
#[derive(Debug, PartialEq)]
pub enum StorageDirection {
Load,
Save,
}
pub struct StoredDirectory<'a, S, C: 'a + CommandRunner>
where
S: Storage,
{
path: Cow<'a, str>,
storage: S,
dir: StorageDirection,
command_runner: &'a C,
}
impl<'a, S, C: CommandRunner> StoredDirectory<'a, S, C>
where
S: Storage,
{
pub fn new(path: Cow<'a, str>, storage: S, dir: StorageDirection, command_runner: &'a C) -> Self {
StoredDirectory {
path,
storage,
dir,
command_runner,
}
}
}
impl<'a, S, C: CommandRunner> fmt::Display for StoredDirectory<'a, S, C>
where
S: Storage,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Stored directory {} ({:?})", self.path, self.dir)
}
}
impl<'a, S, C: CommandRunner> Symbol for StoredDirectory<'a, S, C>
where
S: Storage,
{
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
let metadata = fs::metadata(self.path.as_ref());
// Check if dir exists
if let Err(e) = metadata {
return if e.kind() == io::ErrorKind::NotFound {
Ok(self.dir == StorageDirection::Save)
} else {
Err(Box::new(e))
};
}
if !metadata.unwrap().is_dir() {
return Err(Box::new(io::Error::new(
io::ErrorKind::AlreadyExists,
"Could not create a directory, non-directory file exists",
)));
}
let dump_date = self.storage.recent_date()?;
let output = self.command_runner.get_output(
"sh",
&[
"-c",
&format!(
"find {} -printf '%T@\\n' | sort -r | head -n1 | grep '^[0-9]\\+' -o",
self.path
),
],
)?;
let modified_date = u64::from_str(String::from_utf8(output)?.trim_end())?;
if if self.dir == StorageDirection::Save {
modified_date > dump_date
} else {
dump_date > modified_date
} {
let output = self.command_runner.run_with_args(
"diff",
&["-rq", &self.storage.read_filename()?, self.path.borrow()],
)?;
match output.status.code() {
Some(0) => Ok(true),
Some(1) => Ok(false),
_ => Err(String::from_utf8(output.stderr)?.into()),
}
} else {
Ok(true)
}
}
fn execute(&self) -> Result<(), Box<dyn Error>> {
if self.dir == StorageDirection::Load {
self
.command_runner
.run_successfully("rm", &["-rf", self.path.borrow()])?;
self.command_runner.run_successfully(
"cp",
&["-a", &self.storage.read_filename()?, self.path.borrow()],
)
} else {
self.command_runner.run_successfully(
"cp",
&["-a", self.path.borrow(), &self.storage.write_filename()],
)
}
}
fn get_prerequisites(&self) -> Vec<Resource> {
if self.dir == StorageDirection::Save {
return vec![];
}
if let Some(parent) = Path::new(self.path.as_ref()).parent() {
vec![Resource::new("dir", parent.to_string_lossy())]
} else {
vec![]
}
}
fn provides(&self) -> Option<Vec<Resource>> {
if self.dir == StorageDirection::Load {
Some(vec![Resource::new("dir", self.path.to_string())])
} else {
None
}
}
fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
Box::new(SymbolAction::new(runner, self))
}
fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
where
Self: 'b,
{
Box::new(OwnedSymbolAction::new(runner, *self))
}
}
#[cfg(test)]
mod test {}