From 8e5e4bc91cb783cffccff788de84ea0787d12b65 Mon Sep 17 00:00:00 2001 From: "Frank A. Stevenson" Date: Sun, 31 Jan 2021 15:43:16 +0100 Subject: [PATCH 1/4] Remove some return statements. --- src/main.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index a6e98fe..fa6fba2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,10 +36,13 @@ fn handle_enable(token: Option, host: Option) -> ExitCode { let res = pihole::enable(&config); match res { - Ok(_) => print_success("PiHole enabled successfully!"), + Ok(_) => { + print_success("PiHole enabled successfully!"); + ExitCode::Ok + } Err(err) => { print_pihole_error(err); - return ExitCode::Error; + ExitCode::Error } } } @@ -49,11 +52,9 @@ fn handle_enable(token: Option, host: Option) -> ExitCode { err )); - return ExitCode::Error; + ExitCode::Error } } - - ExitCode::Ok } fn handle_disable(token: Option, host: Option, time: Option) -> ExitCode { @@ -64,10 +65,13 @@ fn handle_disable(token: Option, host: Option, time: Option let res = pihole::disable(&config, disable_time); match res { - Ok(_) => print_success("PiHole disabled successfully!"), + Ok(_) => { + print_success("PiHole disabled successfully!"); + ExitCode::Ok + } Err(err) => { print_pihole_error(err); - return ExitCode::Error; + ExitCode::Error } } } @@ -77,11 +81,9 @@ fn handle_disable(token: Option, host: Option, time: Option err )); - return ExitCode::Error; + ExitCode::Error } } - - ExitCode::Ok } fn build_pihole_config(token: String, host: Option) -> PiHoleConfig { From 697035d997620efd7caa83b9abf07cef04605289 Mon Sep 17 00:00:00 2001 From: "Frank A. Stevenson" Date: Sun, 31 Jan 2021 16:03:37 +0100 Subject: [PATCH 2/4] Make a more generic handle_command(...) function to reduce copypasta --- src/main.rs | 62 ++++++++++++++++++++--------------------------- src/pihole/mod.rs | 2 +- 2 files changed, 27 insertions(+), 37 deletions(-) diff --git a/src/main.rs b/src/main.rs index fa6fba2..6782798 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use structopt::StructOpt; use pihole_switch::pihole; use pihole_switch::pihole::config::{PiHoleConfig, PIHOLE_DEFAULT_HOST}; use pihole_switch::pihole::disable_time::PiHoleDisableTime; +use pihole_switch::pihole::PiholeResult; use pihole_switch::resolve_api_token::resolve_api_token; use crate::cli::io::{print_error, print_pihole_error, print_success}; @@ -21,23 +22,41 @@ fn main() { let host = args.host; let exit_code = match args.cmd { - Command::Enable { token } => handle_enable(token, host), - Command::Disable { token, time } => handle_disable(token, host, time), + Command::Enable { token } => handle_command( + token, + host, + &|conf: &PiHoleConfig| pihole::enable(conf), + &"enable", + ), + Command::Disable { token, time } => handle_command( + token, + host, + &|conf: &PiHoleConfig| { + let disable_time = PiHoleDisableTime::from_secs(time); + pihole::disable(conf, disable_time) + }, + &"disable", + ), }; std::process::exit(exit_code as i32); } -fn handle_enable(token: Option, host: Option) -> ExitCode { +fn handle_command( + token: Option, + host: Option, + cmd_func: &dyn Fn(&PiHoleConfig) -> PiholeResult, + cmd_name: &str, +) -> ExitCode { match resolve_api_token(token) { Ok(token) => { let config = build_pihole_config(token, host); - let res = pihole::enable(&config); + let res = cmd_func(&config); match res { Ok(_) => { - print_success("PiHole enabled successfully!"); + print_success(&format!("PiHole {}d successfully!", cmd_name)); ExitCode::Ok } Err(err) => { @@ -48,37 +67,8 @@ fn handle_enable(token: Option, host: Option) -> ExitCode { } Err(err) => { print_error(&format!( - "{}\nUse `phs enable [token]` or set PIHOLE_TOKEN environment variable", - err - )); - - ExitCode::Error - } - } -} - -fn handle_disable(token: Option, host: Option, time: Option) -> ExitCode { - match resolve_api_token(token) { - Ok(token) => { - let config = build_pihole_config(token, host); - let disable_time = PiHoleDisableTime::from_secs(time); - let res = pihole::disable(&config, disable_time); - - match res { - Ok(_) => { - print_success("PiHole disabled successfully!"); - ExitCode::Ok - } - Err(err) => { - print_pihole_error(err); - ExitCode::Error - } - } - } - Err(err) => { - print_error(&format!( - "{}\nUse `phs disable [token]` or set PIHOLE_TOKEN environment variable", - err + "{}\nUse `phs {} [token]` or set PIHOLE_TOKEN environment variable", + cmd_name, err )); ExitCode::Error diff --git a/src/pihole/mod.rs b/src/pihole/mod.rs index 4f3c7f3..a3941a9 100644 --- a/src/pihole/mod.rs +++ b/src/pihole/mod.rs @@ -23,7 +23,7 @@ impl ExpectedStatus { } } -type PiholeResult = Result<(), PiHoleError>; +pub type PiholeResult = Result<(), PiHoleError>; pub fn enable(config: &PiHoleConfig) -> PiholeResult { let url = format!("{}?enable&auth={}", &config.api_url, &config.api_token); From 57be5cee838e6d7c48cc718b8659a78c9c6bbba3 Mon Sep 17 00:00:00 2001 From: "Frank A. Stevenson" Date: Sun, 31 Jan 2021 16:07:49 +0100 Subject: [PATCH 3/4] Match on return value without assigning first --- src/main.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6782798..b59833a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,10 +51,7 @@ fn handle_command( match resolve_api_token(token) { Ok(token) => { let config = build_pihole_config(token, host); - - let res = cmd_func(&config); - - match res { + match cmd_func(&config) { Ok(_) => { print_success(&format!("PiHole {}d successfully!", cmd_name)); ExitCode::Ok From 3f7bf32ca825a2501f60e62e3a29e1cbd47085b8 Mon Sep 17 00:00:00 2001 From: devmatteini Date: Sat, 6 Feb 2021 17:47:28 +0100 Subject: [PATCH 4/4] cli: fix wrong order of parameters when formatting api token error --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index b59833a..8e5849c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,7 +65,7 @@ fn handle_command( Err(err) => { print_error(&format!( "{}\nUse `phs {} [token]` or set PIHOLE_TOKEN environment variable", - cmd_name, err + err, cmd_name )); ExitCode::Error