gandi-live-dns-rust/src/main.rs

61 lines
1.6 KiB
Rust
Raw Normal View History

2021-12-12 02:49:21 -06:00
use directories::ProjectDirs;
2021-12-12 00:36:40 -06:00
use serde::Deserialize;
use std::error::Error;
use std::fs;
2021-12-12 02:49:21 -06:00
use std::path::{Path, PathBuf};
use structopt::StructOpt;
/// A tool to automatically update DNS entries on Gandi, using it as a dynamic DNS system.
#[derive(StructOpt, Debug)]
#[structopt(name = "gandi-dynamic-dns")]
struct Opts {
/// The path to the configuration file.
#[structopt(long)]
config: Option<String>,
/// If set, it will only update the DNS once then exit.
#[structopt(long)]
oneshot: bool,
}
2021-12-12 00:36:40 -06:00
#[derive(Deserialize, Debug)]
struct Config {
fqdn: String,
}
2021-12-12 02:49:21 -06:00
fn gandi_api(fqdn: &str) -> String {
2021-12-12 00:36:40 -06:00
return format!("https://api.gandi.net/v5/livedns/domains/{}/records", fqdn);
}
2021-12-12 02:49:21 -06:00
fn load_config(file: PathBuf) -> Result<Config, Box<dyn Error>> {
let output = fs::read_to_string(file)?;
let contents = output.as_str();
2021-12-12 00:36:40 -06:00
let config = toml::from_str(contents)?;
return Ok(config);
}
2021-12-12 02:49:21 -06:00
fn config_path(opts: &Opts) -> PathBuf {
return opts
.config
.as_ref()
.and_then(|conf| Some(PathBuf::from(conf)))
.unwrap_or(
ProjectDirs::from("me", "kaangenc", "gandi-dynamic-dns")
.and_then(|dir| Some(PathBuf::from(dir.config_dir())))
.unwrap_or(PathBuf::from(".")),
);
}
fn main() -> Result<(), Box<dyn Error>> {
let opts = Opts::from_args();
println!("{:#?}", opts);
let conf_path = config_path(&opts);
println!("{:#?}", conf_path);
let conf = load_config(conf_path);
println!("{:#?}", conf);
2021-12-12 00:36:40 -06:00
println!("Hello, world!");
2021-12-12 02:49:21 -06:00
return Ok(());
2021-12-12 00:36:40 -06:00
}