use anyhow::{Context, Result};
use serde::Serialize;
#[derive(Debug, Serialize)]
pub struct ConfigSummary {
pub config_file: String,
pub log_level: String,
pub current_site: Option<String>,
pub read_only: bool,
pub groups_available: Option<Vec<String>>,
pub current_hsm: Option<String>,
}
pub fn print(summary: &ConfigSummary, output_opt: Option<&str>) -> Result<()> {
if let Some("json") = output_opt {
println!(
"{}",
serde_json::to_string(summary)
.context("Failed to serialize config summary to JSON")?
);
} else {
println!("Configuration file: {}", summary.config_file);
println!("Log level: {}", summary.log_level);
println!(
"Current site: {}",
summary.current_site.as_deref().unwrap_or("(unset)")
);
println!(
"Read-only: {}",
if summary.read_only { "yes" } else { "no" }
);
let groups = match (&summary.groups_available, &summary.current_site) {
(Some(v), _) => v.join(", "),
(None, None) => "(no site selected)".to_string(),
(None, Some(_)) => "Could not get list of groups available".to_string(),
};
println!("Groups available: {groups}");
println!(
"Current group: {}",
summary.current_hsm.as_deref().unwrap_or("(unset)")
);
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::Value;
fn sample() -> ConfigSummary {
ConfigSummary {
config_file: "/home/u/.config/manta/cli.toml".to_string(),
log_level: "info".to_string(),
current_site: Some("alps".to_string()),
read_only: false,
groups_available: Some(vec!["compute".to_string(), "uan".to_string()]),
current_hsm: Some("compute".to_string()),
}
}
#[test]
fn text_mode_writes_lines_in_legacy_order() {
print(&sample(), None).unwrap();
print(&sample(), Some("table")).unwrap();
}
#[test]
fn json_mode_emits_one_object_with_expected_fields() {
let s = sample();
let json = serde_json::to_string(&s).unwrap();
let v: Value = serde_json::from_str(&json).unwrap();
assert_eq!(v["config_file"], "/home/u/.config/manta/cli.toml");
assert_eq!(v["log_level"], "info");
assert_eq!(v["current_site"], "alps");
assert_eq!(v["read_only"], false);
assert_eq!(v["groups_available"][0], "compute");
assert_eq!(v["current_hsm"], "compute");
}
#[test]
fn groups_available_none_renders_as_null_in_json() {
let mut s = sample();
s.groups_available = None;
let json = serde_json::to_string(&s).unwrap();
let v: Value = serde_json::from_str(&json).unwrap();
assert!(v["groups_available"].is_null());
}
#[test]
fn current_site_none_renders_as_null_in_json() {
let mut s = sample();
s.current_site = None;
let json = serde_json::to_string(&s).unwrap();
let v: Value = serde_json::from_str(&json).unwrap();
assert!(v["current_site"].is_null());
}
#[test]
fn current_hsm_none_renders_as_null_in_json() {
let mut s = sample();
s.current_hsm = None;
let json = serde_json::to_string(&s).unwrap();
let v: Value = serde_json::from_str(&json).unwrap();
assert!(v["current_hsm"].is_null());
}
#[test]
fn text_mode_renders_with_no_site() {
let mut s = sample();
s.current_site = None;
print(&s, None).unwrap();
}
}