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.

36 lines
798 B

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