config file and skeleton

This commit is contained in:
2024-07-13 14:26:55 +02:00
parent 9a3f6897ae
commit 1262510ab9
6 changed files with 172 additions and 0 deletions

0
src/cloudflare.rs Normal file
View File

48
src/config.rs Normal file
View File

@ -0,0 +1,48 @@
use dirs;
use log::warn;
use serde::{Deserialize, Serialize};
use std::{env, fs, path::PathBuf};
use crate::PROGRAM_NAME;
#[derive(Deserialize, Serialize, Debug)]
pub struct Config {
pub cloudflare_zone_id: Box<str>,
pub cloudflare_api_key: Box<str>,
}
pub fn get_config_path() -> Option<PathBuf> {
match env::current_dir() {
Ok(current_dir) => {
let cwd_config = current_dir.join(format!("{}.toml", PROGRAM_NAME));
if let Ok(meta) = fs::metadata(&cwd_config) {
if meta.is_file() {
return Some(cwd_config);
}
}
}
Err(e) => {
warn!("Failed to get current directory {:?}", &e);
}
}
match dirs::config_dir() {
Some(config_dir) => {
let config_file = config_dir.join(PROGRAM_NAME).join("config.toml");
if let Ok(meta) = fs::metadata(&config_file) {
if meta.is_file() {
return Some(config_file);
}
}
}
None => {}
}
None
}
pub fn read_config(path: PathBuf) -> Option<Config>{
None
}

6
src/lib.rs Normal file
View File

@ -0,0 +1,6 @@
mod cloudflare;
mod config;
pub use config::{Config, get_config_path};
pub const PROGRAM_NAME: &'static str = "dynip-cloudflare";

View File

@ -7,6 +7,8 @@ use netlink_packet_route::RouteNetlinkMessage as RtnlMessage;
use netlink_sys::{AsyncSocket, SocketAddr};
use rtnetlink::new_connection;
use dynip_cloudflare::{get_config_path, Config};
const RTNLGRP_LINK: u32 = 1;
const RTNLGRP_IPV4_IFADDR: u32 = 5;
@ -24,6 +26,14 @@ const fn nl_mgrp(group: u32) -> u32 {
#[tokio::main]
async fn main() -> Result<()> {
env_logger::init();
let config_path = match get_config_path() {
Some(cp) => cp,
None => {
error!("Failed to find any config file");
return Ok(());
}
};
let (mut conn, mut _handle, mut messages) = new_connection().map_err(|e| anyhow!(e))?;
let groups = nl_mgrp(RTNLGRP_LINK) | nl_mgrp(RTNLGRP_IPV4_IFADDR);