use anyhow::{Context, Error};
use comfy_table::Table;
use manta_shared::types::dto::BosSessionTemplate;
pub fn print(
templates: &[BosSessionTemplate],
output: &str,
) -> Result<(), Error> {
if output == "json" {
println!(
"{}",
serde_json::to_string_pretty(templates)
.context("Failed to serialize BOS sessiontemplates")?
);
} else {
print_table_struct(templates.to_vec());
}
Ok(())
}
pub fn print_table_struct(bos_sessiontemplate_vec: Vec<BosSessionTemplate>) {
let mut table = Table::new();
table.set_header(vec![
"Name",
"Image ID",
"Runtime Configuration",
"Cfs Enabled",
"Target",
]);
for bos_template in bos_sessiontemplate_vec {
let enable_cfs = bos_template
.enable_cfs
.map_or_else(|| "N/A".to_string(), |v| v.to_string());
if let Some(boot_sets) = bos_template.boot_sets {
for boot_set in boot_sets {
let target: Vec<String> =
if let Some(node_groups) = boot_set.1.node_groups {
node_groups
} else {
boot_set.1.node_list.unwrap_or_default()
};
table.add_row(vec![
bos_template.name.clone().unwrap_or_default(),
boot_set
.1
.path
.unwrap_or_default()
.trim_start_matches("s3://boot-images/")
.trim_end_matches("/manifest.json")
.to_string(),
bos_template
.cfs
.as_ref()
.and_then(|cfs| cfs.configuration.clone())
.unwrap_or_else(|| "NA".to_string()),
enable_cfs.clone(),
crate::common::multi_line::string_vec_to_multi_line_string(
Some(&target),
2,
),
]);
}
}
}
println!("{table}");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn print_json_on_empty_succeeds() {
assert!(print(&[], "json").is_ok());
}
#[test]
fn print_table_on_empty_succeeds() {
assert!(print(&[], "table").is_ok());
}
#[test]
fn print_unknown_format_falls_back_to_table() {
assert!(print(&[], "garbage").is_ok());
assert!(print(&[], "").is_ok());
}
#[test]
fn print_table_struct_does_not_panic_on_empty() {
print_table_struct(vec![]);
}
}