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.

70 lines
1.7 KiB

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