diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..5c7247b --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,7 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [] +} \ No newline at end of file diff --git a/src/app.rs b/src/app.rs index ea4bbc1..6556f3c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,6 +1,7 @@ use ratatui::widgets::ListState; use crate::config::*; +use crate::logger::*; pub enum AppRenderDir { Vertical, @@ -37,7 +38,7 @@ impl App { render_direction: AppRenderDir::Vertical, current_state: CurrentState::Lookup, menu_state: MenuState::Main, - config: Config::from_file("test.ini".to_string()), + config: Config::load(), state: ListState::default(), } } diff --git a/src/config.rs b/src/config.rs index 421381e..479e532 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,7 @@ use configparser::ini::Ini; use core::fmt; +use std::env; +use std::fs; pub struct Config { pub subdomains: Vec, @@ -20,21 +22,72 @@ impl fmt::Display for Config { impl Config { pub fn from_file(file_path: String) -> Config { let mut config = Ini::new(); - config.load(file_path).unwrap(); + match config.load(file_path) { + Ok(_) => { + let subdomains = config.get("General", "subdomains").unwrap(); + let mut subvec: Vec = vec![]; - let subdomains = config.get("General", "subdomains").unwrap(); - let mut subvec: Vec = vec![]; + for x in subdomains.split(',') { + subvec.push(x.to_string()); + } - for x in subdomains.split(',') { - subvec.push(x.to_string()); - } + let wildcard_test = config.get("General", "wildcard"); + let datadir = config.get("General", "data_directory"); - let wildcard_test = config.get("General", "wildcard_test"); - let datadir = config.get("General", "data_directory"); - Config { - subdomains: subvec, - wildcard_test: wildcard_test.unwrap(), - data_dir: datadir.unwrap(), + Config { + subdomains: subvec, + wildcard_test: wildcard_test.unwrap(), + data_dir: datadir.unwrap(), + } + } + Err(err) => { + println!("{:?}", err); + Config::new() + } } } + + pub fn new() -> Config { + let mut config = Ini::new(); + let mut default_path = env::var_os("HOME").unwrap(); + default_path.push("/.config/dnslookup"); + + // Create the directories if they don't exist + match fs::create_dir_all(&default_path) { + Ok(_) => {} + Err(err) => { + println!("Unable to create directories, got {}", err) + } + } + + config.set( + "General", + "wildcard", + Some(String::from("dfjgnkdfjngkdfngjkd")), + ); + config.set( + "General", + "subdomains", + Some(String::from("www,ftp,mail,files")), + ); + config.set( + "General", + "data_directory", + Some(String::from(default_path.to_str().unwrap())), + ); + + let mut conf_path = default_path.clone(); + conf_path.push("/config.ini"); + println!("{:?}", conf_path); + config.write(&conf_path.clone()).unwrap(); + Config::from_file(conf_path.into_string().unwrap()) + } + + pub fn load() -> Config { + // Default path is $HOME/.config/dnslookup/config.ini + let mut conf_path = env::var_os("HOME").unwrap(); + conf_path.push("/.config/dnslookup/config.ini"); + + Config::from_file(conf_path.into_string().unwrap()) + } } diff --git a/src/fsutil.rs b/src/fsutil.rs index f2cf425..5f63650 100644 --- a/src/fsutil.rs +++ b/src/fsutil.rs @@ -37,7 +37,8 @@ impl DomainData { date.format("%Y-%m-%d_%H-%M-%S"), self.name ); - let data_dir = Path::new(&config.data_dir.clone()).exists(); + let path = format!("{}/lookups", config.data_dir); + let data_dir = Path::new(&path).exists(); if data_dir != true { match fs::create_dir_all(format!("{}/lookups", config.data_dir)) { Ok(_val) => {} diff --git a/src/main.rs b/src/main.rs index c9a1881..8e5b6b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,9 +39,8 @@ fn main() -> Result<(), Box> { let backend = CrosstermBackend::new(stdout); let mut terminal = Terminal::new(backend)?; - // Initialise the App and config + // Initialise the App let mut app = App::new(); - let config = Config::from_file("test.ini".to_string()); // Run the app let _res = run_app(&mut terminal, &mut app);