fix type issues with tasks

This commit is contained in:
Kaan Genc 2022-01-23 03:22:41 -05:00
parent 093b99d69d
commit c5ed8fe6ab
3 changed files with 40 additions and 15 deletions

7
Cargo.lock generated
View file

@ -11,6 +11,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "anyhow"
version = "1.0.53"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94a45b455c14666b85fc40a019e8ab9eb75e3a124e05494f5397122bc9eb06e0"
[[package]] [[package]]
name = "atty" name = "atty"
version = "0.2.14" version = "0.2.14"
@ -248,6 +254,7 @@ dependencies = [
name = "gandi-rust-dns-updater" name = "gandi-rust-dns-updater"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"directories", "directories",
"futures", "futures",
"json", "json",

View file

@ -15,3 +15,4 @@ directories = "4.0.1"
structopt = "0.3.25" structopt = "0.3.25"
tokio = { version = "1.14.0", features = ["full"] } tokio = { version = "1.14.0", features = ["full"] }
futures = "0.3.17" futures = "0.3.17"
anyhow = "1.0"

View file

@ -1,10 +1,10 @@
use std::collections::HashMap;
use crate::config::Config; use crate::config::Config;
use reqwest::{header, Client, ClientBuilder}; use anyhow;
use std::error::Error;
use structopt::StructOpt;
use tokio;
use futures; use futures;
use reqwest::{header, Client, ClientBuilder, StatusCode};
use std::collections::HashMap;
use structopt::StructOpt;
use tokio::{self, task::JoinHandle};
mod config; mod config;
mod opts; mod opts;
@ -13,10 +13,13 @@ fn gandi_api_get(fqdn: &str) -> String {
} }
fn gandi_api_url(fqdn: &str, rrset_name: &str, rrset_type: &str) -> String { fn gandi_api_url(fqdn: &str, rrset_name: &str, rrset_type: &str) -> String {
return format!(" https://api.gandi.net/v5/livedns/domains/{}/records/{}/{}", fqdn, rrset_name, rrset_type); return format!(
" https://api.gandi.net/v5/livedns/domains/{}/records/{}/{}",
fqdn, rrset_name, rrset_type
);
} }
fn api_client(api_key: &str) -> Result<Client, Box<dyn Error>> { fn api_client(api_key: &str) -> anyhow::Result<Client> {
let client_builder = ClientBuilder::new(); let client_builder = ClientBuilder::new();
let key = format!("Apikey {}", api_key); let key = format!("Apikey {}", api_key);
@ -31,7 +34,7 @@ fn api_client(api_key: &str) -> Result<Client, Box<dyn Error>> {
} }
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> { async fn main() -> anyhow::Result<()> {
let opts = opts::Opts::from_args(); let opts = opts::Opts::from_args();
let conf_path = config::config_path(&opts); let conf_path = config::config_path(&opts);
println!("Loading config from {:#?}", conf_path); println!("Loading config from {:#?}", conf_path);
@ -43,19 +46,33 @@ async fn main() -> Result<(), Box<dyn Error>> {
let ipv4 = String::from("173.89.215.91"); let ipv4 = String::from("173.89.215.91");
let ipv6 = String::from("2603:6011:be07:302:79f4:50dd:6abe:be38"); let ipv6 = String::from("2603:6011:be07:302:79f4:50dd:6abe:be38");
let mut results = Vec::new(); let mut results: Vec<JoinHandle<(StatusCode, String)>> = Vec::new();
for entry in &conf.entry { for entry in &conf.entry {
for entry_type in Config::types(entry) { for entry_type in Config::types(entry) {
let fqdn = Config::fqdn(&entry, &conf); let fqdn = Config::fqdn(&entry, &conf);
let url = gandi_api_url(fqdn, entry.name.as_str(), entry_type); let url = gandi_api_url(fqdn, entry.name.as_str(), entry_type);
let ip = if entry_type.eq("A") { ipv4.as_str() } else { ipv6.as_str() }; let ip = if entry_type.eq("A") {
ipv4.as_str()
} else {
ipv6.as_str()
};
let mut map = HashMap::new(); let mut map = HashMap::new();
map.insert("rrset_values", ip); map.insert("rrset_values", ip);
let req = client.put(url).json(&map); let req = client.put(url).json(&map);
let task = tokio::task::spawn(async move { let task = tokio::task::spawn(async move {
let response = req.send().await?; match req.send().await {
return (response.status(), response.text().await?); Ok(response) => (
response.status(),
response
.text()
.await
.unwrap_or_else(|error| error.to_string()),
),
Err(error) => (
StatusCode::IM_A_TEAPOT, error.to_string()
),
}
}); });
results.push(task); results.push(task);
} }