From 347ad5c744e911e4ad3059d5d382bb4dad6d78a1 Mon Sep 17 00:00:00 2001 From: Trevor Merritt Date: Wed, 23 Apr 2025 16:16:46 -0400 Subject: [PATCH] Updates format of input file to CSV with , as the format. --- belleville.txt | 1 + hosts.txt | 0 lindsay.txt | 1 + peterborough.txt | 1 + src/app_settings.rs | 10 ++++++ src/bin/pp.rs | 21 +++++++------ src/bin/rat.rs | 9 ++++-- src/lib.rs | 1 + src/tui/ratatui_app.rs | 71 +++++++++++++++++++++++++++++------------- 9 files changed, 82 insertions(+), 33 deletions(-) delete mode 100644 hosts.txt create mode 100644 src/app_settings.rs diff --git a/belleville.txt b/belleville.txt index 8669809..57839c9 100644 --- a/belleville.txt +++ b/belleville.txt @@ -1,3 +1,4 @@ +address,name 10.3.100.1,Belleville Router 10.11.31.3,Belleville VPN 11-31 10.12.32.1,Belleville VPN 12-32 diff --git a/hosts.txt b/hosts.txt deleted file mode 100644 index e69de29..0000000 diff --git a/lindsay.txt b/lindsay.txt index 8669809..57839c9 100644 --- a/lindsay.txt +++ b/lindsay.txt @@ -1,3 +1,4 @@ +address,name 10.3.100.1,Belleville Router 10.11.31.3,Belleville VPN 11-31 10.12.32.1,Belleville VPN 12-32 diff --git a/peterborough.txt b/peterborough.txt index 8a5c0a7..8253f40 100644 --- a/peterborough.txt +++ b/peterborough.txt @@ -1,3 +1,4 @@ +address,name 10.3.100.1,Belleville Gateway 10.11.31.3,Belleville VPN 11-31 10.12.32.1,Belleville VPN 12-32 diff --git a/src/app_settings.rs b/src/app_settings.rs new file mode 100644 index 0000000..fb55ffe --- /dev/null +++ b/src/app_settings.rs @@ -0,0 +1,10 @@ +use std::path::PathBuf; +use clap::Parser; + +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +pub struct AppSettings { + /// File of list of hosts + #[arg(short, long)] + pub ping_host_file: Option, +} \ No newline at end of file diff --git a/src/bin/pp.rs b/src/bin/pp.rs index 79c08e5..e46afdb 100644 --- a/src/bin/pp.rs +++ b/src/bin/pp.rs @@ -17,6 +17,7 @@ use std::{env, error::Error, ffi::OsString, process}; use color_eyre::owo_colors::OwoColorize; use crossterm::style::Stylize; use log::debug; +use pp::app_settings::AppSettings; const SECONDS_IN_MINUTE: u32 = 60; const SECONDS_IN_HOUR: u32 = SECONDS_IN_MINUTE * 60; @@ -119,7 +120,7 @@ impl PPState { } pub fn build_targets_from_file(filename: Option) -> BTreeMap { - PPState::get_default_targets(); + // PPState::get_default_targets(); if let Some(file) = filename { let mut working = BTreeMap::new(); if !&file.exists() { @@ -157,20 +158,22 @@ impl PPState { } /// Simple program to greet a person - #[derive(Parser, Debug)] - #[command(version, about, long_about = None)] - pub struct AppSettings { - /// File of list of hosts - #[arg(short, long)] - ping_host_file: Option, - } + fn main() { // Get App Settings let settings = AppSettings::parse(); print!("Prep to load targets..."); - let mut targets = PPState::build_targets_from_file(settings.ping_host_file); + let file_to_check = match settings.ping_host_file { + None => { + PathBuf::from("./hosts.txt") + } + Some(actual) => { + actual + } + }; + let mut targets = PPState::build_targets_from_file(Some(file_to_check)); // channel to send requests to ping let (ping_response_sender, ping_response_listener) = mpsc::channel::(); diff --git a/src/bin/rat.rs b/src/bin/rat.rs index a83bdeb..0c8ce07 100644 --- a/src/bin/rat.rs +++ b/src/bin/rat.rs @@ -1,9 +1,14 @@ - +use clap::Parser; +use pp::app_settings::AppSettings; use pp::tui::ratatui_app::RatatuiApp; fn main() -> color_eyre::Result<()> { + // find out what file we are using to get our hosts + let settings = AppSettings::parse(); + + color_eyre::install()?; let terminal = ratatui::init(); - let result = RatatuiApp::new().run(terminal); + let result = RatatuiApp::new(settings.ping_host_file).run(terminal); ratatui::restore(); result } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 355c4ba..01e69c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ pub mod ping_request; pub mod ping_result; pub mod target_state; pub mod tui; +pub mod app_settings; pub const SECONDS_BETWEEN_DISPLAY: u32 = 1; pub const SECONDS_BETWEEN_PING: u32 = 2; diff --git a/src/tui/ratatui_app.rs b/src/tui/ratatui_app.rs index 61748e3..30782d8 100644 --- a/src/tui/ratatui_app.rs +++ b/src/tui/ratatui_app.rs @@ -1,6 +1,9 @@ use color_eyre::Result; use std::collections::BTreeMap; +use std::fs::File; use std::net::Ipv4Addr; +use std::path::PathBuf; +use std::str::FromStr; use std::sync::mpsc; use std::sync::mpsc::Receiver; use std::time::{Duration, SystemTime}; @@ -61,22 +64,6 @@ pub struct RatatuiApp { } impl RatatuiApp { - pub fn setup_default_hosts(&mut self) { - self.state.insert( - "Test Host 1".to_string(), - TargetState { - target: Ipv4Addr::new(127, 0, 0, 1), - name: "Localhost".to_string(), - ..TargetState::default() - }, - ); - - self.state.insert( - "Test Host 2".to_string(), - TargetState { target: Ipv4Addr::new(8, 8, 8, 8), name: "Google".to_string(), ..TargetState::default() }, - ); - } - pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> { self.running = true; // start the 'manager' thread that spawns its ping threads as needed @@ -140,7 +127,7 @@ impl RatatuiApp { }; working = format!("{}\n{} ({}) - {} / {} / {}", working, - current.name, + color_name, current.target, current.alive, current.last_rtt, @@ -175,7 +162,6 @@ impl RatatuiApp { fn quit(&mut self) { self.running = false; } - /// Handles the key events and updates the state of [`App`]. fn on_key_event(&mut self, key: KeyEvent) { match (key.modifiers, key.code) { @@ -187,10 +173,51 @@ impl RatatuiApp { } } - - pub fn new() -> Self { + pub fn new(option: Option) -> Self { + let targets = if let Some(file) = option { + let mut working = BTreeMap::new(); + if !&file.exists() { + RatatuiApp::get_default_targets() + } else { + let real_file = File::open(file); + let mut rdr = csv::Reader::from_reader(real_file.unwrap()); + for result in rdr.records() { + let record = result.unwrap(); + working.insert(record[1].to_string(), + TargetState { + name: record[1].to_string(), + target: Ipv4Addr::from_str(&record[0]).unwrap(), + alive: false, + last_alive_change: SystemTime::now(), + last_rtt: 0 + }); + } + working + } + } else { RatatuiApp::get_default_targets() }; let mut working = Self::default(); - working.setup_default_hosts(); + working.state = targets; working } -} \ No newline at end of file + + fn get_default_targets() -> BTreeMap { + let mut working = BTreeMap::new(); + + working.insert("1111 DNS".to_string(), TargetState { + name: "1111 DNS".to_string(), + target: Ipv4Addr::new(1,1,1,1), + ..TargetState::default() + }); + working.insert("Google DNS".to_string(), TargetState { + name: "Google DNS".to_string(), + target: Ipv4Addr::new(8,8,8,8), + ..TargetState::default() + }); + working.insert("Test Site 1".to_string(), TargetState { + name: "Test Site 1".to_string(), + target: Ipv4Addr::new(216,234,202,122), + ..TargetState::default() + }); + working + } +}