Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of cli commands #3

Merged
merged 4 commits into from
Feb 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 30 additions & 41 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -21,67 +22,55 @@ 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<String>, host: Option<String>) -> ExitCode {
fn handle_command(
token: Option<String>,
host: Option<String>,
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);

match res {
Ok(_) => print_success("PiHole enabled successfully!"),
Err(err) => {
print_pihole_error(err);
return ExitCode::Error;
match cmd_func(&config) {
Ok(_) => {
print_success(&format!("PiHole {}d successfully!", cmd_name));
ExitCode::Ok
}
}
}
Err(err) => {
print_error(&format!(
"{}\nUse `phs enable [token]` or set PIHOLE_TOKEN environment variable",
err
));

return ExitCode::Error;
}
}

ExitCode::Ok
}

fn handle_disable(token: Option<String>, host: Option<String>, time: Option<u64>) -> 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!"),
Err(err) => {
print_pihole_error(err);
return ExitCode::Error;
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",
err, cmd_name
));

return ExitCode::Error;
ExitCode::Error
}
}

ExitCode::Ok
}

fn build_pihole_config(token: String, host: Option<String>) -> PiHoleConfig {
Expand Down
2 changes: 1 addition & 1 deletion src/pihole/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down