#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
pub async fn handle_quality_gates_command(
command: Option<QualityGatesCommand>,
config_path: PathBuf,
report: bool,
json: bool,
project_dir: PathBuf,
) -> Result<()> {
match command {
Some(QualityGatesCommand::Init { force }) => {
handle_init_config(&config_path, force)?;
}
Some(QualityGatesCommand::Validate) => {
handle_validate_config(&config_path)?;
}
Some(QualityGatesCommand::Show { format }) => {
handle_show_config(&config_path, format)?;
}
None => {
run_quality_gates(config_path, report, json, project_dir).await?;
}
}
Ok(())
}
async fn run_quality_gates(
config_path: PathBuf,
report: bool,
json: bool,
project_dir: PathBuf,
) -> Result<()> {
let config = if config_path.exists() {
load_config_from_file(&config_path)?
} else {
GateConfig::default()
};
let gate_report = execute_all_gates(&config, &project_dir)?;
if json {
output_json(&gate_report)?;
} else if report {
output_markdown(&gate_report)?;
} else {
output_summary(&gate_report)?;
}
if gate_report.passed {
Ok(())
} else {
std::process::exit(1);
}
}
fn handle_init_config(path: &Path, force: bool) -> Result<()> {
if path.exists() && !force {
return Err(anyhow::anyhow!(
"Configuration file already exists. Use --force to overwrite."
));
}
let config = generate_default_config();
std::fs::write(path, config)?;
println!("✅ Created {}", path.display());
Ok(())
}
fn handle_validate_config(path: &Path) -> Result<()> {
let config = load_config_from_file(&path.to_path_buf())?;
match validate_config(&config) {
Ok(()) => {
println!("✅ Configuration is valid");
Ok(())
}
Err(errors) => {
println!("❌ Configuration has {} error(s):", errors.len());
for error in errors {
println!(" - {}", error);
}
std::process::exit(1);
}
}
}
fn handle_show_config(path: &Path, format: ConfigFormat) -> Result<()> {
let config = load_config_from_file(&path.to_path_buf())?;
match format {
ConfigFormat::Toml => {
let toml = generate_config_toml(&config);
println!("{}", toml);
}
ConfigFormat::Json => {
let json = serde_json::to_string_pretty(&config)?;
println!("{}", json);
}
ConfigFormat::Env => {
return Err(anyhow::anyhow!(
"Environment variable format not supported for quality gates configuration"
));
}
}
Ok(())
}
fn load_config_from_file(path: &PathBuf) -> Result<GateConfig> {
let content = std::fs::read_to_string(path)?;
let config: GateConfigToml = toml::from_str(&content)?;
Ok(config.into())
}