Command runner args, Paths and AsRef

This commit is contained in:
Adrian Heine 2019-09-24 22:53:15 +02:00
parent 5d5e9dfcb4
commit 3ccf64fac1
32 changed files with 696 additions and 721 deletions

View file

@ -1,7 +1,7 @@
use std::error::Error;
use std::fmt;
use std::io;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use command_runner::CommandRunner;
use resources::Resource;
@ -41,16 +41,11 @@ impl<E: Error> fmt::Display for NginxServerError<E> {
}
}
pub struct NginxServer<'a, C: 'a + CommandRunner, T>
where
T: Deref<Target = str>,
{
pub struct NginxServer<'a, C: CommandRunner, T: AsRef<str>, P: AsRef<Path>> {
command_runner: &'a C,
file: FileSymbol<T, String>,
file: FileSymbol<T, P>,
}
use std::borrow::Cow;
pub fn server_config(domain: &str, content: &str) -> String {
format!(
"server {{
@ -80,9 +75,9 @@ server {{
)
}
pub fn php_server_config_snippet<'a>(
socket_path: Cow<'a, str>,
static_path: Cow<'a, str>,
pub fn php_server_config_snippet<'a, SOCKET: AsRef<Path>, STATIC: AsRef<Path>>(
socket_path: SOCKET,
static_path: STATIC,
) -> String {
format!(
"
@ -92,7 +87,8 @@ pub fn php_server_config_snippet<'a>(
fastcgi_pass unix:{};
include \"snippets/fastcgi-php.conf\";
}}",
static_path, socket_path
static_path.as_ref().to_str().unwrap(),
socket_path.as_ref().to_str().unwrap()
)
}
@ -100,9 +96,12 @@ pub trait SocketSpec {
fn to_nginx(&self) -> String;
}
impl SocketSpec for &str {
impl<T> SocketSpec for T
where
T: AsRef<Path>,
{
fn to_nginx(&self) -> String {
format!("unix:{}:", self)
format!("unix:{}:", self.as_ref().to_str().unwrap())
}
}
@ -120,7 +119,7 @@ impl SocketSpec for LocalTcpSocket {
}
}
impl<'a, C: CommandRunner> NginxServer<'a, C, String> {
impl<'a, C: CommandRunner> NginxServer<'a, C, String, PathBuf> {
pub fn new_redir(domain: &'a str, target: &'a str, command_runner: &'a C) -> Self {
let content = server_config(
domain,
@ -134,10 +133,10 @@ impl<'a, C: CommandRunner> NginxServer<'a, C, String> {
NginxServer::new(domain, content, command_runner)
}
pub fn new_proxy<S: SocketSpec>(
pub fn new_proxy<S: SocketSpec, STATIC: AsRef<Path>>(
domain: &'a str,
socket_path: S,
static_path: &'a str,
static_path: STATIC,
command_runner: &'a C,
) -> Self {
let proxy_content = format!(
@ -160,16 +159,17 @@ location @proxy {{
root {};
{}
",
static_path, proxy_content
static_path.as_ref().to_str().unwrap(),
proxy_content
),
);
NginxServer::new(domain, content, command_runner)
}
pub fn new_php(
pub fn new_php<SOCKET: AsRef<Path>, STATIC: AsRef<Path>>(
domain: &'a str,
socket_path: Cow<'a, str>,
static_path: Cow<'a, str>,
socket_path: SOCKET,
static_path: STATIC,
command_runner: &'a C,
additional_config: &'a str,
) -> Self {
@ -180,7 +180,11 @@ location @proxy {{
NginxServer::new(domain, content, command_runner)
}
pub fn new_static(domain: &'a str, static_path: &'a str, command_runner: &'a C) -> Self {
pub fn new_static<S: AsRef<Path>>(
domain: &'a str,
static_path: S,
command_runner: &'a C,
) -> Self {
let content = server_config(
domain,
&format!(
@ -188,14 +192,14 @@ location @proxy {{
root {};
try_files $uri $uri/ $uri.html =404;
",
static_path
static_path.as_ref().to_str().unwrap()
),
);
NginxServer::new(domain, content, command_runner)
}
pub fn new(domain: &'a str, content: String, command_runner: &'a C) -> Self {
let file_path = String::from("/etc/nginx/sites-enabled/") + domain;
let file_path: PathBuf = ["/etc/nginx/sites-enabled/", domain].iter().collect();
NginxServer {
command_runner,
file: FileSymbol::new(file_path, content),
@ -203,10 +207,7 @@ location @proxy {{
}
}
impl<'a, C: CommandRunner, T> Symbol for NginxServer<'a, C, T>
where
T: Deref<Target = str>,
{
impl<'a, C: CommandRunner, T: AsRef<str>, P: AsRef<Path>> Symbol for NginxServer<'a, C, T, P> {
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
if !self.file.target_reached()? {
return Ok(false);
@ -219,7 +220,7 @@ where
self.file.execute()?;
self
.command_runner
.run_successfully("systemctl", &["reload-or-restart", "nginx"])
.run_successfully("systemctl", args!["reload-or-restart", "nginx"])
}
fn get_prerequisites(&self) -> Vec<Resource> {
@ -238,9 +239,8 @@ where
}
}
impl<'a, C: CommandRunner, T> fmt::Display for NginxServer<'a, C, T>
where
T: Deref<Target = str>,
impl<'a, C: CommandRunner, T: AsRef<str>, P: AsRef<Path>> fmt::Display
for NginxServer<'a, C, T, P>
{
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "Nginx server config")