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.

67 lines
1.8 KiB

7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
5 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
  1. use std::error::Error;
  2. use std::fmt;
  3. use std::fs;
  4. use std::io;
  5. use std::path::Path;
  6. use resources::Resource;
  7. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  8. pub struct Dir<D> where D: AsRef<str> {
  9. path: D
  10. }
  11. impl<D> Dir<D> where D: AsRef<str> {
  12. pub fn new(path: D) -> Self {
  13. Dir { path }
  14. }
  15. }
  16. impl<D> Symbol for Dir<D> where D: AsRef<str> {
  17. fn target_reached(&self) -> Result<bool, Box<Error>> {
  18. let metadata = fs::metadata(self.path.as_ref());
  19. // Check if dir exists
  20. if let Err(e) = metadata {
  21. return if e.kind() == io::ErrorKind::NotFound {
  22. Ok(false)
  23. } else {
  24. Err(Box::new(e))
  25. };
  26. }
  27. if metadata.unwrap().is_dir() {
  28. Ok(true)
  29. } else {
  30. Err(Box::new(io::Error::new(io::ErrorKind::AlreadyExists, "Could not create a directory, non-directory file exists")))
  31. }
  32. }
  33. fn execute(&self) -> Result<(), Box<Error>> {
  34. fs::create_dir(self.path.as_ref()).map_err(|e| Box::new(e) as Box<Error>)
  35. }
  36. fn get_prerequisites(&self) -> Vec<Resource> {
  37. if let Some(parent) = Path::new(self.path.as_ref()).parent() {
  38. vec![ Resource::new("dir", parent.to_string_lossy()) ]
  39. } else {
  40. vec![]
  41. }
  42. }
  43. fn provides(&self) -> Option<Vec<Resource>> {
  44. Some(vec![ Resource::new("dir", self.path.as_ref()) ])
  45. }
  46. fn as_action<'a>(&'a self, runner: &'a SymbolRunner) -> Box<Action + 'a> {
  47. Box::new(SymbolAction::new(runner, self))
  48. }
  49. fn into_action<'a>(self: Box<Self>, runner: &'a SymbolRunner) -> Box<Action + 'a> where Self: 'a {
  50. Box::new(OwnedSymbolAction::new(runner, *self))
  51. }
  52. }
  53. impl<D> fmt::Display for Dir<D> where D: AsRef<str> {
  54. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
  55. write!(f, "Dir {}", self.path.as_ref())
  56. }
  57. }