From 955361433d953329fa252969b6deb5ef3ef8d224 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 24 Nov 2019 07:37:42 -0800 Subject: [PATCH] split daemon options out of config/mod.rs --- src/config/daemon.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++ src/config/mod.rs | 58 ++------------------------------------------ 2 files changed, 60 insertions(+), 56 deletions(-) create mode 100644 src/config/daemon.rs diff --git a/src/config/daemon.rs b/src/config/daemon.rs new file mode 100644 index 000000000..bf585bbe0 --- /dev/null +++ b/src/config/daemon.rs @@ -0,0 +1,58 @@ +use crate::config::*; +use std::fs::{File, OpenOptions}; +use std::path::PathBuf; + +#[derive(Default, Debug, Clone, Deserialize)] +pub struct DaemonOptions { + pub pid_file: Option, + pub stdout: Option, + pub stderr: Option, +} + +fn open_log(path: PathBuf) -> Fallible { + create_user_owned_dirs( + path.parent() + .ok_or_else(|| format_err!("path {} has no parent dir!?", path.display()))?, + )?; + let mut options = OpenOptions::new(); + options.write(true).create(true).append(true); + options + .open(&path) + .map_err(|e| format_err!("failed to open log stream: {}: {}", path.display(), e)) +} + +impl DaemonOptions { + #[cfg_attr(windows, allow(dead_code))] + pub fn pid_file(&self) -> PathBuf { + self.pid_file + .as_ref() + .cloned() + .unwrap_or_else(|| RUNTIME_DIR.join("pid")) + } + + #[cfg_attr(windows, allow(dead_code))] + pub fn stdout(&self) -> PathBuf { + self.stdout + .as_ref() + .cloned() + .unwrap_or_else(|| RUNTIME_DIR.join("log")) + } + + #[cfg_attr(windows, allow(dead_code))] + pub fn stderr(&self) -> PathBuf { + self.stderr + .as_ref() + .cloned() + .unwrap_or_else(|| RUNTIME_DIR.join("log")) + } + + #[cfg_attr(windows, allow(dead_code))] + pub fn open_stdout(&self) -> Fallible { + open_log(self.stdout()) + } + + #[cfg_attr(windows, allow(dead_code))] + pub fn open_stderr(&self) -> Fallible { + open_log(self.stderr()) + } +} diff --git a/src/config/mod.rs b/src/config/mod.rs index eb562a4c0..03c26acb2 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -13,7 +13,6 @@ use std::collections::HashMap; use std::convert::TryInto; use std::ffi::{OsStr, OsString}; use std::fs; -use std::fs::{File, OpenOptions}; use std::io::prelude::*; use std::path::PathBuf; use term; @@ -23,7 +22,9 @@ use termwiz::hyperlink; use termwiz::input::{KeyCode, Modifiers}; use toml; +mod daemon; mod keys; +pub use daemon::*; pub use keys::*; #[derive(Debug, Deserialize, Clone)] @@ -193,61 +194,6 @@ fn default_dpi() -> f64 { 96.0 } -#[derive(Default, Debug, Clone, Deserialize)] -pub struct DaemonOptions { - pub pid_file: Option, - pub stdout: Option, - pub stderr: Option, -} - -fn open_log(path: PathBuf) -> Fallible { - create_user_owned_dirs( - path.parent() - .ok_or_else(|| format_err!("path {} has no parent dir!?", path.display()))?, - )?; - let mut options = OpenOptions::new(); - options.write(true).create(true).append(true); - options - .open(&path) - .map_err(|e| format_err!("failed to open log stream: {}: {}", path.display(), e)) -} - -impl DaemonOptions { - #[cfg_attr(windows, allow(dead_code))] - pub fn pid_file(&self) -> PathBuf { - self.pid_file - .as_ref() - .cloned() - .unwrap_or_else(|| RUNTIME_DIR.join("pid")) - } - - #[cfg_attr(windows, allow(dead_code))] - pub fn stdout(&self) -> PathBuf { - self.stdout - .as_ref() - .cloned() - .unwrap_or_else(|| RUNTIME_DIR.join("log")) - } - - #[cfg_attr(windows, allow(dead_code))] - pub fn stderr(&self) -> PathBuf { - self.stderr - .as_ref() - .cloned() - .unwrap_or_else(|| RUNTIME_DIR.join("log")) - } - - #[cfg_attr(windows, allow(dead_code))] - pub fn open_stdout(&self) -> Fallible { - open_log(self.stdout()) - } - - #[cfg_attr(windows, allow(dead_code))] - pub fn open_stderr(&self) -> Fallible { - open_log(self.stderr()) - } -} - #[derive(Default, Debug, Clone, Deserialize)] pub struct SshDomain { /// The name of this specific domain. Must be unique amongst