From 4a27892ab66df2a404c2d06d06db42b240322afc Mon Sep 17 00:00:00 2001 From: glyph Date: Wed, 12 Jan 2022 10:58:36 +0200 Subject: [PATCH] idiomatic paths and result type for checking new dns address --- peach-lib/src/dyndns_client.rs | 36 +++++++++++----------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/peach-lib/src/dyndns_client.rs b/peach-lib/src/dyndns_client.rs index c38b852..1e1c97f 100644 --- a/peach-lib/src/dyndns_client.rs +++ b/peach-lib/src/dyndns_client.rs @@ -9,14 +9,8 @@ //! //! The domain for dyndns updates is stored in /var/lib/peachcloud/config.yml //! The tsig key for authenticating the updates is stored in /var/lib/peachcloud/peach-dyndns/tsig.key -use std::{ - fs, - fs::OpenOptions, - io::Write, - process::{Command}, - str::FromStr, -}; use std::ffi::OsStr; +use std::{fs, fs::OpenOptions, io::Write, process::Command, str::FromStr}; use chrono::prelude::*; use jsonrpc_client_core::{expand_params, jsonrpc_client}; @@ -24,11 +18,8 @@ use jsonrpc_client_http::HttpTransport; use log::{debug, info}; use regex::Regex; -use crate::{ - config_manager::{load_peach_config, set_peach_dyndns_config}, - error::PeachError, -}; use crate::config_manager::get_dyndns_server_address; +use crate::{config_manager, error::PeachError}; /// constants for dyndns configuration pub const TSIG_KEY_PATH: &str = "/var/lib/peachcloud/peach-dyndns/tsig.key"; @@ -75,7 +66,8 @@ pub fn register_domain(domain: &str) -> std::result::Result // save new TSIG key save_dyndns_key(&key)?; // save new configuration values - let set_config_result = set_peach_dyndns_config(domain, &http_server, TSIG_KEY_PATH, true); + let set_config_result = + config_manager::set_peach_dyndns_config(domain, &http_server, TSIG_KEY_PATH, true); match set_config_result { Ok(_) => { let response = "success".to_string(); @@ -115,8 +107,7 @@ fn get_public_ip_address() -> Result { /// Reads dyndns configurations from config.yml /// and then uses nsupdate to update the IP address for the configured domain pub fn dyndns_update_ip() -> Result { - - let peach_config = load_peach_config()?; + let peach_config = config_manager::load_peach_config()?; info!( "Using config: dyn_tsig_key_path: {:?} @@ -226,18 +217,14 @@ pub fn get_num_seconds_since_successful_dns_update() -> Result, Peac /// and has successfully run recently (in the last six minutes) pub fn is_dns_updater_online() -> Result { // first check if it is enabled in peach-config - let peach_config = load_peach_config()?; + let peach_config = config_manager::load_peach_config()?; let is_enabled = peach_config.dyn_enabled; // then check if it has successfully run within the last 6 minutes (60*6 seconds) let num_seconds_since_successful_update = get_num_seconds_since_successful_dns_update()?; let ran_recently: bool = match num_seconds_since_successful_update { - Some(seconds) => { - seconds < (60 * 6) - } + Some(seconds) => seconds < (60 * 6), // if the value is None, then the last time it ran successfully is unknown - None => { - false - } + None => false, }; // debug log info!("is_dyndns_enabled: {:?}", is_enabled); @@ -260,11 +247,10 @@ pub fn get_dyndns_subdomain(dyndns_full_domain: &str) -> Option { } // helper function which checks if a dyndns domain is new -pub fn check_is_new_dyndns_domain(dyndns_full_domain: &str) -> bool { - // TODO: return `Result` and replace `unwrap` with `?` operator - let peach_config = load_peach_config().unwrap(); +pub fn check_is_new_dyndns_domain(dyndns_full_domain: &str) -> Result { + let peach_config = config_manager::load_peach_config()?; let previous_dyndns_domain = peach_config.dyn_domain; - dyndns_full_domain != previous_dyndns_domain + Ok(dyndns_full_domain != previous_dyndns_domain) } jsonrpc_client!(pub struct PeachDynDnsClient {