Fixed config loading and default config generation
This commit is contained in:
parent
618779102b
commit
42c78a3a8b
7
.vscode/launch.json
vendored
Normal file
7
.vscode/launch.json
vendored
Normal file
@ -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": []
|
||||||
|
}
|
||||||
@ -1,6 +1,7 @@
|
|||||||
use ratatui::widgets::ListState;
|
use ratatui::widgets::ListState;
|
||||||
|
|
||||||
use crate::config::*;
|
use crate::config::*;
|
||||||
|
use crate::logger::*;
|
||||||
|
|
||||||
pub enum AppRenderDir {
|
pub enum AppRenderDir {
|
||||||
Vertical,
|
Vertical,
|
||||||
@ -37,7 +38,7 @@ impl App {
|
|||||||
render_direction: AppRenderDir::Vertical,
|
render_direction: AppRenderDir::Vertical,
|
||||||
current_state: CurrentState::Lookup,
|
current_state: CurrentState::Lookup,
|
||||||
menu_state: MenuState::Main,
|
menu_state: MenuState::Main,
|
||||||
config: Config::from_file("test.ini".to_string()),
|
config: Config::load(),
|
||||||
state: ListState::default(),
|
state: ListState::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
use configparser::ini::Ini;
|
use configparser::ini::Ini;
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
|
use std::env;
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub subdomains: Vec<String>,
|
pub subdomains: Vec<String>,
|
||||||
@ -20,8 +22,8 @@ impl fmt::Display for Config {
|
|||||||
impl Config {
|
impl Config {
|
||||||
pub fn from_file(file_path: String) -> Config {
|
pub fn from_file(file_path: String) -> Config {
|
||||||
let mut config = Ini::new();
|
let mut config = Ini::new();
|
||||||
config.load(file_path).unwrap();
|
match config.load(file_path) {
|
||||||
|
Ok(_) => {
|
||||||
let subdomains = config.get("General", "subdomains").unwrap();
|
let subdomains = config.get("General", "subdomains").unwrap();
|
||||||
let mut subvec: Vec<String> = vec![];
|
let mut subvec: Vec<String> = vec![];
|
||||||
|
|
||||||
@ -29,12 +31,63 @@ impl Config {
|
|||||||
subvec.push(x.to_string());
|
subvec.push(x.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
let wildcard_test = config.get("General", "wildcard_test");
|
let wildcard_test = config.get("General", "wildcard");
|
||||||
let datadir = config.get("General", "data_directory");
|
let datadir = config.get("General", "data_directory");
|
||||||
|
|
||||||
Config {
|
Config {
|
||||||
subdomains: subvec,
|
subdomains: subvec,
|
||||||
wildcard_test: wildcard_test.unwrap(),
|
wildcard_test: wildcard_test.unwrap(),
|
||||||
data_dir: datadir.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())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -37,7 +37,8 @@ impl DomainData {
|
|||||||
date.format("%Y-%m-%d_%H-%M-%S"),
|
date.format("%Y-%m-%d_%H-%M-%S"),
|
||||||
self.name
|
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 {
|
if data_dir != true {
|
||||||
match fs::create_dir_all(format!("{}/lookups", config.data_dir)) {
|
match fs::create_dir_all(format!("{}/lookups", config.data_dir)) {
|
||||||
Ok(_val) => {}
|
Ok(_val) => {}
|
||||||
|
|||||||
@ -39,9 +39,8 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
let backend = CrosstermBackend::new(stdout);
|
let backend = CrosstermBackend::new(stdout);
|
||||||
let mut terminal = Terminal::new(backend)?;
|
let mut terminal = Terminal::new(backend)?;
|
||||||
|
|
||||||
// Initialise the App and config
|
// Initialise the App
|
||||||
let mut app = App::new();
|
let mut app = App::new();
|
||||||
let config = Config::from_file("test.ini".to_string());
|
|
||||||
|
|
||||||
// Run the app
|
// Run the app
|
||||||
let _res = run_app(&mut terminal, &mut app);
|
let _res = run_app(&mut terminal, &mut app);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user