Skip to content

Commit

Permalink
Merge pull request #74 from sanders41/pager
Browse files Browse the repository at this point in the history
Add an option to send the output to a pager
  • Loading branch information
sanders41 committed Mar 4, 2023
2 parents 808da9e + 83a7176 commit 18f375a
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 46 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
anyhow = "1.0.69"
clap = { version = "4.1.8", features = ["color", "suggestions", "derive"] }
colored = "2.0.0"
pager = "0.16.1"
reqwest = { version = "0.11.14", features = ["blocking", "json"] }
serde = { version = "1.0.152", features = ["derive"] }

Expand Down
81 changes: 51 additions & 30 deletions src/github_api.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use anyhow::Result;
use colored::*;
use pager::Pager;
use serde::Deserialize;

trait GitHubApiEndpoint: Sized {
fn get_info(url: &str) -> Result<Self>;

fn print(&self) -> Result<()>;
fn print(&self, pager: bool) -> Result<()>;
}

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -90,7 +91,11 @@ impl GitHubApiEndpoint for ComponentInfo {
Ok(result)
}

fn print(&self) -> Result<()> {
fn print(&self, pager: bool) -> Result<()> {
if pager {
Pager::new().setup();
}

for component in &self.components {
if component.description.is_some() {
if component.status == "operational" {
Expand Down Expand Up @@ -128,11 +133,11 @@ impl GitHubApiEndpoint for ComponentInfo {
}

impl ComponentInfo {
pub fn print_info() {
pub fn print_info(pager: bool) {
let status = ComponentInfo::get_info("https://www.githubstatus.com/api/v2/components.json");

match status {
Ok(s) => ComponentInfo::print(&s).unwrap(),
Ok(s) => ComponentInfo::print(&s, pager).unwrap(),
_ => println!("{}", "Error retrieving information".red()),
};
}
Expand All @@ -151,7 +156,11 @@ impl GitHubApiEndpoint for MaintenanceInfo {
Ok(result)
}

fn print(&self) -> Result<()> {
fn print(&self, pager: bool) -> Result<()> {
if pager {
Pager::new().setup();
}

if self.scheduled_maintenances.is_empty() {
println!("No unresolved incidents reported");
println!();
Expand Down Expand Up @@ -206,35 +215,35 @@ impl GitHubApiEndpoint for MaintenanceInfo {
}

impl MaintenanceInfo {
pub fn print_activate() {
pub fn print_activate(pager: bool) {
let info = MaintenanceInfo::get_info(
"https://www.githubstatus.com/api/v2/scheduled-maintenances/active.json",
);

match info {
Ok(i) => MaintenanceInfo::print(&i).unwrap(),
Ok(i) => MaintenanceInfo::print(&i, pager).unwrap(),
_ => println!("{}", "Error retrieving information".red()),
}
}

pub fn print_all() {
pub fn print_all(pager: bool) {
let info = MaintenanceInfo::get_info(
"https://www.githubstatus.com/api/v2/scheduled-maintenances.json",
);

match info {
Ok(i) => MaintenanceInfo::print(&i).unwrap(),
Ok(i) => MaintenanceInfo::print(&i, pager).unwrap(),
_ => println!("{}", "Error retrieving information".red()),
}
}

pub fn print_upcoming() {
pub fn print_upcoming(pager: bool) {
let info = MaintenanceInfo::get_info(
"https://www.githubstatus.com/api/v2/scheduled-maintenances/upcoming.json",
);

match info {
Ok(i) => MaintenanceInfo::print(&i).unwrap(),
Ok(i) => MaintenanceInfo::print(&i, pager).unwrap(),
_ => println!("{}", "Error retrieving information".red()),
}
}
Expand All @@ -253,7 +262,11 @@ impl GitHubApiEndpoint for StatusInfo {
Ok(result)
}

fn print(&self) -> Result<()> {
fn print(&self, pager: bool) -> Result<()> {
if pager {
Pager::new().setup();
}

if self.status.indicator == "none" {
println!("{}", self.status.description.green());
} else if self.status.indicator == "minor" {
Expand All @@ -277,11 +290,11 @@ impl GitHubApiEndpoint for StatusInfo {
}

impl StatusInfo {
pub fn print_info() {
pub fn print_info(pager: bool) {
let status = StatusInfo::get_info("https://www.githubstatus.com/api/v2/status.json");

match status {
Ok(s) => StatusInfo::print(&s).unwrap(),
Ok(s) => StatusInfo::print(&s, pager).unwrap(),
_ => println!("{}", "Error retrieving information".red()),
};
}
Expand All @@ -303,7 +316,11 @@ impl GitHubApiEndpoint for SummaryInfo {
Ok(result)
}

fn print(&self) -> Result<()> {
fn print(&self, pager: bool) -> Result<()> {
if pager {
Pager::new().setup();
}

if self.status.indicator == "none" {
println!("{}", self.status.description.green());
} else if self.status.indicator == "minor" {
Expand Down Expand Up @@ -352,11 +369,11 @@ impl GitHubApiEndpoint for SummaryInfo {
}

impl SummaryInfo {
pub fn print_info() {
pub fn print_info(pager: bool) {
let summary = SummaryInfo::get_info("https://www.githubstatus.com/api/v2/summary.json");

match summary {
Ok(s) => SummaryInfo::print(&s).unwrap(),
Ok(s) => SummaryInfo::print(&s, pager).unwrap(),
_ => println!("{}", "Error retrieving information".red()),
};
}
Expand All @@ -375,7 +392,11 @@ impl GitHubApiEndpoint for IncidentInfo {
Ok(result)
}

fn print(&self) -> Result<()> {
fn print(&self, pager: bool) -> Result<()> {
if pager {
Pager::new().setup();
}

if self.incidents.is_empty() {
println!("No unresolved incidents reported");
println!();
Expand Down Expand Up @@ -430,21 +451,21 @@ impl GitHubApiEndpoint for IncidentInfo {
}

impl IncidentInfo {
pub fn print_all() {
pub fn print_all(pager: bool) {
let info = IncidentInfo::get_info("https://www.githubstatus.com/api/v2/incidents.json");

match info {
Ok(i) => IncidentInfo::print(&i).unwrap(),
Ok(i) => IncidentInfo::print(&i, pager).unwrap(),
_ => println!("{}", "Error retrieving information".red()),
}
}

pub fn print_unresolved() {
pub fn print_unresolved(pager: bool) {
let info =
IncidentInfo::get_info("https://www.githubstatus.com/api/v2/incidents/unresolved.json");

match info {
Ok(i) => IncidentInfo::print(&i).unwrap(),
Ok(i) => IncidentInfo::print(&i, pager).unwrap(),
_ => println!("{}", "Error retrieving information".red()),
}
}
Expand Down Expand Up @@ -504,7 +525,7 @@ mod tests {
}"#;

let info: ComponentInfo = serde_json::from_str(data).unwrap();
let result = info.print();
let result = info.print(false);
assert!(result.is_ok());
}

Expand Down Expand Up @@ -569,7 +590,7 @@ mod tests {
}"#;

let info: IncidentInfo = serde_json::from_str(data).unwrap();
let result = info.print();
let result = info.print(false);
assert!(result.is_ok());
}

Expand Down Expand Up @@ -611,7 +632,7 @@ mod tests {
}"#;

let info: IncidentInfo = serde_json::from_str(data).unwrap();
let result = info.print();
let result = info.print(false);
assert!(result.is_ok());
}

Expand Down Expand Up @@ -664,7 +685,7 @@ mod tests {
}"#;

let info: MaintenanceInfo = serde_json::from_str(data).unwrap();
let result = info.print();
let result = info.print(false);
assert!(result.is_ok());
}

Expand Down Expand Up @@ -742,7 +763,7 @@ mod tests {
}"#;

let info: MaintenanceInfo = serde_json::from_str(data).unwrap();
let result = info.print();
let result = info.print(false);
assert!(result.is_ok());
}

Expand Down Expand Up @@ -786,7 +807,7 @@ mod tests {
}"#;

let info: MaintenanceInfo = serde_json::from_str(data).unwrap();
let result = info.print();
let result = info.print(false);
assert!(result.is_ok());
}

Expand All @@ -807,7 +828,7 @@ mod tests {
}"#;

let info: StatusInfo = serde_json::from_str(data).unwrap();
let result = info.print();
let result = info.print(false);
assert!(result.is_ok());
}

Expand Down Expand Up @@ -936,7 +957,7 @@ mod tests {
}"#;

let info: SummaryInfo = serde_json::from_str(data).unwrap();
let result = info.print();
let result = info.print(false);
assert!(result.is_ok());
}
}
16 changes: 8 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ fn main() {
let opt = Options::parse();

match opt.command {
Command::ActiveMaintenance => MaintenanceInfo::print_activate(),
Command::AllIncidents => IncidentInfo::print_all(),
Command::AllScheduledMaintenances => MaintenanceInfo::print_all(),
Command::Component => ComponentInfo::print_info(),
Command::Status => StatusInfo::print_info(),
Command::Summary => SummaryInfo::print_info(),
Command::UnresolvedIncidents => IncidentInfo::print_unresolved(),
Command::UpcomingMaintenance => MaintenanceInfo::print_upcoming(),
Command::ActiveMaintenance { pager } => MaintenanceInfo::print_activate(pager),
Command::AllIncidents { pager } => IncidentInfo::print_all(pager),
Command::AllScheduledMaintenances { pager } => MaintenanceInfo::print_all(pager),
Command::Component { pager } => ComponentInfo::print_info(pager),
Command::Status { pager } => StatusInfo::print_info(pager),
Command::Summary { pager } => SummaryInfo::print_info(pager),
Command::UnresolvedIncidents { pager } => IncidentInfo::print_unresolved(pager),
Command::UpcomingMaintenance { pager } => MaintenanceInfo::print_upcoming(pager),
}
}
40 changes: 32 additions & 8 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,50 @@ pub struct Options {
#[derive(Debug, Subcommand)]
pub enum Command {
/// Gets a list of active maintenance.
ActiveMaintenance,
ActiveMaintenance {
#[clap(short, long, help = "If set the output will be displayed in a pager")]
pager: bool,
},

/// Gets a list of all incidents.
AllIncidents,
AllIncidents {
#[clap(short, long, help = "If set the output will be displayed in a pager")]
pager: bool,
},

/// Gets a list of the 50 most recent scheduled maintenances.
AllScheduledMaintenances,
AllScheduledMaintenances {
#[clap(short, long, help = "If set the output will be displayed in a pager")]
pager: bool,
},

/// Status of each component.
Component,
Component {
#[clap(short, long, help = "If set the output will be displayed in a pager")]
pager: bool,
},

/// Gets the current status
Status,
Status {
#[clap(short, long, help = "If set the output will be displayed in a pager")]
pager: bool,
},

/// Gets a summary for the current GitHub status.
Summary,
Summary {
#[clap(short, long, help = "If set the output will be displayed in a pager")]
pager: bool,
},

/// Gets a list of any unresolved incidents.
UnresolvedIncidents,
UnresolvedIncidents {
#[clap(short, long, help = "If set the output will be displayed in a pager")]
pager: bool,
},

/// Gets a list of upcoming maintenance
UpcomingMaintenance,
UpcomingMaintenance {
#[clap(short, long, help = "If set the output will be displayed in a pager")]
pager: bool,
},
}

0 comments on commit 18f375a

Please sign in to comment.