Add some prerequisites
This commit is contained in:
parent
124b639e50
commit
f3b70607f1
6 changed files with 81 additions and 0 deletions
|
|
@ -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 }
|
||||
}
|
||||
|
|
|
|||
49
src/symbols/dir.rs
Normal file
49
src/symbols/dir.rs
Normal file
|
|
@ -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<D> where D: AsRef<str> + fmt::Display {
|
||||
path: D
|
||||
}
|
||||
|
||||
impl<D> Dir<D> where D: AsRef<str> + fmt::Display {
|
||||
pub fn new(path: D) -> Self {
|
||||
Dir { path: path }
|
||||
}
|
||||
}
|
||||
|
||||
impl<D> Symbol for Dir<D> where D: AsRef<str> + fmt::Display {
|
||||
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
||||
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<Error>> {
|
||||
fs::create_dir_all(self.path.as_ref()).map_err(|e| Box::new(e) as Box<Error>)
|
||||
}
|
||||
}
|
||||
|
||||
impl<D> fmt::Display for Dir<D> where D: AsRef<str> + fmt::Display {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
|
||||
write!(f, "Dir {}", self.path)
|
||||
}
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ use std::ops::Deref;
|
|||
use std::path::Path;
|
||||
|
||||
use symbols::Symbol;
|
||||
use resources::{DirResource, Resource};
|
||||
|
||||
pub struct File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt::Display {
|
||||
path: D,
|
||||
|
|
@ -52,6 +53,11 @@ impl<C, D> Symbol for File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt
|
|||
try!(file.write_all(self.content.as_bytes()));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_prerequisites(&self) -> Vec<Box<Resource>> {
|
||||
vec![
|
||||
Box::new(DirResource { path: String::from(Path::new(self.path.as_ref()).parent().unwrap().to_string_lossy()).into() }) ]
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, D> fmt::Display for File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt::Display {
|
||||
|
|
|
|||
|
|
@ -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<bool, Box<Error>>;
|
||||
fn execute(&self) -> Result<(), Box<Error>>;
|
||||
fn get_prerequisites(&self) -> Vec<Box<Resource>> {
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
pub mod dir;
|
||||
pub mod file;
|
||||
pub mod git;
|
||||
pub mod npm;
|
||||
|
|
|
|||
|
|
@ -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<E: Error> {
|
||||
|
|
@ -99,6 +100,10 @@ impl<'a, C> Symbol for NginxServer<'a, C> where C: Deref<Target=str> {
|
|||
try!(self.command_runner.run_with_args("systemctl", &["reload-or-restart", "nginx"]));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_prerequisites(&self) -> Vec<Box<Resource>> {
|
||||
self.file.get_prerequisites()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, C> fmt::Display for NginxServer<'a, C> where C: Deref<Target=str> {
|
||||
|
|
|
|||
|
|
@ -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<E: Error> {
|
||||
|
|
@ -146,6 +147,10 @@ impl<'a, P, C> Symbol for NodeJsSystemdUserService<'a, P, C> where P: AsRef<str>
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_prerequisites(&self) -> Vec<Box<Resource>> {
|
||||
self.file.get_prerequisites()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, P, C> fmt::Display for NodeJsSystemdUserService<'a, P, C> where P: AsRef<str> + fmt::Display, C: Deref<Target=str> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue