Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve Issue 15 #16

Merged
merged 7 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ fn main() -> Result<(), Box<dyn Error>> {

match args.command {
Commands::Init { config } => {
log_info!("Loading from config file '{}'...", config);
let settings = DnsSettings::load(config.clone())?;
log_info!("Loaded from config file '{}'...", settings.config_file);
log_info!("Database File Path: {:#?}", settings.database_file);

let path = Path::new(settings.database_file.as_str());
Expand All @@ -91,8 +91,8 @@ fn main() -> Result<(), Box<dyn Error>> {
}
}
Commands::Start { config } => {
log_info!("Loading from config file '{}'...", config);
let settings = DnsSettings::load(config.clone())?;
log_info!("Loaded from config file '{}'...", settings.config_file);
log_info!("Settings: {:?}", settings);

let server_udp = DnsUdpServer::new(settings.clone());
Expand Down Expand Up @@ -120,8 +120,8 @@ fn main() -> Result<(), Box<dyn Error>> {
_handle.join().unwrap();
}
Commands::Add { config, interactive, .. } if interactive => {
log_info!("Loading from config file '{}'...", config);
let settings = DnsSettings::load(config.clone())?;
log_info!("Loaded from config file '{}'...", settings.config_file);
log_info!("Database File Path: {:#?}", settings.database_file);

let domain = get_input("Domain: ", None, "A domain is required.", |x| !x.is_empty()); // TODO should check for valid domain
Expand Down Expand Up @@ -170,8 +170,8 @@ fn main() -> Result<(), Box<dyn Error>> {
log_info!("Successfully added record: {:?}", record);
}
Commands::Add { config, interactive, domain, query_type, class, ttl, host, ip, priority } if !interactive => {
log_info!("Loading from config file '{}'...", config);
let settings = DnsSettings::load(config.clone())?;
log_info!("Loaded from config file '{}'...", settings.config_file);
jmeaster30 marked this conversation as resolved.
Show resolved Hide resolved
log_info!("Database File Path: {:#?}", settings.database_file);

let domain = domain.unwrap();
Expand Down
20 changes: 18 additions & 2 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern crate shellexpand;
pub struct DnsSettings {
pub listening_port: u16,
pub remote_lookup_port: u16,
pub config_file: String,
pub database_file: String,
pub thread_count: u32,
pub use_udp: bool,
Expand All @@ -17,8 +18,22 @@ pub struct DnsSettings {

impl DnsSettings {
pub fn load(filename: String) -> Result<Self, Box<dyn Error>> {
let contents = fs::read_to_string(shellexpand::full(filename.as_str()).unwrap().to_string())
.expect("Aw man, there was an issue while opening the config file :(");

let filenames= [filename.as_str(), "./dns.config.yaml", "~/.config/simpledns/dns.config.yaml", "/etc/simpledns/dns.config.yaml"];
jmeaster30 marked this conversation as resolved.
Show resolved Hide resolved
let mut matched = false;
let mut contents = String::new();
let mut config_file = String::new();
for filename in filenames {
contents = match fs::read_to_string(shellexpand::full(filename).unwrap().to_string()) {
jmeaster30 marked this conversation as resolved.
Show resolved Hide resolved
Ok(data) => { matched = true; config_file = String::from(filename); data},
Err(_) => { continue },
};
break
}
if !matched { panic!("Aw man, there was an issue while opening the config file :(") }
jmeaster30 marked this conversation as resolved.
Show resolved Hide resolved

//let contents = fs::read_to_string(shellexpand::full(filename.as_str()).unwrap().to_string())
// .expect("Aw man, there was an issue while opening the config file :(");

let yaml_files = &YamlLoader::load_from_str(contents.as_str())?;
let config_settings_option = &yaml_files.get(0);
Expand Down Expand Up @@ -56,6 +71,7 @@ impl DnsSettings {
Ok(DnsSettings {
listening_port,
remote_lookup_port,
config_file,
database_file,
thread_count,
use_udp,
Expand Down