Use -c for config. Fix #3.

This commit is contained in:
2018-10-03 08:06:50 +02:00
parent 51e5ef2815
commit e5b0caad36
3 changed files with 17 additions and 18 deletions

View File

@@ -24,14 +24,14 @@ use subprocess::{Exec, NullFile};
#[derive(StructOpt)]
#[structopt(name = "ips-uptime")]
struct Opt {
#[structopt(parse(from_os_str))]
#[structopt(short = "c", long = "config", parse(from_os_str))]
config: PathBuf,
}
#[derive(Debug, Deserialize)]
struct Config {
command: HashMap<String, Command>,
hosts: Vec<Host>,
checks: Vec<Check>,
}
#[derive(Debug, Deserialize)]
@@ -40,7 +40,7 @@ struct Command {
}
#[derive(Debug, Deserialize)]
struct Host {
struct Check {
name: String,
command: String,
#[serde(flatten)]
@@ -49,7 +49,7 @@ struct Host {
cmd: String,
}
impl Host {
impl Check {
fn status(&self) -> bool {
let result = Exec::shell(&self.cmd)
.stdout(NullFile)
@@ -76,15 +76,15 @@ fn main() {
handlebars.set_strict_mode(true);
for host in &mut config.hosts {
for check in &mut config.checks {
let command = config
.command
.get(&host.command)
.get(&check.command)
.map(|command| &command.command)
.unwrap_or(&host.command);
.unwrap_or(&check.command);
host.cmd = handlebars
.render_template(command, &host.args)
check.cmd = handlebars
.render_template(command, &check.args)
.expect("failed to register template");
}
@@ -96,10 +96,9 @@ fn main() {
let now = Local::now();
let result = config
.hosts
.checks
.iter()
// .inspect(|host| println!("{}", host.name))
.find(|host| host.status());
.find(|check| check.status());
if result.is_some() {
if let Some(down) = status {