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

7 years ago
5 years ago
5 years ago
7 years ago
  1. use crate::symbols::Symbol;
  2. use async_trait::async_trait;
  3. use std::error::Error;
  4. use std::fs;
  5. use std::io;
  6. use std::path::Path;
  7. #[derive(Debug)]
  8. pub struct Dir<P> {
  9. path: P,
  10. }
  11. impl<P> Dir<P> {
  12. pub fn new(path: P) -> Self {
  13. Self { path }
  14. }
  15. }
  16. #[async_trait(?Send)]
  17. impl<P: AsRef<Path>> Symbol for Dir<P> {
  18. async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  19. if !self.path.as_ref().exists() {
  20. return Ok(false);
  21. }
  22. let metadata = fs::metadata(self.path.as_ref())?;
  23. if !metadata.is_dir() {
  24. return Err(Box::new(io::Error::new(
  25. io::ErrorKind::AlreadyExists,
  26. "Could not create a directory, non-directory file exists",
  27. )));
  28. }
  29. Ok(true)
  30. }
  31. async fn execute(&self) -> Result<(), Box<dyn Error>> {
  32. fs::create_dir(self.path.as_ref()).map_err(|e| Box::new(e) as Box<dyn Error>)
  33. }
  34. }