diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7c86574 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "cSpell.words": [ + "gandi", + "structopt" + ] +} \ No newline at end of file diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..aa9c99c --- /dev/null +++ b/src/config.rs @@ -0,0 +1,31 @@ +use directories::ProjectDirs; +use serde::Deserialize; +use std::error::Error; +use std::fs; +use std::path::PathBuf; +use crate::opts; + +#[derive(Deserialize, Debug)] +pub struct Config { + fqdn: String, +} + +pub fn load_config(file: PathBuf) -> Result> { + let output = fs::read_to_string(file)?; + let contents = output.as_str(); + + let config = toml::from_str(contents)?; + return Ok(config); +} + +pub fn config_path(opts: &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(".")), + ); +} diff --git a/src/main.rs b/src/main.rs index 77af389..2ceef41 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,58 +1,19 @@ -use directories::ProjectDirs; -use serde::Deserialize; -use std::error::Error; -use std::fs; -use std::path::{Path, PathBuf}; use structopt::StructOpt; +use std::error::Error; +mod opts; +mod config; -/// 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, - - /// If set, it will only update the DNS once then exit. - #[structopt(long)] - oneshot: bool, -} - -#[derive(Deserialize, Debug)] -struct Config { - fqdn: String, -} fn gandi_api(fqdn: &str) -> String { return format!("https://api.gandi.net/v5/livedns/domains/{}/records", fqdn); } -fn load_config(file: PathBuf) -> Result> { - let output = fs::read_to_string(file)?; - let contents = output.as_str(); - - let config = toml::from_str(contents)?; - return Ok(config); -} - -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> { - let opts = Opts::from_args(); + let opts = opts::Opts::from_args(); println!("{:#?}", opts); - let conf_path = config_path(&opts); + let conf_path = config::config_path(&opts); println!("{:#?}", conf_path); - let conf = load_config(conf_path); + let conf = config::load_config(conf_path); println!("{:#?}", conf); println!("Hello, world!"); diff --git a/src/opts.rs b/src/opts.rs new file mode 100644 index 0000000..252e68d --- /dev/null +++ b/src/opts.rs @@ -0,0 +1,14 @@ +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")] +pub struct Opts { + /// The path to the configuration file. + #[structopt(long)] + pub config: Option, + + /// If set, it will only update the DNS once then exit. + #[structopt(long)] + pub oneshot: bool, +}