Skip to content

Commit

Permalink
Code cleanup with suggestions from PR maintainer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Sabatini authored and Ryan Sabatini committed Nov 14, 2022
1 parent 6a69636 commit b08ab4d
Showing 1 changed file with 12 additions and 49 deletions.
61 changes: 12 additions & 49 deletions src/modules/azure.rs
Expand Up @@ -78,38 +78,22 @@ fn get_azure_profile_info(context: &Context) -> Option<Subscription> {
let mut config_path = get_config_file_location(context)?;
config_path.push("azureProfile.json");

if config_path.exists() {
let azure_profile: Option<AzureProfile> = 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<AzureProfile> {
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<AzureProfile> =
if let Ok(azure_profile) = serde_json::from_str::<AzureProfile>(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::<AzureProfile>(sanitized_json_data) {
Some(azure_profile)
} else {
log::info!("Failed to parse azure profile.");
None
}
}

fn get_config_file_location(context: &Context) -> Option<PathBuf> {
Expand All @@ -123,27 +107,6 @@ fn get_config_file_location(context: &Context) -> Option<PathBuf> {
})
}

<<<<<<< HEAD
fn parse_json(json_file_path: &Path) -> Option<JValue> {
let mut buffer: Vec<u8> = 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;
Expand Down

0 comments on commit b08ab4d

Please sign in to comment.