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.

49 lines
1.2 KiB

  1. use std::borrow::Cow;
  2. use std::error::Error;
  3. use std::fmt;
  4. use std::fs;
  5. use std::io;
  6. use std::io::{Read, Write};
  7. use std::ops::Deref;
  8. use std::path::Path;
  9. use symbols::Symbol;
  10. pub struct Dir<D> where D: AsRef<str> + fmt::Display {
  11. path: D
  12. }
  13. impl<D> Dir<D> where D: AsRef<str> + fmt::Display {
  14. pub fn new(path: D) -> Self {
  15. Dir { path: path }
  16. }
  17. }
  18. impl<D> Symbol for Dir<D> where D: AsRef<str> + fmt::Display {
  19. fn target_reached(&self) -> Result<bool, Box<Error>> {
  20. let metadata = fs::metadata(self.path.as_ref());
  21. // Check if dir exists
  22. if let Err(e) = metadata {
  23. return if e.kind() == io::ErrorKind::NotFound {
  24. Ok(false)
  25. } else {
  26. Err(Box::new(e))
  27. };
  28. }
  29. if metadata.unwrap().is_dir() {
  30. Ok(true)
  31. } else {
  32. Err(Box::new(io::Error::new(io::ErrorKind::AlreadyExists, "Could not create a directory, non-directory file exists")))
  33. }
  34. }
  35. fn execute(&self) -> Result<(), Box<Error>> {
  36. fs::create_dir_all(self.path.as_ref()).map_err(|e| Box::new(e) as Box<Error>)
  37. }
  38. }
  39. impl<D> fmt::Display for Dir<D> where D: AsRef<str> + fmt::Display {
  40. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
  41. write!(f, "Dir {}", self.path)
  42. }
  43. }