use anyhow::{Context, Result};
use clap::Parser;
use cmx::profile::{Profile, RawProfile};
use std::fs;
use std::io::{self, Write};
use std::path::PathBuf;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[arg(value_name = "PROFILE")]
profile: PathBuf,
#[arg(short, long, value_name = "FILE")]
output: Option<PathBuf>,
}
fn main() -> Result<()> {
let cli = Cli::parse();
let profile_bytes = fs::read(&cli.profile)
.with_context(|| format!("Failed to read profile from {:?}", cli.profile))?;
let raw_profile = RawProfile::from_bytes(&profile_bytes)
.map_err(|e| anyhow::anyhow!("{}", e))
.with_context(|| format!("Failed to parse profile {:?}", cli.profile))?;
let profile = Profile::Raw(raw_profile);
let toml_output = profile.to_string();
if let Some(output_path) = cli.output {
fs::write(&output_path, toml_output)
.with_context(|| format!("Failed to write to {output_path:?}"))?;
} else {
io::stdout().write_all(toml_output.as_bytes())?;
}
Ok(())
}