use std::path::Path;
use serde_json::Value;
use crate::cli::args::ProjectOtelArgs;
use crate::exit_codes::{EXIT_CONFIG_ERROR, EXIT_SUCCESS};
fn read_json(path: &Path) -> anyhow::Result<Value> {
let text = std::fs::read_to_string(path)
.map_err(|e| anyhow::anyhow!("cannot read {}: {e}", path.display()))?;
serde_json::from_str(&text)
.map_err(|e| anyhow::anyhow!("invalid JSON in {}: {e}", path.display()))
}
pub fn run(args: ProjectOtelArgs) -> anyhow::Result<i32> {
let capability_surface = match read_json(&args.capability_surface) {
Ok(v) => v,
Err(e) => {
eprintln!("error: {e}");
return Ok(EXIT_CONFIG_ERROR);
}
};
let observation_health = match args
.observation_health
.as_deref()
.map(read_json)
.transpose()
{
Ok(v) => v,
Err(e) => {
eprintln!("error: {e}");
return Ok(EXIT_CONFIG_ERROR);
}
};
let enforcement_health = match args
.enforcement_health
.as_deref()
.map(read_json)
.transpose()
{
Ok(v) => v,
Err(e) => {
eprintln!("error: {e}");
return Ok(EXIT_CONFIG_ERROR);
}
};
let projection = assay_core::otel::projection::project(
&capability_surface,
observation_health.as_ref(),
enforcement_health.as_ref(),
);
let json = serde_json::to_string_pretty(&projection)?;
match &args.out {
Some(path) => {
if let Err(e) = std::fs::write(path, format!("{json}\n")) {
eprintln!("error: cannot write {}: {e}", path.display());
return Ok(EXIT_CONFIG_ERROR);
}
}
None => println!("{json}"),
}
Ok(EXIT_SUCCESS)
}