organize into folders

This commit is contained in:
Kaan Genc 2021-12-12 04:32:05 -05:00
parent 68d992748e
commit cfe229234e
4 changed files with 57 additions and 45 deletions

6
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,6 @@
{
"cSpell.words": [
"gandi",
"structopt"
]
}

31
src/config.rs Normal file
View 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(".")),
);
}

View file

@ -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<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 {
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>> {
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!");

14
src/opts.rs Normal file
View 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,
}