use std::sync::Arc;
use anyhow::{anyhow, bail, Context, Result};
use colored::Colorize;
use serde_json::{json, Value};
use toon_format::encode_default as toon_encode;
pub mod api;
use api::{HealthHistoryEntry, InfraApi, ListResourcesParams, ResourceData, ResourceTypeMapping};
use crate::config::OutputFormat;
use crate::execution::{fan_out, report_errors_and_collect_successes, ExecutionTarget};
use crate::render;
const JSON_KEY_PROFILE: &str = "profile";
const ALLOWED_SCOPE_KEYS: [&str; 3] = ["service", "environment", "team"];
pub async fn run_types(targets: &[Arc<ExecutionTarget>], output: OutputFormat) -> Result<()> {
eprintln!("{}", "Fetching available resource types...".dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |target| async move {
let api = InfraApi::new(&target.client);
Ok(api.available_types().await?)
})
.await;
let mut merged: Vec<(String, ResourceTypeMapping)> = Vec::new();
for (profile, resp) in report_errors_and_collect_successes(per_profile)? {
for mapping in resp.resource_types {
merged.push((profile.clone(), mapping));
}
}
match output {
OutputFormat::Json | OutputFormat::Toon => {
let rows: Vec<Value> = merged
.iter()
.map(|(profile, m)| type_mapping_to_json(m, include_profile, profile))
.collect();
render_machine_rows(output, &rows)?;
}
OutputFormat::Text => {
if merged.is_empty() {
render::print_no_results("No resource types found.");
return Ok(());
}
let rows: Vec<Vec<String>> = merged
.iter()
.map(|(profile, m)| {
vec![
profile.clone(),
display_or_dash(
m.category_type.as_ref().and_then(|c| c.category.as_deref()),
),
display_or_dash(
m.category_type
.as_ref()
.and_then(|c| c.type_name.as_deref()),
),
display_or_dash(m.label.as_deref()),
]
})
.collect();
render::render_table(&["Category", "Type", "Label"], rows, include_profile);
}
}
Ok(())
}
#[allow(clippy::too_many_arguments)]
pub async fn run_list(
targets: &[Arc<ExecutionTarget>],
category: &str,
resource_type: &str,
name_filter: Option<&str>,
scope: &[String],
start_row: Option<i64>,
end_row: Option<i64>,
output: OutputFormat,
) -> Result<()> {
let category = require_non_empty(category, "--category")?;
let resource_type = require_non_empty(resource_type, "--type")?;
let name_filter = name_filter.map(str::trim).filter(|s| !s.is_empty());
let scope_filters = parse_scope_filters(scope)?;
validate_page_window(start_row, end_row)?;
eprintln!("{}", "Fetching resources...".dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |target| {
let scope_filters = scope_filters.clone();
let category = category.to_string();
let resource_type = resource_type.to_string();
let name_filter = name_filter.map(String::from);
async move {
let api = InfraApi::new(&target.client);
let params = ListResourcesParams {
category: &category,
resource_type: &resource_type,
name_filter: name_filter.as_deref(),
scope_filters: &scope_filters,
start_row,
end_row,
};
Ok(api.list(¶ms).await?)
}
})
.await;
let mut counts: Vec<ProfileCounts> = Vec::new();
let mut merged: Vec<(String, ResourceData)> = Vec::new();
for (profile, resp) in report_errors_and_collect_successes(per_profile)? {
counts.push(ProfileCounts {
profile: profile.clone(),
total_count: resp.total_count,
returned_count: resp.resources.len(),
});
for resource in resp.resources {
merged.push((profile.clone(), resource));
}
}
let total_count = aggregate_total(&counts);
match output {
OutputFormat::Json | OutputFormat::Toon => {
let rows: Vec<Value> = merged
.iter()
.map(|(profile, r)| resource_to_json(r, include_profile, profile))
.collect();
let envelope = build_list_envelope(total_count, rows, &counts, include_profile);
render_machine_envelope(output, &envelope)?;
}
OutputFormat::Text => {
if merged.is_empty() {
render::print_no_results("No resources found.");
return Ok(());
}
let rows: Vec<Vec<String>> = merged
.iter()
.map(|(profile, r)| {
vec![
profile.clone(),
display_or_dash(r.resource_id.as_deref()),
display_or_dash(display_name(r)),
]
})
.collect();
render::render_table(&["Resource ID", "Name"], rows, include_profile);
eprintln!(
"{}",
format_count_summary(merged.len(), total_count, &counts, include_profile).dimmed()
);
}
}
Ok(())
}
pub async fn run_health_history(
targets: &[Arc<ExecutionTarget>],
resource_id: &str,
output: OutputFormat,
) -> Result<()> {
let resource_id = require_non_empty(resource_id, "resource id")?;
let target = single_target(targets, "health-history")?;
eprintln!(
"{}",
format!("Fetching health history for '{resource_id}'...").dimmed()
);
let resp = InfraApi::new(&target.client)
.health_history(resource_id)
.await
.with_context(|| format!("profile '{}' failed", target.profile_name))?;
let history = resp.health_history;
match output {
OutputFormat::Json | OutputFormat::Toon => {
let rows: Vec<Value> = history.iter().map(health_entry_to_json).collect();
render_machine_rows(output, &rows)?;
}
OutputFormat::Text => {
if history.is_empty() {
render::print_no_results("No health history found.");
return Ok(());
}
let rows: Vec<Vec<String>> = history
.iter()
.map(|entry| {
vec![
target.profile_name.clone(),
display_or_dash(entry.timestamp.as_deref()),
display_or_dash(entry.status.as_deref()),
]
})
.collect();
render::render_table(&["Timestamp", "Status"], rows, false);
}
}
Ok(())
}
pub async fn run_raw_data(
targets: &[Arc<ExecutionTarget>],
resource_id: &str,
output: OutputFormat,
) -> Result<()> {
let resource_id = require_non_empty(resource_id, "resource id")?;
let target = single_target(targets, "raw-data")?;
eprintln!(
"{}",
format!("Fetching raw resource data for '{resource_id}'...").dimmed()
);
let resp = InfraApi::new(&target.client)
.raw_data(resource_id)
.await
.with_context(|| format!("profile '{}' failed", target.profile_name))?;
let results: Vec<Value> = match resp.raw_data {
Some(doc) => vec![doc],
None => {
eprintln!("{}", "no raw data for this resource".yellow());
Vec::new()
}
};
match output {
OutputFormat::Json => render::render_json_auto(&results)?,
OutputFormat::Toon => render::render_toon(&results)?,
OutputFormat::Text => {
render::render_get_text(&results, false, "No raw data found.", None)?;
}
}
Ok(())
}
fn render_machine_rows(output: OutputFormat, rows: &[Value]) -> Result<()> {
match output {
OutputFormat::Json => render::render_json(rows),
OutputFormat::Toon => render::render_toon(rows),
OutputFormat::Text => {
unreachable!("callers render text themselves; only Json/Toon reach here")
}
}
}
struct ProfileCounts {
profile: String,
total_count: i64,
returned_count: usize,
}
fn aggregate_total(counts: &[ProfileCounts]) -> i64 {
counts
.iter()
.fold(0i64, |acc, c| acc.saturating_add(c.total_count))
}
fn build_list_envelope(
total_count: i64,
rows: Vec<Value>,
counts: &[ProfileCounts],
include_profile: bool,
) -> Value {
let mut envelope = serde_json::Map::new();
envelope.insert("total_count".to_string(), json!(total_count));
envelope.insert("returned_count".to_string(), json!(rows.len()));
if include_profile {
let per_profile: Vec<Value> = counts
.iter()
.map(|c| {
json!({
JSON_KEY_PROFILE: c.profile,
"total_count": c.total_count,
"returned_count": c.returned_count,
})
})
.collect();
envelope.insert("counts_by_profile".to_string(), Value::Array(per_profile));
}
envelope.insert("resources".to_string(), Value::Array(rows));
Value::Object(envelope)
}
fn render_machine_envelope(output: OutputFormat, envelope: &Value) -> Result<()> {
match output {
OutputFormat::Json => render::render_json_auto(std::slice::from_ref(envelope)),
OutputFormat::Toon => {
let encoded =
toon_encode(envelope).map_err(|e| anyhow!("TOON encoding failed: {e}"))?;
println!("{encoded}");
Ok(())
}
OutputFormat::Text => {
unreachable!("callers render text themselves; only Json/Toon reach here")
}
}
}
fn format_count_summary(
returned: usize,
total: i64,
counts: &[ProfileCounts],
include_profile: bool,
) -> String {
let mut out = format!("Showing {returned} of {total} total resources");
if include_profile {
for c in counts {
out.push_str(&format!(
"\n {}: {} of {}",
c.profile, c.returned_count, c.total_count
));
}
}
out
}
fn require_non_empty<'v>(value: &'v str, field_name: &str) -> Result<&'v str> {
let trimmed = value.trim();
if trimmed.is_empty() {
bail!("{field_name} must not be empty");
}
Ok(trimmed)
}
fn single_target<'t>(
targets: &'t [Arc<ExecutionTarget>],
subcommand: &str,
) -> Result<&'t ExecutionTarget> {
match targets {
[target] => Ok(target),
[] => bail!("no profile resolved for `cx infra resources {subcommand}`"),
_ => bail!(
"`cx infra resources {subcommand}` accepts a single profile, but {} were given; \
a resource id is scoped to one team and cannot resolve in another profile. \
Re-run once per profile with a single -p.",
targets.len()
),
}
}
fn validate_page_window(start_row: Option<i64>, end_row: Option<i64>) -> Result<()> {
if let Some(start) = start_row {
if start < 0 {
bail!("--start-row must not be negative (got {start}); rows are 0-based");
}
}
if let Some(end) = end_row {
if end < 0 {
bail!("--end-row must not be negative (got {end})");
}
}
if let (Some(start), Some(end)) = (start_row, end_row) {
if end <= start {
bail!(
"--end-row ({end}) must be greater than --start-row ({start}); \
--end-row is exclusive, so this window selects no rows"
);
}
}
Ok(())
}
fn parse_scope_filters(scope: &[String]) -> Result<Vec<(String, String)>> {
let mut filters: Vec<(String, String)> = Vec::new();
for raw in scope {
let Some((key, value)) = raw.split_once('=') else {
bail!("invalid --scope '{raw}': expected key=value");
};
let key = key.trim();
let value = value.trim();
if !ALLOWED_SCOPE_KEYS.contains(&key) {
bail!(
"unknown --scope key '{key}'; allowed keys: {}",
ALLOWED_SCOPE_KEYS.join(", ")
);
}
if value.is_empty() {
bail!("invalid --scope '{raw}': value must not be empty");
}
if let Some((_, existing)) = filters.iter().find(|(k, _)| k == key) {
bail!(
"--scope key '{key}' given more than once ('{existing}' then '{value}'); \
each scope key accepts a single value and different keys combine with AND, \
so repeating one cannot match either value - run one query per value"
);
}
filters.push((key.to_string(), value.to_string()));
}
Ok(filters)
}
fn resource_to_json(item: &ResourceData, include_profile: bool, profile: &str) -> Value {
let v = json!({
"resource_id": item.resource_id,
"name": item.name,
"columns": item.columns,
});
tag_profile(v, include_profile, profile)
}
fn tag_profile(mut v: Value, include_profile: bool, profile: &str) -> Value {
if include_profile {
if let Value::Object(ref mut m) = v {
m.insert(
JSON_KEY_PROFILE.to_string(),
Value::String(profile.to_string()),
);
}
}
v
}
fn health_entry_to_json(item: &HealthHistoryEntry) -> Value {
json!({
"timestamp": item.timestamp,
"status": item.status,
})
}
fn type_mapping_to_json(item: &ResourceTypeMapping, include_profile: bool, profile: &str) -> Value {
let v = json!({
"category": item.category_type.as_ref().and_then(|c| c.category.clone()),
"type": item.category_type.as_ref().and_then(|c| c.type_name.clone()),
"label": item.label,
});
tag_profile(v, include_profile, profile)
}
fn display_or_dash(value: Option<&str>) -> String {
value.filter(|s| !s.is_empty()).unwrap_or("-").to_string()
}
fn display_name(item: &ResourceData) -> Option<&str> {
item.columns
.iter()
.find(|(key, _)| key.eq_ignore_ascii_case("name"))
.map(|(_, value)| value.as_str())
.filter(|s| !s.is_empty())
.or(item.name.as_deref())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn require_non_empty_trims_and_accepts_values() {
assert_eq!(require_non_empty(" Hosts ", "--category").unwrap(), "Hosts");
}
#[test]
fn require_non_empty_rejects_empty_and_whitespace() {
let err = require_non_empty("", "--category").unwrap_err();
assert!(err.to_string().contains("--category must not be empty"));
let err = require_non_empty(" ", "resource id").unwrap_err();
assert!(err.to_string().contains("resource id must not be empty"));
}
#[test]
fn parse_scope_filters_accepts_allowed_keys() {
let scope = vec![
"service=checkout".to_string(),
"environment=prod".to_string(),
"team=platform".to_string(),
];
let filters = parse_scope_filters(&scope).unwrap();
assert_eq!(
filters,
vec![
("service".to_string(), "checkout".to_string()),
("environment".to_string(), "prod".to_string()),
("team".to_string(), "platform".to_string()),
]
);
}
#[test]
fn parse_scope_filters_trims_whitespace() {
let scope = vec![" service = checkout ".to_string()];
let filters = parse_scope_filters(&scope).unwrap();
assert_eq!(
filters,
vec![("service".to_string(), "checkout".to_string())]
);
}
#[test]
fn parse_scope_filters_rejects_missing_equals() {
let err = parse_scope_filters(&["service".to_string()]).unwrap_err();
assert!(err.to_string().contains("expected key=value"));
}
#[test]
fn parse_scope_filters_rejects_unknown_key() {
let err = parse_scope_filters(&["region=us-east-1".to_string()]).unwrap_err();
let msg = err.to_string();
assert!(msg.contains("unknown --scope key 'region'"));
assert!(msg.contains("service, environment, team"));
}
#[test]
fn parse_scope_filters_rejects_empty_value() {
let err = parse_scope_filters(&["service=".to_string()]).unwrap_err();
assert!(err.to_string().contains("must not be empty"));
}
#[test]
fn parse_scope_filters_empty_input_yields_no_filters() {
assert!(parse_scope_filters(&[]).unwrap().is_empty());
}
#[test]
fn parse_scope_filters_rejects_a_repeated_key() {
let err =
parse_scope_filters(&["service=a".to_string(), "service=b".to_string()]).unwrap_err();
let msg = err.to_string();
assert!(msg.contains("'service' given more than once"), "got: {msg}");
assert!(msg.contains('a') && msg.contains('b'), "got: {msg}");
}
#[test]
fn parse_scope_filters_rejects_a_repeated_key_even_with_the_same_value() {
let err =
parse_scope_filters(&["service=a".to_string(), "service=a".to_string()]).unwrap_err();
assert!(err.to_string().contains("given more than once"));
}
#[test]
fn parse_scope_filters_detects_a_repeat_after_trimming() {
let err = parse_scope_filters(&[" service = a ".to_string(), "service=b".to_string()])
.unwrap_err();
assert!(err.to_string().contains("given more than once"));
}
#[test]
fn page_window_accepts_an_ascending_window_and_open_ends() {
assert!(validate_page_window(None, None).is_ok());
assert!(validate_page_window(Some(0), Some(100)).is_ok());
assert!(validate_page_window(Some(100), Some(200)).is_ok());
assert!(validate_page_window(Some(100), None).is_ok());
assert!(validate_page_window(None, Some(50)).is_ok());
assert!(validate_page_window(Some(0), Some(1)).is_ok());
}
#[test]
fn page_window_rejects_a_negative_start_row() {
let err = validate_page_window(Some(-5), None).unwrap_err();
let msg = err.to_string();
assert!(
msg.contains("--start-row must not be negative"),
"got: {msg}"
);
assert!(msg.contains("-5"), "got: {msg}");
}
#[test]
fn page_window_rejects_a_negative_end_row() {
let err = validate_page_window(None, Some(-1)).unwrap_err();
assert!(err.to_string().contains("--end-row must not be negative"));
}
#[test]
fn page_window_rejects_an_inverted_window() {
let err = validate_page_window(Some(200), Some(100)).unwrap_err();
let msg = err.to_string();
assert!(msg.contains("must be greater than"), "got: {msg}");
assert!(msg.contains("100") && msg.contains("200"), "got: {msg}");
}
#[test]
fn page_window_rejects_an_empty_window() {
let err = validate_page_window(Some(100), Some(100)).unwrap_err();
assert!(err.to_string().contains("selects no rows"));
}
#[test]
fn parse_scope_filters_allows_distinct_keys_sharing_a_value() {
let filters =
parse_scope_filters(&["service=core".to_string(), "team=core".to_string()]).unwrap();
assert_eq!(
filters,
vec![
("service".to_string(), "core".to_string()),
("team".to_string(), "core".to_string()),
]
);
}
#[test]
fn tag_profile_inserts_key_only_when_multi_profile() {
let tagged = tag_profile(json!({"a": 1}), true, "prod");
assert_eq!(tagged["profile"], "prod");
let untagged = tag_profile(json!({"a": 1}), false, "prod");
assert!(untagged.get("profile").is_none());
}
#[test]
fn aggregate_total_sums_profile_totals() {
let counts = counts(&[("prod", 150, 100), ("staging", 90, 90)]);
assert_eq!(aggregate_total(&counts), 240);
}
#[test]
fn aggregate_total_of_no_profiles_is_zero() {
assert_eq!(aggregate_total(&[]), 0);
}
#[test]
fn aggregate_total_saturates_instead_of_overflowing() {
let counts = counts(&[("a", i64::MAX, 1), ("b", 1, 1)]);
assert_eq!(aggregate_total(&counts), i64::MAX);
}
#[test]
fn envelope_reports_total_and_returned_counts() {
let counts = counts(&[("prod", 512_000, 100)]);
let envelope = build_list_envelope(512_000, rows(100), &counts, false);
assert_eq!(envelope["total_count"], 512_000);
assert_eq!(envelope["returned_count"], 100);
assert_eq!(envelope["resources"].as_array().unwrap().len(), 100);
}
#[test]
fn envelope_orders_counts_before_resources() {
let counts = counts(&[("prod", 5, 5)]);
let envelope = build_list_envelope(5, rows(5), &counts, false);
let keys: Vec<&str> = envelope
.as_object()
.unwrap()
.keys()
.map(String::as_str)
.collect();
assert_eq!(keys, vec!["total_count", "returned_count", "resources"]);
}
#[test]
fn envelope_distinguishes_a_partial_window_from_the_fleet_total() {
let counts = counts(&[("prod", 512_000, 100)]);
let envelope = build_list_envelope(512_000, rows(100), &counts, false);
assert_ne!(envelope["total_count"], envelope["returned_count"]);
assert_eq!(envelope["total_count"], 512_000);
assert_eq!(envelope["returned_count"], 100);
}
#[test]
fn envelope_returned_count_matches_the_emitted_row_count() {
let counts = counts(&[("prod", 150, 100), ("staging", 90, 90)]);
let envelope = build_list_envelope(240, rows(190), &counts, true);
assert_eq!(envelope["returned_count"], 190);
assert_eq!(
envelope["returned_count"].as_u64().unwrap() as usize,
envelope["resources"].as_array().unwrap().len()
);
}
#[test]
fn envelope_omits_per_profile_counts_for_a_single_profile() {
let counts = counts(&[("prod", 150, 100)]);
let envelope = build_list_envelope(150, rows(100), &counts, false);
assert!(envelope.get("counts_by_profile").is_none());
}
#[test]
fn envelope_breaks_counts_down_per_profile_when_fanning_out() {
let counts = counts(&[("prod", 150, 100), ("staging", 90, 90)]);
let envelope = build_list_envelope(240, rows(190), &counts, true);
let per_profile = envelope["counts_by_profile"].as_array().unwrap();
assert_eq!(per_profile.len(), 2);
assert_eq!(per_profile[0]["profile"], "prod");
assert_eq!(per_profile[0]["total_count"], 150);
assert_eq!(per_profile[0]["returned_count"], 100);
assert_eq!(per_profile[1]["profile"], "staging");
assert_eq!(per_profile[1]["total_count"], 90);
assert_eq!(per_profile[1]["returned_count"], 90);
}
#[test]
fn envelope_for_no_results_still_reports_counts() {
let envelope = build_list_envelope(0, rows(0), &counts(&[("prod", 0, 0)]), false);
assert_eq!(envelope["total_count"], 0);
assert_eq!(envelope["returned_count"], 0);
assert!(envelope["resources"].as_array().unwrap().is_empty());
}
#[test]
fn count_summary_reports_the_total() {
let counts = counts(&[("prod", 512_000, 100)]);
assert_eq!(
format_count_summary(100, 512_000, &counts, false),
"Showing 100 of 512000 total resources"
);
}
#[test]
fn count_summary_breaks_down_per_profile_when_fanning_out() {
let counts = counts(&[("prod", 150, 100), ("staging", 90, 90)]);
assert_eq!(
format_count_summary(190, 240, &counts, true),
"Showing 190 of 240 total resources\n prod: 100 of 150\n staging: 90 of 90"
);
}
#[test]
fn display_or_dash_falls_back_on_none_and_empty() {
assert_eq!(display_or_dash(Some("value")), "value");
assert_eq!(display_or_dash(Some("")), "-");
assert_eq!(display_or_dash(None), "-");
}
fn resource(name: Option<&str>, columns: &[(&str, &str)]) -> ResourceData {
ResourceData {
resource_id: Some("4013226:host_id=i-077a1626590913a16".to_string()),
name: name.map(String::from),
columns: columns
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect(),
}
}
#[test]
fn display_name_prefers_the_name_column() {
let r = resource(
Some("i-077a1626590913a16"),
&[("Name", "ip-10-109-46-236.eu-west-1.compute.internal")],
);
assert_eq!(
display_name(&r),
Some("ip-10-109-46-236.eu-west-1.compute.internal")
);
}
#[test]
fn display_name_matches_the_column_key_case_insensitively() {
let r = resource(Some("i-abc123"), &[("name", "web-server-1")]);
assert_eq!(display_name(&r), Some("web-server-1"));
}
#[test]
fn display_name_falls_back_to_the_name_field() {
let missing = resource(Some("i-abc123"), &[("region", "eu-west-1")]);
assert_eq!(display_name(&missing), Some("i-abc123"));
let empty = resource(Some("i-abc123"), &[("Name", "")]);
assert_eq!(display_name(&empty), Some("i-abc123"));
let neither = resource(None, &[]);
assert_eq!(display_name(&neither), None);
}
fn counts(entries: &[(&str, i64, usize)]) -> Vec<ProfileCounts> {
entries
.iter()
.map(|(profile, total_count, returned_count)| ProfileCounts {
profile: profile.to_string(),
total_count: *total_count,
returned_count: *returned_count,
})
.collect()
}
fn rows(n: usize) -> Vec<Value> {
(0..n)
.map(|i| json!({ "resource_id": format!("id-{i}") }))
.collect()
}
}