Reuse File object in reading config from path

master
Edward Shen 2023-07-28 00:16:36 -07:00
parent 93d90ba90b
commit 87001a9850
Signed by: edward
GPG Key ID: 0A400FFE10097C30
1 changed files with 14 additions and 11 deletions

View File

@ -3,7 +3,7 @@
use std::collections::{BTreeMap, HashMap, HashSet}; use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt::{Debug, Display}; use std::fmt::{Debug, Display};
use std::fs::File; use std::fs::File;
use std::io::{self, IsTerminal}; use std::io::{self, BufReader, IsTerminal};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -514,7 +514,7 @@ fn load_config_from_path<P: AsRef<Path>>(path: P) -> Option<Config> {
found = format!("{:o}", current_mode), found = format!("{:o}", current_mode),
expected = "600", expected = "600",
"File permissions too broad! Your GLOBAL Cloudflare API key is accessible to all users on the system!" "File permissions too broad! Your GLOBAL Cloudflare API key is accessible to all users on the system!"
) );
} }
} }
Err(e) => { Err(e) => {
@ -522,16 +522,19 @@ fn load_config_from_path<P: AsRef<Path>>(path: P) -> Option<Config> {
} }
} }
match std::fs::read_to_string(&path) { let data = match std::io::read_to_string(BufReader::new(file)) {
Ok(data) => match toml::from_str(&data) { Ok(data) => data,
Ok(config) => return Some(config), Err(e) => {
Err(err) => { warn!("Failed to read config file: {e}");
debug!("Failed to parse config file: {err}"); return None;
} }
}, };
match toml::from_str(&data) {
Ok(config) => Some(config),
Err(err) => { Err(err) => {
debug!("Unable to read the config file: {err}",); warn!("Failed to parse config file: {err}");
None
} }
} }
None
} }