From b08ab4d1edd816f36e9cc42258362e222590d274 Mon Sep 17 00:00:00 2001 From: Ryan Sabatini Date: Tue, 8 Nov 2022 12:33:41 -0600 Subject: [PATCH] Code cleanup with suggestions from PR maintainer --- src/modules/azure.rs | 61 +++++++++----------------------------------- 1 file changed, 12 insertions(+), 49 deletions(-) diff --git a/src/modules/azure.rs b/src/modules/azure.rs index 7ae610f9e0d11..77c7015985265 100644 --- a/src/modules/azure.rs +++ b/src/modules/azure.rs @@ -78,38 +78,22 @@ fn get_azure_profile_info(context: &Context) -> Option { let mut config_path = get_config_file_location(context)?; config_path.push("azureProfile.json"); - if config_path.exists() { - let azure_profile: Option = load_azure_profile(&config_path); - - if let Some(azure_profile) = azure_profile { - let subscription = azure_profile.subscriptions.iter().find_map(|s| { - if s.is_default { - Some(s.clone()) - } else { - None - } - }); - subscription - } else { - None - } - } else { - None - } + let azure_profile = load_azure_profile(&config_path)?; + azure_profile + .subscriptions + .into_iter() + .find(|s| s.is_default) } fn load_azure_profile(config_path: &PathBuf) -> Option { - let json_data = fs::read_to_string(&config_path).expect("Unable to open azureProfile.json"); + let json_data = fs::read_to_string(&config_path).ok()?; let sanitized_json_data = json_data.strip_prefix('\u{feff}').unwrap_or(&json_data); - let azure_profile: Option = - if let Ok(azure_profile) = serde_json::from_str::(sanitized_json_data) { - Some(azure_profile) - } else { - log::info!("Failed to parse azure profile."); - None - }; - - azure_profile + if let Ok(azure_profile) = serde_json::from_str::(sanitized_json_data) { + Some(azure_profile) + } else { + log::info!("Failed to parse azure profile."); + None + } } fn get_config_file_location(context: &Context) -> Option { @@ -123,27 +107,6 @@ fn get_config_file_location(context: &Context) -> Option { }) } -<<<<<<< HEAD -fn parse_json(json_file_path: &Path) -> Option { - let mut buffer: Vec = Vec::new(); - - let json_file = File::open(json_file_path).ok()?; - let mut reader = BufReader::new(json_file); - reader.read_to_end(&mut buffer).ok()?; - - let bytes = buffer.as_mut_slice(); - let decodedbuffer = bytes.strip_prefix(&[239, 187, 191]).unwrap_or(bytes); - - if let Ok(parsed_json) = serde_json::from_slice(decodedbuffer) { - Some(parsed_json) - } else { - log::info!("Failed to parse json"); - None - } -} - -======= ->>>>>>> 7446ed0d (add username to azure module config) #[cfg(test)] mod tests { use crate::modules::azure::load_azure_profile;