diff --git a/src/main.rs b/src/main.rs index 4279438..fc49565 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,16 +32,16 @@ struct Args { #[derive(Debug, Subcommand)] enum Commands { Start { - #[arg(short, long, value_parser, default_value = "~/.config/simpledns/dns.config.yaml")] - config: String, + #[arg(short, long, value_parser)] + config: Option, }, Init { - #[arg(short, long, value_parser, default_value = "~/.config/simpledns/dns.config.yaml")] - config: String, + #[arg(short, long, value_parser)] + config: Option, }, Add { - #[arg(short, long, value_parser, default_value = "~/.config/simpledns/dns.config.yaml")] - config: String, + #[arg(short, long, value_parser)] + config: Option, #[arg(short, long, action)] interactive: bool, #[arg(long, value_parser, required_unless_present("interactive"))] @@ -74,8 +74,12 @@ fn main() -> Result<(), Box> { match args.command { Commands::Init { config } => { - log_info!("Loading from config file '{}'...", config); - let settings = DnsSettings::load(config.clone())?; + let settings = match config { + Some(filename) => DnsSettings::load_from_file(filename.clone()), + None => DnsSettings::load_default(), + }; + let settings = settings.expect("Error reading settings!"); + log_info!("Database File Path: {:#?}", settings.database_file); let path = Path::new(settings.database_file.as_str()); @@ -91,8 +95,11 @@ fn main() -> Result<(), Box> { } } Commands::Start { config } => { - log_info!("Loading from config file '{}'...", config); - let settings = DnsSettings::load(config.clone())?; + let settings = match config { + Some(filename) => DnsSettings::load_from_file(filename.clone()), + None => DnsSettings::load_default(), + }; + let settings = settings.expect("Error reading settings!"); log_info!("Settings: {:?}", settings); let server_udp = DnsUdpServer::new(settings.clone()); @@ -120,8 +127,11 @@ fn main() -> Result<(), Box> { _handle.join().unwrap(); } Commands::Add { config, interactive, .. } if interactive => { - log_info!("Loading from config file '{}'...", config); - let settings = DnsSettings::load(config.clone())?; + let settings = match config { + Some(filename) => DnsSettings::load_from_file(filename.clone()), + None => DnsSettings::load_default(), + }; + let settings = settings.expect("Error reading settings!"); 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 @@ -170,8 +180,11 @@ fn main() -> Result<(), Box> { 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())?; + let settings = match config { + Some(filename) => DnsSettings::load_from_file(filename.clone()), + None => DnsSettings::load_default(), + }; + let settings = settings.expect("Error reading settings!"); log_info!("Database File Path: {:#?}", settings.database_file); let domain = domain.unwrap(); diff --git a/src/settings.rs b/src/settings.rs index b9106d9..1256452 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -2,6 +2,9 @@ use std::error::Error; use std::fs; use std::io::ErrorKind; use yaml_rust::YamlLoader; +use std::path::Path; + +use crate::log_info; extern crate shellexpand; @@ -16,9 +19,13 @@ pub struct DnsSettings { } impl DnsSettings { - pub fn load(filename: String) -> Result> { + + pub fn load_from_file(filename: String) -> Result> { + + let error_str = "Aw man, there was an issue while opening the config file '{".to_owned() + filename.as_str() + "}' :("; 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 :("); + .expect(&error_str); + log_info!("Loaded from config file '{}'...", filename.as_str()); let yaml_files = &YamlLoader::load_from_str(contents.as_str())?; let config_settings_option = &yaml_files.get(0); @@ -68,4 +75,14 @@ impl DnsSettings { ))), } } + + pub fn load_default() -> Result> { + let filenames = ["./dns.config.yaml", "~/.config/simpledns/dns.config.yaml", "/etc/simpledns/dns.config.yaml"]; + let mut config_file = ""; + for filename in filenames { + if Path::new(filename).exists() { config_file = filename; break; } + } + if config_file == "" { panic!("No valid config file given"); } + Self::load_from_file(String::from(config_file)) + } }