Skip to content

Commit

Permalink
feat: filter sat file template data base on cli arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Sopena Ballesteros committed Aug 10, 2024
1 parent c9c5117 commit 82b3435
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 45 deletions.
72 changes: 61 additions & 11 deletions src/cli/commands/apply_sat_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ use crate::{
common::{
self,
sat_file::{
self, import_images_section_in_sat_file, validate_sat_file_configurations_section,
validate_sat_file_images_section,
self,
image::Ims,
import_images_section_in_sat_file,
sessiontemplate::{self, Image, ImsDetails},
validate_sat_file_configurations_section, validate_sat_file_images_section, SatFile,
},
},
};
Expand Down Expand Up @@ -84,27 +87,76 @@ pub async fn exec(
};
}

let mut sat_template_file_yaml: Mapping = sat_file::render_jinja2_sat_file_yaml(
let sat_template_file_yaml: Value = sat_file::render_jinja2_sat_file_yaml(
&sat_file_content,
values_file_content_opt.as_ref(),
values_cli_opt,
)
.as_mapping_mut()
.unwrap()
// .as_mapping_mut()
// .unwrap()
.clone();

let sat_template_file_string = serde_yaml::to_string(&sat_template_file_yaml).unwrap();

let mut sat_template: SatFile = serde_yaml::from_str(&sat_template_file_string)
.expect("Could not parse SAT template yaml file");

// Filter either images or session_templates section according to user request
//
// Clean SAT template file if user only wan'ts to process the 'images' section. In this case,
// we will remove 'session_templates' section from SAT fiel and also the entries in
// 'configurations' section not used
if image_only {
let configuration_name_image_vec: Vec<String> = sat_template
.images
.iter()
.filter_map(|sat_template_image| sat_template_image.configuration.clone())
.collect();

// Remove configurations not used by any image
sat_template
.configurations
.retain(|configuration| configuration_name_image_vec.contains(&configuration.name));

// Remove section "session_templates"
sat_template_file_yaml.remove_entry("session_templates");
sat_template.session_templates = Vec::new();
}

// Clean SAT template file if user only wan'ts to process the 'session_template' section. In this case,
// we will remove 'images' section from SAT fiel and also the entries in
// 'configurations' section not used
if session_template_only {
// Remove section "images"
sat_template_file_yaml.remove_entry("images");
let configuration_name_sessiontemplate_vec: Vec<String> = sat_template
.session_templates
.iter()
.map(|sat_sessiontemplate| sat_sessiontemplate.configuration.clone())
.collect();

// Remove configurations not used by any sessiontemplate
sat_template.configurations.retain(|configuration| {
configuration_name_sessiontemplate_vec.contains(&configuration.name)
});

let image_name_sessiontemplate_vec: Vec<String> = sat_template
.session_templates
.iter()
.filter_map(|sessiontemplate| match &sessiontemplate.image {
Image::ImageRef(name) => Some(name),
Image::Ims { ims } => match ims {
ImsDetails::Name { name } => Some(name),
ImsDetails::Id { .. } => None,
},
})
.cloned()
.collect();

// Remove images not used by any sessiontemplate
sat_template
.images
.retain(|image| image_name_sessiontemplate_vec.contains(&image.name));
}

let sat_template_file_yaml: Value = serde_yaml::to_value(sat_template_file_yaml).unwrap();
let sat_template_file_yaml: Value = serde_yaml::to_value(sat_template).unwrap();

println!(
"{}#### SAT file content ####{}\n{}",
Expand All @@ -118,8 +170,6 @@ pub async fn exec(
.interact()
.unwrap();

println!();

// Run/process Pre-hook
if prehook.is_some() {
println!("Running the pre-hook '{}'", &prehook.unwrap());
Expand Down
Loading

0 comments on commit 82b3435

Please sign in to comment.