mirror of
https://github.com/SeriousBug/gandi-live-dns-rust
synced 2024-12-27 23:59:56 -06:00
organize into folders
This commit is contained in:
parent
68d992748e
commit
cfe229234e
6
.vscode/settings.json
vendored
Normal file
6
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"cSpell.words": [
|
||||||
|
"gandi",
|
||||||
|
"structopt"
|
||||||
|
]
|
||||||
|
}
|
31
src/config.rs
Normal file
31
src/config.rs
Normal file
|
@ -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<Config, Box<dyn Error>> {
|
||||||
|
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(".")),
|
||||||
|
);
|
||||||
|
}
|
51
src/main.rs
51
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 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<String>,
|
|
||||||
|
|
||||||
/// 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 {
|
fn gandi_api(fqdn: &str) -> String {
|
||||||
return format!("https://api.gandi.net/v5/livedns/domains/{}/records", fqdn);
|
return format!("https://api.gandi.net/v5/livedns/domains/{}/records", fqdn);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_config(file: PathBuf) -> Result<Config, Box<dyn Error>> {
|
|
||||||
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<dyn Error>> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let opts = Opts::from_args();
|
let opts = opts::Opts::from_args();
|
||||||
println!("{:#?}", opts);
|
println!("{:#?}", opts);
|
||||||
let conf_path = config_path(&opts);
|
let conf_path = config::config_path(&opts);
|
||||||
println!("{:#?}", conf_path);
|
println!("{:#?}", conf_path);
|
||||||
let conf = load_config(conf_path);
|
let conf = config::load_config(conf_path);
|
||||||
println!("{:#?}", conf);
|
println!("{:#?}", conf);
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
|
|
||||||
|
|
14
src/opts.rs
Normal file
14
src/opts.rs
Normal file
|
@ -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<String>,
|
||||||
|
|
||||||
|
/// If set, it will only update the DNS once then exit.
|
||||||
|
#[structopt(long)]
|
||||||
|
pub oneshot: bool,
|
||||||
|
}
|
Loading…
Reference in a new issue