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.

38 lines
862 B

use crate::symbols::Symbol;
use async_trait::async_trait;
use std::error::Error;
use std::fs;
use std::io;
use std::path::Path;
#[derive(Debug)]
pub struct Dir<P> {
path: P,
}
impl<P> Dir<P> {
pub fn new(path: P) -> Self {
Self { path }
}
}
#[async_trait(?Send)]
impl<P: AsRef<Path>> Symbol for Dir<P> {
async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
if !self.path.as_ref().exists() {
return Ok(false);
}
let metadata = fs::metadata(self.path.as_ref())?;
if !metadata.is_dir() {
return Err(Box::new(io::Error::new(
io::ErrorKind::AlreadyExists,
"Could not create a directory, non-directory file exists",
)));
}
Ok(true)
}
async fn execute(&self) -> Result<(), Box<dyn Error>> {
fs::create_dir(self.path.as_ref()).map_err(|e| Box::new(e) as Box<dyn Error>)
}
}