diff --git a/src/main.rs b/src/main.rs index ae8aed9..662931b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use futures; use ip_source::icanhazip::IPSourceIcanhazip; use reqwest::{header, Client, ClientBuilder, StatusCode}; use serde::Serialize; +use std::thread; use std::{num::NonZeroU32, sync::Arc, time::Duration}; use tokio::{self, task::JoinHandle}; mod config; @@ -42,7 +43,7 @@ pub struct APIPayload { pub rrset_ttl: u32, } -async fn run(base_url: &str, conf: Config) -> anyhow::Result<()> { +async fn run(base_url: &str, conf: &Config) -> anyhow::Result<()> { config::validate_config(&conf).die_with(|error| format!("Invalid config: {}", error)); println!("Finding out the IP address..."); let ipv4_result = IP::get_ipv4().await; @@ -124,6 +125,21 @@ async fn main() -> anyhow::Result<()> { let conf = config::load_config(&opts) .die_with(|error| format!("Failed to read config file: {}", error)); + // run indefinitely if repeat is given + if let Some(delay) = opts.repeat { + loop { + run_dispatch(&conf).await.ok(); + thread::sleep(Duration::from_secs(delay)) + } + } + // otherwise run just once + else { + run_dispatch(&conf).await?; + Ok(()) + } +} + +async fn run_dispatch(conf: &Config) -> anyhow::Result<()> { match conf.ip_source { IPSourceName::Ipify => run::("https://api.gandi.net", conf).await, IPSourceName::Icanhazip => run::("https://api.gandi.net", conf).await, @@ -182,7 +198,7 @@ mod tests { ..Opts::default() }; let conf = config::load_config(&opts).expect("Failed to load config"); - run::(server.base_url().as_str(), conf) + run::(server.base_url().as_str(), &conf) .await .expect("Failed when running the update"); diff --git a/src/opts.rs b/src/opts.rs index 9eb5e4d..93fb62e 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -17,4 +17,9 @@ pub struct Opts { /// If enabled, any IPv6 (AAAA) records in the configuration file are ignored. #[clap(action, long)] pub skip_ipv6: bool, + /// Repeat after specified delay + /// + /// If enabled waits for the given delay between updating DNS records + #[clap(long)] + pub repeat: Option, }