From f3b70607f10506496d029d5a27f4f38c8a1b37e1 Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Thu, 16 Feb 2017 20:48:46 +0100 Subject: [PATCH] Add some prerequisites --- src/resources/mod.rs | 11 +++++ src/symbols/dir.rs | 49 +++++++++++++++++++++ src/symbols/file.rs | 6 +++ src/symbols/mod.rs | 5 +++ src/symbols/nginx/server.rs | 5 +++ src/symbols/systemd/node_js_user_service.rs | 5 +++ 6 files changed, 81 insertions(+) create mode 100644 src/symbols/dir.rs diff --git a/src/resources/mod.rs b/src/resources/mod.rs index 1c3e3e6..8358845 100644 --- a/src/resources/mod.rs +++ b/src/resources/mod.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + pub trait Resource { fn get_type(&self) -> &str; fn get_value(&self) -> &str; @@ -11,3 +13,12 @@ impl<'a> Resource for UserResource<'a> { fn get_type(&self) -> &str { "user" } fn get_value(&self) -> &str { self.name } } + +pub struct DirResource<'a> { + pub path: Cow<'a, str> +} + +impl<'a> Resource for DirResource<'a> { + fn get_type(&self) -> &str { "dir" } + fn get_value(&self) -> &str { &*self.path } +} diff --git a/src/symbols/dir.rs b/src/symbols/dir.rs new file mode 100644 index 0000000..b5188b7 --- /dev/null +++ b/src/symbols/dir.rs @@ -0,0 +1,49 @@ +use std::borrow::Cow; +use std::error::Error; +use std::fmt; +use std::fs; +use std::io; +use std::io::{Read, Write}; +use std::ops::Deref; +use std::path::Path; + +use symbols::Symbol; + +pub struct Dir where D: AsRef + fmt::Display { + path: D +} + +impl Dir where D: AsRef + fmt::Display { + pub fn new(path: D) -> Self { + Dir { path: path } + } +} + +impl Symbol for Dir where D: AsRef + fmt::Display { + fn target_reached(&self) -> Result> { + let metadata = fs::metadata(self.path.as_ref()); + // Check if dir exists + if let Err(e) = metadata { + return if e.kind() == io::ErrorKind::NotFound { + Ok(false) + } else { + Err(Box::new(e)) + }; + } + if metadata.unwrap().is_dir() { + Ok(true) + } else { + Err(Box::new(io::Error::new(io::ErrorKind::AlreadyExists, "Could not create a directory, non-directory file exists"))) + } + } + + fn execute(&self) -> Result<(), Box> { + fs::create_dir_all(self.path.as_ref()).map_err(|e| Box::new(e) as Box) + } +} + +impl fmt::Display for Dir where D: AsRef + fmt::Display { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{ + write!(f, "Dir {}", self.path) + } +} diff --git a/src/symbols/file.rs b/src/symbols/file.rs index eff720b..b5b29ae 100644 --- a/src/symbols/file.rs +++ b/src/symbols/file.rs @@ -8,6 +8,7 @@ use std::ops::Deref; use std::path::Path; use symbols::Symbol; +use resources::{DirResource, Resource}; pub struct File where C: Deref, D: AsRef + fmt::Display { path: D, @@ -52,6 +53,11 @@ impl Symbol for File where C: Deref, D: AsRef + fmt try!(file.write_all(self.content.as_bytes())); Ok(()) } + + fn get_prerequisites(&self) -> Vec> { + vec![ + Box::new(DirResource { path: String::from(Path::new(self.path.as_ref()).parent().unwrap().to_string_lossy()).into() }) ] + } } impl fmt::Display for File where C: Deref, D: AsRef + fmt::Display { diff --git a/src/symbols/mod.rs b/src/symbols/mod.rs index d05c2cb..bc58be4 100644 --- a/src/symbols/mod.rs +++ b/src/symbols/mod.rs @@ -1,12 +1,17 @@ use std::borrow::Cow; use std::error::Error; use std::fmt::Display; +use resources::Resource; pub trait Symbol: Display { fn target_reached(&self) -> Result>; fn execute(&self) -> Result<(), Box>; + fn get_prerequisites(&self) -> Vec> { + vec![] + } } +pub mod dir; pub mod file; pub mod git; pub mod npm; diff --git a/src/symbols/nginx/server.rs b/src/symbols/nginx/server.rs index 27baf39..3fcf786 100644 --- a/src/symbols/nginx/server.rs +++ b/src/symbols/nginx/server.rs @@ -6,6 +6,7 @@ use std::ops::Deref; use command_runner::CommandRunner; use symbols::Symbol; use symbols::file::File as FileSymbol; +use resources::Resource; #[derive(Debug)] pub enum NginxServerError { @@ -99,6 +100,10 @@ impl<'a, C> Symbol for NginxServer<'a, C> where C: Deref { try!(self.command_runner.run_with_args("systemctl", &["reload-or-restart", "nginx"])); Ok(()) } + + fn get_prerequisites(&self) -> Vec> { + self.file.get_prerequisites() + } } impl<'a, C> fmt::Display for NginxServer<'a, C> where C: Deref { diff --git a/src/symbols/systemd/node_js_user_service.rs b/src/symbols/systemd/node_js_user_service.rs index 4a19531..941cfbe 100644 --- a/src/symbols/systemd/node_js_user_service.rs +++ b/src/symbols/systemd/node_js_user_service.rs @@ -12,6 +12,7 @@ use std::ops::Deref; use command_runner::CommandRunner; use symbols::Symbol; use symbols::file::File as FileSymbol; +use resources::Resource; #[derive(Debug)] pub enum NodeJsSystemdUserServiceError { @@ -146,6 +147,10 @@ impl<'a, P, C> Symbol for NodeJsSystemdUserService<'a, P, C> where P: AsRef Ok(()) } + + fn get_prerequisites(&self) -> Vec> { + self.file.get_prerequisites() + } } impl<'a, P, C> fmt::Display for NodeJsSystemdUserService<'a, P, C> where P: AsRef + fmt::Display, C: Deref {