style: Format the code with cargo fmt
This commit is contained in:
parent
0a7148d62b
commit
39cc9223d2
|
@ -1,7 +1,7 @@
|
|||
use failure::Error;
|
||||
use std::fs::File;
|
||||
use std::path::PathBuf;
|
||||
use std::io::prelude::*;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub fn read_cache_file(path: &PathBuf) -> Result<String, Error> {
|
||||
let mut file = File::open(&path)?;
|
||||
|
@ -16,4 +16,4 @@ pub fn write_cache_file(path: &PathBuf, ip: &str) -> Result<(), Error> {
|
|||
file.write_all(ip.as_bytes())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
35
src/main.rs
35
src/main.rs
|
@ -1,12 +1,12 @@
|
|||
mod network;
|
||||
mod file;
|
||||
mod network;
|
||||
|
||||
use std::path::PathBuf;
|
||||
use quicli::prelude::*;
|
||||
use structopt::StructOpt;
|
||||
use human_panic::{setup_panic};
|
||||
use network::{get_zone_identifier, get_dns_record_id, get_current_ip, update_ddns};
|
||||
use file::{read_cache_file, write_cache_file};
|
||||
use human_panic::setup_panic;
|
||||
use network::{get_current_ip, get_dns_record_id, get_zone_identifier, update_ddns};
|
||||
use quicli::prelude::*;
|
||||
use std::path::PathBuf;
|
||||
use structopt::StructOpt;
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
/// Inform Cloudflare's DDNS service of the current IP address for your domain
|
||||
|
@ -45,11 +45,10 @@ fn main() -> CliResult {
|
|||
} else {
|
||||
Some("0.0.0.0".to_owned())
|
||||
}
|
||||
},
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
|
||||
|
||||
let current_ip = get_current_ip()?;
|
||||
if cached_ip.is_some() && current_ip == cached_ip.unwrap() {
|
||||
println!("IP is unchanged. Exiting...");
|
||||
|
@ -57,16 +56,30 @@ fn main() -> CliResult {
|
|||
}
|
||||
|
||||
if should_use_cache {
|
||||
println!("Saving current IP {} to cache file {:?}...", ¤t_ip, &args.cache.clone().unwrap());
|
||||
println!(
|
||||
"Saving current IP {} to cache file {:?}...",
|
||||
¤t_ip,
|
||||
&args.cache.clone().unwrap()
|
||||
);
|
||||
write_cache_file(&args.cache.unwrap(), ¤t_ip)?;
|
||||
}
|
||||
|
||||
let zone_id = get_zone_identifier(&args.zone, &args.email, &args.auth_key)?;
|
||||
let record_id = get_dns_record_id(&zone_id, &args.domain, &args.email, &args.auth_key)?;
|
||||
|
||||
update_ddns(¤t_ip, &args.domain, &zone_id, &record_id, &args.email, &args.auth_key)?;
|
||||
update_ddns(
|
||||
¤t_ip,
|
||||
&args.domain,
|
||||
&zone_id,
|
||||
&record_id,
|
||||
&args.email,
|
||||
&args.auth_key,
|
||||
)?;
|
||||
|
||||
println!("Successfully updated the A record for {} to {}", &args.domain, ¤t_ip);
|
||||
println!(
|
||||
"Successfully updated the A record for {} to {}",
|
||||
&args.domain, ¤t_ip
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use serde::{Serialize, Deserialize};
|
||||
use failure::{Error, format_err};
|
||||
use failure::{format_err, Error};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct CloudflareListResponse {
|
||||
|
@ -22,10 +22,10 @@ struct ObjectWithId {
|
|||
|
||||
#[derive(Serialize, Debug)]
|
||||
struct UpdateIpData {
|
||||
id: String,
|
||||
r#type: String,
|
||||
name: String,
|
||||
content: String,
|
||||
id: String,
|
||||
r#type: String,
|
||||
name: String,
|
||||
content: String,
|
||||
}
|
||||
|
||||
pub fn get_zone_identifier(zone: &str, email: &str, key: &str) -> Result<String, Error> {
|
||||
|
@ -38,18 +38,29 @@ pub fn get_zone_identifier(zone: &str, email: &str, key: &str) -> Result<String,
|
|||
.header("Content-Type", "application/json")
|
||||
.send()?
|
||||
.json()?;
|
||||
|
||||
if !response.success {
|
||||
let err: String = response.errors.iter().map(|s| format!("{}\n", s.to_owned())).collect();
|
||||
let err: String = response
|
||||
.errors
|
||||
.iter()
|
||||
.map(|s| format!("{}\n", s.to_owned()))
|
||||
.collect();
|
||||
return Err(format_err!("API Error: {}", err));
|
||||
}
|
||||
|
||||
Ok(response.result[0].id.clone())
|
||||
}
|
||||
|
||||
pub fn get_dns_record_id(zone_id: &str, domain: &str, email: &str, key: &str) -> Result<String, Error> {
|
||||
pub fn get_dns_record_id(
|
||||
zone_id: &str,
|
||||
domain: &str,
|
||||
email: &str,
|
||||
key: &str,
|
||||
) -> Result<String, Error> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("https://api.cloudflare.com/client/v4/zones/{}/dns_records?name={}", zone_id, domain);
|
||||
let url = format!(
|
||||
"https://api.cloudflare.com/client/v4/zones/{}/dns_records?name={}",
|
||||
zone_id, domain
|
||||
);
|
||||
let response: CloudflareListResponse = client
|
||||
.get(&url)
|
||||
.header("X-Auth-Email", email)
|
||||
|
@ -57,9 +68,12 @@ pub fn get_dns_record_id(zone_id: &str, domain: &str, email: &str, key: &str) ->
|
|||
.header("Content-Type", "application/json")
|
||||
.send()?
|
||||
.json()?;
|
||||
|
||||
if !response.success {
|
||||
let err: String = response.errors.iter().map(|s| format!("{}\n", s.to_owned())).collect();
|
||||
let err: String = response
|
||||
.errors
|
||||
.iter()
|
||||
.map(|s| format!("{}\n", s.to_owned()))
|
||||
.collect();
|
||||
return Err(format_err!("API Error: {}", err));
|
||||
}
|
||||
|
||||
|
@ -75,9 +89,19 @@ pub fn get_current_ip() -> Result<String, Error> {
|
|||
.into())
|
||||
}
|
||||
|
||||
pub fn update_ddns(ip: &str, domain: &str, zone_id: &str, record_id: &str, email: &str, key: &str) -> Result<(), Error> {
|
||||
pub fn update_ddns(
|
||||
ip: &str,
|
||||
domain: &str,
|
||||
zone_id: &str,
|
||||
record_id: &str,
|
||||
email: &str,
|
||||
key: &str,
|
||||
) -> Result<(), Error> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("https://api.cloudflare.com/client/v4/zones/{}/dns_records/{}", zone_id, record_id);
|
||||
let url = format!(
|
||||
"https://api.cloudflare.com/client/v4/zones/{}/dns_records/{}",
|
||||
zone_id, record_id
|
||||
);
|
||||
|
||||
let update_data = UpdateIpData {
|
||||
id: zone_id.to_owned(),
|
||||
|
@ -96,9 +120,13 @@ pub fn update_ddns(ip: &str, domain: &str, zone_id: &str, record_id: &str, email
|
|||
.json()?;
|
||||
|
||||
if !response.success {
|
||||
let err: String = response.errors.iter().map(|s| format!("{}\n", s.to_owned())).collect();
|
||||
let err: String = response
|
||||
.errors
|
||||
.iter()
|
||||
.map(|s| format!("{}\n", s.to_owned()))
|
||||
.collect();
|
||||
return Err(format_err!("Unsuccessful update of DNS record: {}", err));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue