Skip to content

Commit

Permalink
Merge pull request #205 from sanders41/watch
Browse files Browse the repository at this point in the history
Add option to poll status
  • Loading branch information
sanders41 authored Mar 1, 2024
2 parents d44e366 + e4a245d commit 9a79b4b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/github_api.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{process::exit, thread, time::Duration};

use anyhow::Result;
use colored::*;
use pager::Pager;
Expand Down Expand Up @@ -376,6 +378,50 @@ impl SummaryInfo {
_ => println!("{}", "Error retrieving information".red()),
};
}

pub fn watch(duration: Duration, cancel_when_operational: bool) {
let mut check = 1;

if cancel_when_operational {
println!(
"Watching GitHub status with {:?} between checks. Polling will stop when everything is operational. Press Ctrl + c to cancel early.",
duration
);
} else {
println!(
"Watching GitHub status with {:?} between checks. Press Ctrl + c to cancel.",
duration
)
};

loop {
println!("\nCheck number: {check}\n");

let summary = SummaryInfo::get_info("https://www.githubstatus.com/api/v2/summary.json");

if let Ok(s) = summary {
SummaryInfo::print(&s, false).unwrap();

if cancel_when_operational {
let mut operational = true;

for component in s.components {
if component.description.is_some() && component.status != "operational" {
operational = false;
}
}

if operational {
println!("All services are operational, exiting");
exit(0);
}
}
};

check += 1;
thread::sleep(duration);
}
}
}

#[derive(Deserialize, Debug)]
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod github_api;
mod options;

use std::time::Duration;

use crate::github_api::{ComponentInfo, IncidentInfo, MaintenanceInfo, StatusInfo, SummaryInfo};
use crate::options::{Command, Options};

Expand All @@ -18,5 +20,9 @@ fn main() {
Command::Summary { pager } => SummaryInfo::print_info(pager),
Command::UnresolvedIncidents { pager } => IncidentInfo::print_unresolved(pager),
Command::UpcomingMaintenance { pager } => MaintenanceInfo::print_upcoming(pager),
Command::Watch {
duration,
cancel_when_operational,
} => SummaryInfo::watch(Duration::from_secs(duration * 60), cancel_when_operational),
}
}
18 changes: 18 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,22 @@ pub enum Command {
#[clap(short, long, help = "If set the output will be displayed in a pager")]
pager: bool,
},

/// Continue polling for status
Watch {
#[clap(
short,
long,
default_value_t = 1,
help = "The duration to wait between polling in minutes"
)]
duration: u64,

#[clap(
short,
long,
help = "Cancel the watch when all services are operational"
)]
cancel_when_operational: bool,
},
}

0 comments on commit 9a79b4b

Please sign in to comment.