From 47384192ee9d734927a179c49fb5bdd2bcbfee5c Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Sat, 24 Jan 2026 22:28:18 +0100 Subject: [PATCH] handle CLI args before launching GUI --- src/cli/mod.rs | 101 ++++++++++++++++++++++++++----------------------- src/main.rs | 5 +-- 2 files changed, 56 insertions(+), 50 deletions(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index ce2eaf62..4a0e32e5 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -13,7 +13,7 @@ use iced::{Task, window}; version = APP_VERSION, about = "Application to comfortably monitor your network traffic" )] -struct Args { +pub(crate) struct Args { /// Start sniffing packets from the supplied network adapter #[arg(short, long, value_name = "NAME", default_missing_value = CONF.device.device_name.as_str(), num_args = 0..=1)] adapter: Option, @@ -29,61 +29,68 @@ struct Args { restore_default: bool, } -pub fn handle_cli_args() -> Task { - let args = Args::parse(); +impl Args { + /// Handle and return CLI arguments + pub fn handle() -> Self { + let args = Args::parse(); - #[cfg(all(windows, not(debug_assertions)))] - if let Some(logs_file) = crate::utils::formatted_strings::get_logs_file_path() { - if args.logs { - std::process::Command::new("explorer") - .arg(logs_file) - .spawn() - .unwrap() - .wait() - .unwrap_or_default(); - std::process::exit(0); - } else { - // truncate logs file - let _ = std::fs::OpenOptions::new() - .write(true) - .truncate(true) - .open(logs_file); + #[cfg(all(windows, not(debug_assertions)))] + if let Some(logs_file) = crate::utils::formatted_strings::get_logs_file_path() { + if args.logs { + std::process::Command::new("explorer") + .arg(logs_file) + .spawn() + .unwrap() + .wait() + .unwrap_or_default(); + std::process::exit(0); + } else { + // truncate logs file + let _ = std::fs::OpenOptions::new() + .write(true) + .truncate(true) + .open(logs_file); + } } - } - if args.restore_default { - if Conf::default().store().is_ok() { - println!("Restored default settings"); - std::process::exit(0); - } else { - eprintln!("Could not restore default settings"); - std::process::exit(1); + if args.restore_default { + if Conf::default().store().is_ok() { + println!("Restored default settings"); + std::process::exit(0); + } else { + eprintln!("Could not restore default settings"); + std::process::exit(1); + } } - } - if args.config_path { - if let Ok(config_path) = - confy::get_configuration_file_path(SNIFFNET_LOWERCASE, Conf::FILE_NAME) - { - println!("Configuration file path: {}", config_path.display()); - std::process::exit(0); - } else { - eprintln!("Could not retrieve configuration file path"); - std::process::exit(1); + if args.config_path { + if let Ok(config_path) = + confy::get_configuration_file_path(SNIFFNET_LOWERCASE, Conf::FILE_NAME) + { + println!("{}", config_path.display()); + std::process::exit(0); + } else { + eprintln!("Could not retrieve configuration file path"); + std::process::exit(1); + } } + + args } - let mut boot_task_chain = window::latest().map(Message::StartApp); - if let Some(adapter) = args.adapter { - boot_task_chain = boot_task_chain - .chain(Task::done(Message::SetCaptureSource( - CaptureSourcePicklist::Device, - ))) - .chain(Task::done(Message::DeviceSelection(adapter))) - .chain(Task::done(Message::Start)); - } + pub fn get_boot_task_chain(&self) -> Task { + let mut boot_task_chain = window::latest().map(Message::StartApp); + if let Some(adapter) = self.adapter.clone() { + boot_task_chain = boot_task_chain + .chain(Task::done(Message::SetCaptureSource( + CaptureSourcePicklist::Device, + ))) + .chain(Task::done(Message::DeviceSelection(adapter))) + .chain(Task::done(Message::Start)); + } - boot_task_chain + boot_task_chain + } } #[cfg(test)] diff --git a/src/main.rs b/src/main.rs index 75718339..0115dccd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,6 @@ use std::borrow::Cow; use chart::types::traffic_chart::TrafficChart; -use cli::handle_cli_args; use gui::pages::types::running_page::RunningPage; use gui::sniffer::Sniffer; use gui::styles::style_constants::FONT_SIZE_BODY; @@ -56,6 +55,7 @@ pub fn main() -> iced::Result { } let conf = CONF.clone(); + let args = cli::Args::handle(); #[cfg(debug_assertions)] { @@ -73,9 +73,8 @@ pub fn main() -> iced::Result { let size = conf.window.size(); let position = Position::Specific(conf.window.position()); - // TODO: parse CLI args before launching GUI application( - move || (Sniffer::new(conf.clone()), handle_cli_args()), + move || (Sniffer::new(conf.clone()), args.get_boot_task_chain()), Sniffer::update, Sniffer::view, )