use cu_profiler_core::Result;
use cu_profiler_report::Format;
use crate::args::RunArgs;
use crate::commands::{load_config, profile, warn_if_demo};
use crate::exit::{DecisionFlags, ExitCode, code_for_report};
pub fn run(args: &RunArgs, quiet: bool) -> Result<ExitCode> {
let loaded = load_config(&args.common.config)?;
let (report, scenarios, _baseline) = profile(&loaded, &args.common, args.baseline.as_deref())?;
warn_if_demo(&scenarios, &args.common.logs_dir, quiet);
let out = &loaded.config.output;
if let Some(path) = &out.json_path {
write_artifact(path, &cu_profiler_report::render(&report, Format::Json)?)?;
}
if let Some(path) = &out.markdown_path {
write_artifact(
path,
&cu_profiler_report::render(&report, Format::Markdown)?,
)?;
}
if let Some(path) = &out.junit_path {
write_artifact(path, &cu_profiler_report::render(&report, Format::Junit)?)?;
}
if let Some(path) = &out.html_path {
write_artifact(path, &cu_profiler_report::render(&report, Format::Html)?)?;
}
if !quiet {
print!("{}", cu_profiler_report::render(&report, Format::Table)?);
}
let flags = DecisionFlags {
fail_on_budget: args.fail_on_budget || loaded.config.defaults.fail_on_budget,
fail_on_regression: args.fail_on_regression || loaded.config.defaults.fail_on_regression,
fail_on_stale_baseline: loaded.config.defaults.fail_on_stale_baseline,
fail_on_low_confidence: args.fail_on_low_confidence || args.strict,
};
Ok(code_for_report(&report, flags))
}
fn write_artifact(path: &std::path::Path, contents: &str) -> Result<()> {
if let Some(parent) = path.parent() {
if !parent.as_os_str().is_empty() {
std::fs::create_dir_all(parent)?;
}
}
std::fs::write(path, contents)?;
Ok(())
}