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.

95 lines
2.1 KiB

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