feat: Use anyhow for error handling
This commit is contained in:
parent
601445e973
commit
cfd5cb9da9
18
src/file.rs
18
src/file.rs
|
@ -1,12 +1,18 @@
|
||||||
use failure::Error;
|
use std::fs::File;
|
||||||
use quicli::fs::{write_to_file, read_file};
|
use std::io::prelude::*;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use anyhow::Result;
|
||||||
|
|
||||||
pub fn read_cache_file(path: &PathBuf) -> Result<String, Error> {
|
pub fn read_file(path: &PathBuf) -> Result<String> {
|
||||||
Ok(read_file(path)?)
|
let mut file = File::open(path)?;
|
||||||
|
let mut contents = String::new();
|
||||||
|
file.read_to_string(&mut contents)?;
|
||||||
|
|
||||||
|
Ok(contents)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_cache_file(path: &PathBuf, ip: &str) -> Result<(), Error> {
|
pub fn write_file(path: &PathBuf, ip: &str) -> Result<()> {
|
||||||
write_to_file(path, ip)?;
|
let mut file = File::create(path)?;
|
||||||
|
file.write_all(ip.as_bytes())?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
20
src/main.rs
20
src/main.rs
|
@ -1,13 +1,13 @@
|
||||||
mod file;
|
mod file;
|
||||||
mod network;
|
mod network;
|
||||||
|
|
||||||
use file::{read_cache_file, write_cache_file};
|
use file::{read_file, write_file};
|
||||||
use human_panic::setup_panic;
|
use human_panic::setup_panic;
|
||||||
use network::{get_current_ip, get_dns_record_id, get_zone_identifier, update_ddns};
|
use network::{get_current_ip, get_dns_record_id, get_zone_identifier, update_ddns};
|
||||||
use quicli::fs::read_file;
|
|
||||||
use quicli::prelude::*;
|
use quicli::prelude::*;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
use anyhow::{Context, Result};
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct Config {
|
struct Config {
|
||||||
|
@ -45,7 +45,7 @@ struct Cli {
|
||||||
cache: Option<PathBuf>,
|
cache: Option<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> CliResult {
|
fn main() -> Result<()> {
|
||||||
setup_panic!();
|
setup_panic!();
|
||||||
let args = Cli::from_args();
|
let args = Cli::from_args();
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ fn main() -> CliResult {
|
||||||
let cached_ip: Option<String> = match args.cache.clone() {
|
let cached_ip: Option<String> = match args.cache.clone() {
|
||||||
Some(v) => {
|
Some(v) => {
|
||||||
if v.exists() {
|
if v.exists() {
|
||||||
Some(read_cache_file(&v.clone())?)
|
Some(read_file(&v.clone()).context("Could not read cache file")?)
|
||||||
} else {
|
} else {
|
||||||
Some("0.0.0.0".to_owned())
|
Some("0.0.0.0".to_owned())
|
||||||
}
|
}
|
||||||
|
@ -73,12 +73,12 @@ fn main() -> CliResult {
|
||||||
¤t_ip,
|
¤t_ip,
|
||||||
&args.cache.clone().unwrap()
|
&args.cache.clone().unwrap()
|
||||||
);
|
);
|
||||||
write_cache_file(&args.cache.unwrap(), ¤t_ip)?;
|
write_file(&args.cache.unwrap(), ¤t_ip)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let (email, auth_key, zone, domain) = match args.config {
|
let (email, auth_key, zone, domain) = match args.config {
|
||||||
Some(c) => {
|
Some(c) => {
|
||||||
let config_str = read_file(c)?;
|
let config_str = read_file(&c)?;
|
||||||
let config: Config = toml::from_str(&config_str)?;
|
let config: Config = toml::from_str(&config_str)?;
|
||||||
(config.email, config.auth_key, config.zone, config.domain)
|
(config.email, config.auth_key, config.zone, config.domain)
|
||||||
}
|
}
|
||||||
|
@ -106,9 +106,9 @@ fn update(
|
||||||
auth_key: &str,
|
auth_key: &str,
|
||||||
zone: &str,
|
zone: &str,
|
||||||
domain: &str,
|
domain: &str,
|
||||||
) -> Result<(), Error> {
|
) -> Result<()> {
|
||||||
let zone_id = get_zone_identifier(&zone, &email, &auth_key)?;
|
let zone_id = get_zone_identifier(&zone, &email, &auth_key).context("Error getting the zone identifier")?;
|
||||||
let record_id = get_dns_record_id(&zone_id, &domain, &email, &auth_key)?;
|
let record_id = get_dns_record_id(&zone_id, &domain, &email, &auth_key).context("Error getting the DNS record ID")?;
|
||||||
|
|
||||||
update_ddns(
|
update_ddns(
|
||||||
¤t_ip,
|
¤t_ip,
|
||||||
|
@ -117,7 +117,7 @@ fn update(
|
||||||
&record_id,
|
&record_id,
|
||||||
&email,
|
&email,
|
||||||
&auth_key,
|
&auth_key,
|
||||||
)?;
|
).context("Error updating the DNS record")?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use failure::{format_err, Error};
|
|
||||||
use serde_derive::{Deserialize, Serialize};
|
use serde_derive::{Deserialize, Serialize};
|
||||||
|
use anyhow::{anyhow};
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
struct CloudflareListResponse {
|
struct CloudflareListResponse {
|
||||||
|
@ -28,7 +28,7 @@ struct UpdateIpData {
|
||||||
content: String,
|
content: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_zone_identifier(zone: &str, email: &str, key: &str) -> Result<String, Error> {
|
pub fn get_zone_identifier(zone: &str, email: &str, key: &str) -> anyhow::Result<String> {
|
||||||
let client = reqwest::blocking::Client::new();
|
let client = reqwest::blocking::Client::new();
|
||||||
let url = format!("https://api.cloudflare.com/client/v4/zones?name={}", zone);
|
let url = format!("https://api.cloudflare.com/client/v4/zones?name={}", zone);
|
||||||
let response: CloudflareListResponse = client
|
let response: CloudflareListResponse = client
|
||||||
|
@ -38,13 +38,14 @@ pub fn get_zone_identifier(zone: &str, email: &str, key: &str) -> Result<String,
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
.send()?
|
.send()?
|
||||||
.json()?;
|
.json()?;
|
||||||
|
|
||||||
if !response.success {
|
if !response.success {
|
||||||
let err: String = response
|
let err: String = response
|
||||||
.errors
|
.errors
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| format!("{}\n", s.to_owned()))
|
.map(|s| format!("{}\n", s.to_owned()))
|
||||||
.collect();
|
.collect();
|
||||||
return Err(format_err!("API Error: {}", err));
|
return Err(anyhow!("API Error: {}", err));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(response.result[0].id.clone())
|
Ok(response.result[0].id.clone())
|
||||||
|
@ -55,7 +56,7 @@ pub fn get_dns_record_id(
|
||||||
domain: &str,
|
domain: &str,
|
||||||
email: &str,
|
email: &str,
|
||||||
key: &str,
|
key: &str,
|
||||||
) -> Result<String, Error> {
|
) -> anyhow::Result<String> {
|
||||||
let client = reqwest::blocking::Client::new();
|
let client = reqwest::blocking::Client::new();
|
||||||
let url = format!(
|
let url = format!(
|
||||||
"https://api.cloudflare.com/client/v4/zones/{}/dns_records?name={}",
|
"https://api.cloudflare.com/client/v4/zones/{}/dns_records?name={}",
|
||||||
|
@ -68,19 +69,20 @@ pub fn get_dns_record_id(
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
.send()?
|
.send()?
|
||||||
.json()?;
|
.json()?;
|
||||||
|
|
||||||
if !response.success {
|
if !response.success {
|
||||||
let err: String = response
|
let err: String = response
|
||||||
.errors
|
.errors
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| format!("{}\n", s.to_owned()))
|
.map(|s| format!("{}\n", s.to_owned()))
|
||||||
.collect();
|
.collect();
|
||||||
return Err(format_err!("API Error: {}", err));
|
return Err(anyhow!("API Error: {}", err));
|
||||||
}
|
}
|
||||||
|
|
||||||
let id = match response.result.first() {
|
let id = match response.result.first() {
|
||||||
Some(v) => v.id.clone(),
|
Some(v) => v.id.clone(),
|
||||||
None => {
|
None => {
|
||||||
return Err(format_err!(
|
return Err(anyhow!(
|
||||||
"Unexpected API result for DNS record. Check if you passed the right options."
|
"Unexpected API result for DNS record. Check if you passed the right options."
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -89,7 +91,7 @@ pub fn get_dns_record_id(
|
||||||
Ok(id)
|
Ok(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_current_ip() -> Result<String, Error> {
|
pub fn get_current_ip() -> anyhow::Result<String> {
|
||||||
Ok(reqwest::blocking::Client::new()
|
Ok(reqwest::blocking::Client::new()
|
||||||
.get("http://ipv4.icanhazip.com")
|
.get("http://ipv4.icanhazip.com")
|
||||||
.send()?
|
.send()?
|
||||||
|
@ -105,7 +107,7 @@ pub fn update_ddns(
|
||||||
record_id: &str,
|
record_id: &str,
|
||||||
email: &str,
|
email: &str,
|
||||||
key: &str,
|
key: &str,
|
||||||
) -> Result<(), Error> {
|
) -> anyhow::Result<()> {
|
||||||
let client = reqwest::blocking::Client::new();
|
let client = reqwest::blocking::Client::new();
|
||||||
let url = format!(
|
let url = format!(
|
||||||
"https://api.cloudflare.com/client/v4/zones/{}/dns_records/{}",
|
"https://api.cloudflare.com/client/v4/zones/{}/dns_records/{}",
|
||||||
|
@ -134,7 +136,7 @@ pub fn update_ddns(
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| format!("{}\n", s.to_owned()))
|
.map(|s| format!("{}\n", s.to_owned()))
|
||||||
.collect();
|
.collect();
|
||||||
return Err(format_err!("Unsuccessful update of DNS record: {}", err));
|
return Err(anyhow!("Unsuccessful update of DNS record: {}", err));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue