mirror of
https://github.com/GyulyVGC/sniffnet.git
synced 2026-08-31 23:49:25 +00:00
handle CLI args before launching GUI
This commit is contained in:
parent
b80458e4b7
commit
47384192ee
2 changed files with 56 additions and 50 deletions
101
src/cli/mod.rs
101
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<String>,
|
||||
|
|
@ -29,61 +29,68 @@ struct Args {
|
|||
restore_default: bool,
|
||||
}
|
||||
|
||||
pub fn handle_cli_args() -> Task<Message> {
|
||||
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<Message> {
|
||||
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)]
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue