From 8632549a8e14938db686396a12876de57da7d4d0 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 9 May 2019 22:12:18 +0200 Subject: Refactoring, move arg parsing and sig handler out of main. --- src/main.rs | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 36c5cf9..bee16bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ // along with this program. If not, see . use phisher; -use clap::{clap_app, crate_name, crate_authors, crate_version, crate_description}; +use clap::{ArgMatches, clap_app, crate_name, crate_authors, crate_version, crate_description}; use std::boxed::Box; use std::error::Error; use std::fs::{ File, remove_file }; @@ -29,20 +29,8 @@ use std::thread; use std::time::SystemTime; fn main() { - // Set signal handler to clean up on termination. - ctrlc::set_handler(move || { - // We don't care about errors in remove_file. - remove_file("/tmp/phisher").unwrap_or(()); - exit(0); - }).expect("Couldn't set signal handler!"); - - let args = clap_app!(app => - (name: crate_name!()) - (version: crate_version!()) - (author: crate_authors!()) - (about: crate_description!()) - (@arg PHISHTANK: --phishtank +required +takes_value "The phishtank json data file to use.") - ).get_matches(); + let args = get_args(); + set_sig_handler(); let start_time = SystemTime::now(); let filename = args.value_of("PHISHTANK").unwrap(); @@ -63,6 +51,27 @@ fn main() { } } +fn get_args() -> ArgMatches<'static> { + clap_app!(app => + (name: crate_name!()) + (version: crate_version!()) + (author: crate_authors!()) + (about: crate_description!()) + (@arg PHISHTANK: --phishtank +required +takes_value + "The phishtank json data file to use.") + ).get_matches() +} + +// Set signal handler to clean up on termination. +// +fn set_sig_handler() { + ctrlc::set_handler(move || { + // We don't care about errors in remove_file. + remove_file("/tmp/phisher").unwrap_or(()); + exit(0); + }).expect("Couldn't set signal handler!"); +} + fn run_server(tank: Arc) -> Result<(), Box> { let bind_address = "/tmp/phisher"; let listener = UnixListener::bind(&bind_address)?; -- cgit v1.2.3