diff --git a/src/app.rs b/src/app.rs new file mode 100644 index 0000000..5c7465f --- /dev/null +++ b/src/app.rs @@ -0,0 +1,43 @@ +use std::process::Command; + +use crate::result::Result; + +#[derive(Clone, Debug, Deserialize)] +pub struct App { + pub name: String, + pub command: String, + pub args: Vec, + pub restart_on_success: Option, + pub restart_on_failure: Option, + pub restart_on_terminate: Option, + pub wait: Option, + pub hold: Option, +} + +impl App { + pub fn wait_start(&self) -> Result<()> { + info!("starting application {}", &self.name); + + let mut command = Command::new(&self.command); + command.args(&self.args); + + match command.output() { + Ok(output) => { + if !output.stdout.is_empty() { + let s = String::from_utf8(output.stdout)?; + for line in s.lines() { + info!("[{}] stdout: {}", &self.name, line); + } + } + if !output.stderr.is_empty() { + let s = String::from_utf8(output.stderr)?; + for line in s.lines() { + info!("[{}] stderr: {}", &self.name, line); + } + } + Ok(()) + } + Err(e) => Err(Box::from(e)), + } + } +} diff --git a/src/main.rs b/src/main.rs index abea9d8..b41c2cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,53 +6,18 @@ extern crate serde; extern crate serde_derive; extern crate toml; -use std::error::Error; +mod app; +mod proc; +mod result; + use std::fs::File; use std::io::Read; use std::path::PathBuf; -use std::process::{Child, Command, ExitStatus, Stdio}; - -type SupResult = Result>; - -#[derive(Clone, Debug, Deserialize)] -struct App { - name: String, - command: String, - args: Vec, - restart_on_success: Option, - restart_on_failure: Option, - restart_on_terminate: Option, - wait: Option, - hold: Option, -} - -impl App { - fn wait_start(&self) -> SupResult<()> { - info!("starting application {}", &self.name); +use std::process::ExitStatus; - let mut command = Command::new(&self.command); - command.args(&self.args); - - match command.output() { - Ok(output) => { - if !output.stdout.is_empty() { - let s = String::from_utf8(output.stdout)?; - for line in s.lines() { - info!("[{}] stdout: {}", &self.name, line); - } - } - if !output.stderr.is_empty() { - let s = String::from_utf8(output.stderr)?; - for line in s.lines() { - info!("[{}] stderr: {}", &self.name, line); - } - } - Ok(()) - } - Err(e) => Err(Box::from(e)), - } - } -} +use app::App; +use proc::Proc; +use result::Result; #[derive(Debug, Deserialize)] struct Config { @@ -60,7 +25,7 @@ struct Config { } impl Config { - fn load>(p: T) -> SupResult { + fn load>(p: T) -> Result { let mut file = File::open(p.into())?; let mut buf = String::new(); file.read_to_string(&mut buf)?; @@ -72,68 +37,6 @@ impl Config { } } -struct Proc { - app: App, - child: Child, -} - -impl Proc { - fn start(app: App) -> SupResult { - info!("starting application {}", &app.name); - - let mut command = Command::new(&app.command); - command.stdout(Stdio::piped()); - command.stderr(Stdio::piped()); - command.args(&app.args); - - let child = command.spawn()?; - - Ok(Proc { app, child }) - } - - fn check_stdout(&mut self) -> Option { - let child_stdout = self.child.stdout.as_mut(); - if let Some(stdout) = child_stdout { - let mut buf = String::new(); - match stdout.read_to_string(&mut buf) { - Ok(size) => { - if size > 0 { - return Some(buf); - } else { - return None; - } - } - Err(e) => { - error!("couldn't read stdout from {}: {:?}", &self.app.name, e); - return None; - } - } - } - None - } - - fn check_stderr(&mut self) -> Option { - let child_stderr = self.child.stderr.as_mut(); - if let Some(stderr) = child_stderr { - let mut buf = String::new(); - match stderr.read_to_string(&mut buf) { - Ok(size) => { - if size > 0 { - return Some(buf); - } else { - return None; - } - } - Err(e) => { - error!("couldn't read stderr from {}: {:?}", &self.app.name, e); - return None; - } - } - } - None - } -} - fn main() { if let Err(e) = rag::init() { println!("FATAL: Error initializing logger: {:?}", e); @@ -172,7 +75,7 @@ fn start_apps(apps: Vec) -> (Vec, Option) { (procs, holds) } -fn run() -> SupResult<()> { +fn run() -> Result<()> { let config = Config::load("sup.toml")?; info!("loaded config: sup.toml"); diff --git a/src/proc.rs b/src/proc.rs new file mode 100644 index 0000000..cc76a0c --- /dev/null +++ b/src/proc.rs @@ -0,0 +1,67 @@ +use std::io::Read; +use std::process::{Command, Child, Stdio}; + +use crate::app::App; +use crate::result::Result; + +pub struct Proc { + pub app: App, + pub child: Child, +} + +impl Proc { + pub fn start(app: App) -> Result { + info!("starting application {}", &app.name); + + let mut command = Command::new(&app.command); + command.stdout(Stdio::piped()); + command.stderr(Stdio::piped()); + command.args(&app.args); + + let child = command.spawn()?; + + Ok(Proc { app, child }) + } + + pub fn check_stdout(&mut self) -> Option { + let child_stdout = self.child.stdout.as_mut(); + if let Some(stdout) = child_stdout { + let mut buf = String::new(); + match stdout.read_to_string(&mut buf) { + Ok(size) => { + if size > 0 { + return Some(buf); + } else { + return None; + } + } + Err(e) => { + error!("couldn't read stdout from {}: {:?}", &self.app.name, e); + return None; + } + } + } + None + } + + pub fn check_stderr(&mut self) -> Option { + let child_stderr = self.child.stderr.as_mut(); + if let Some(stderr) = child_stderr { + let mut buf = String::new(); + match stderr.read_to_string(&mut buf) { + Ok(size) => { + if size > 0 { + return Some(buf); + } else { + return None; + } + } + Err(e) => { + error!("couldn't read stderr from {}: {:?}", &self.app.name, e); + return None; + } + } + } + None + } +} diff --git a/src/result.rs b/src/result.rs new file mode 100644 index 0000000..fa138cb --- /dev/null +++ b/src/result.rs @@ -0,0 +1,3 @@ +use std::error::Error; + +pub type Result = std::result::Result>;