diff options
author | HimbeerserverDE <himbeerserverde@gmail.com> | 2022-10-29 12:20:28 +0200 |
---|---|---|
committer | HimbeerserverDE <himbeerserverde@gmail.com> | 2022-10-29 12:20:28 +0200 |
commit | 87e2ffd8acf240745782cbc5347c379a9db8427c (patch) | |
tree | 809b3b200605685373c646c306526ade7616a0e5 | |
parent | 821327bbcd1b01ae3eb09882411b3d6fbe3b5dc8 (diff) |
take non-default config path from arguments
closes #4
-rw-r--r-- | src/main.rs | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs index 54eec34..633e6bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ +use std::env; use std::fmt; use std::fs::File; +use std::io::Read; use std::net::{Ipv4Addr, Ipv6Addr}; use std::str::FromStr; use std::sync::{mpsc, Arc}; @@ -114,9 +116,18 @@ struct Config { } fn main() -> Result<()> { - let config_file = File::open("/etc/dyndns.conf")?; + // Get the config path from the first command-line argument + // or fall back to the default /etc/dyndns.conf. + let config_path = env::args().nth(1).unwrap_or_else(|| { + String::from("/etc/dyndns.conf") + }); + + let mut config_file = File::open(config_path.as_str())?; + + let mut config_contents = String::new(); + config_file.read_to_string(&mut config_contents).unwrap(); - let parsed_config: Config = serde_json::from_reader(config_file)?; + let parsed_config: Config = serde_json::from_str(&config_contents)?; let config = Arc::new(parsed_config); let config0 = config.clone(); |