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
2.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
5 years ago
7 years ago
6 years ago
5 years ago
7 years ago
7 years ago
7 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
7 years ago
6 years ago
6 years ago
7 years ago
  1. use std::error::Error;
  2. use std::fmt;
  3. use std::fs::File as FsFile;
  4. use std::io;
  5. use std::io::{Read, Write};
  6. use std::ops::Deref;
  7. use std::path::Path;
  8. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  9. use resources::Resource;
  10. pub struct File<C, D> where C: Deref<Target=str>, D: AsRef<Path> {
  11. path: D,
  12. content: C
  13. }
  14. impl<C, D> File<C, D> where C: Deref<Target=str>, D: AsRef<Path> {
  15. pub fn new(path: D, content: C) -> Self {
  16. File { path, content }
  17. }
  18. }
  19. impl<C, D> Symbol for File<C, D> where C: Deref<Target=str>, D: AsRef<Path> {
  20. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  21. let file = FsFile::open(self.path.as_ref());
  22. // Check if file exists
  23. if let Err(e) = file {
  24. return if e.kind() == io::ErrorKind::NotFound {
  25. Ok(false)
  26. } else {
  27. Err(Box::new(e))
  28. };
  29. }
  30. // Check if content is the same
  31. let mut file_content = file.unwrap().bytes();
  32. let mut target_content = self.content.bytes();
  33. loop {
  34. match (file_content.next(), target_content.next()) {
  35. (None, None) => return Ok(true),
  36. (Some(Ok(a)), Some(b)) if a == b => {},
  37. (Some(Err(e)), _) => return Err(Box::new(e)),
  38. (_, _) => return Ok(false)
  39. }
  40. }
  41. }
  42. fn execute(&self) -> Result<(), Box<dyn Error>> {
  43. let mut file = try!(FsFile::create(self.path.as_ref()));
  44. try!(file.write_all(self.content.as_bytes()));
  45. Ok(())
  46. }
  47. fn get_prerequisites(&self) -> Vec<Resource> {
  48. vec![ Resource::new("dir", Path::new(self.path.as_ref()).parent().unwrap().to_string_lossy() ) ]
  49. }
  50. fn as_action<'a>(&'a self, runner: &'a dyn SymbolRunner) -> Box<dyn Action + 'a> {
  51. Box::new(SymbolAction::new(runner, self))
  52. }
  53. fn into_action<'a>(self: Box<Self>, runner: &'a dyn SymbolRunner) -> Box<dyn Action + 'a> where Self: 'a {
  54. Box::new(OwnedSymbolAction::new(runner, *self))
  55. }
  56. }
  57. impl<C, D> fmt::Display for File<C, D> where C: Deref<Target=str>, D: AsRef<Path> {
  58. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
  59. write!(f, "File {}", self.path.as_ref().display())
  60. }
  61. }