use std::path::PathBuf;
use std::{io::IsTerminal, process::ExitCode};
use brake::{Severity, Verdict};
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(
name = "brake",
version = brake::VERSION,
about = "A brake on breaking API changes",
long_about = None,
)]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
Check {
paths: Vec<PathBuf>,
#[arg(long)]
since: Option<String>,
#[arg(long, default_value = "brake.toml")]
config: PathBuf,
#[arg(long, default_value = "warning")]
severity: String,
#[arg(long)]
as_of: Option<String>,
#[arg(long, short, default_value = "auto")]
format: String,
#[arg(long)]
drift: bool,
},
Analyze {
#[arg(default_value = ".")]
path: PathBuf,
#[arg(long, default_value = "brake.toml")]
config: PathBuf,
#[arg(long, short, default_value = "auto")]
format: String,
#[arg(long, default_value = "warning")]
fail_on: String,
},
Diff {
#[arg(long, default_value = "brake.toml")]
config: PathBuf,
#[arg(long, short, default_value = "auto")]
format: String,
},
Explain {
rule: String,
},
}
fn main() -> std::process::ExitCode {
let cli = Cli::parse();
if let Command::Explain { rule } = &cli.command {
if let Some(definition) = brake::rules::catalogue::lookup(rule) {
println!(
"{}\nseverity: {:?}\nsummary: {}\n\n{}",
definition.id, definition.severity, definition.summary, definition.explanation
);
return std::process::ExitCode::from(Verdict::Clean.exit_code() as u8);
}
eprintln!(
"brake {}: unknown rule id `{}`.\n\
\n\
Use a rule ID from the catalogue. Exiting {}.",
brake::VERSION,
rule,
Verdict::ToolFailure.exit_code()
);
return std::process::ExitCode::from(Verdict::ToolFailure.exit_code() as u8);
}
if let Command::Check {
since,
config,
severity,
as_of,
format,
drift,
..
} = &cli.command
{
let config_value = match brake::config::Config::from_path(config) {
Ok(value) => value,
Err(error) => {
eprintln!("{error}");
return ExitCode::from(Verdict::ToolFailure.exit_code() as u8);
}
};
let threshold = match parse_severity(severity) {
Ok(value) => value,
Err(message) => {
eprintln!("{message}");
return ExitCode::from(Verdict::ToolFailure.exit_code() as u8);
}
};
let report = brake::check::check_contracts(
&PathBuf::from("."),
&config_value,
since.as_deref(),
as_of.as_deref(),
*drift,
);
let output = match render_report(&report, format) {
Ok(value) => value,
Err(error) => {
eprintln!("{error}");
return ExitCode::from(Verdict::ToolFailure.exit_code() as u8);
}
};
print!("{output}");
return ExitCode::from(report.exit_code(threshold) as u8);
}
if let Command::Analyze {
path,
config,
fail_on,
format,
..
} = &cli.command
{
let config_value = match brake::config::Config::from_path(config) {
Ok(value) => value,
Err(error) => {
eprintln!("{error}");
return ExitCode::from(Verdict::ToolFailure.exit_code() as u8);
}
};
let threshold = match parse_severity(fail_on) {
Ok(value) => value,
Err(message) => {
eprintln!("{message}");
return ExitCode::from(Verdict::ToolFailure.exit_code() as u8);
}
};
let report = brake::check::check_contracts(path, &config_value, None, None, false);
let output = match render_report(&report, format) {
Ok(value) => value,
Err(error) => {
eprintln!("{error}");
return ExitCode::from(Verdict::ToolFailure.exit_code() as u8);
}
};
print!("{output}");
return ExitCode::from(report.exit_code(threshold) as u8);
}
if let Command::Diff { config, format } = &cli.command {
let config_value = match brake::config::Config::from_path(config) {
Ok(value) => value,
Err(error) => {
eprintln!("{error}");
return ExitCode::from(Verdict::ToolFailure.exit_code() as u8);
}
};
let report =
brake::check::check_contracts(&PathBuf::from("."), &config_value, None, None, false);
let output = match render_report(&report, format) {
Ok(value) => value,
Err(error) => {
eprintln!("{error}");
return ExitCode::from(Verdict::ToolFailure.exit_code() as u8);
}
};
print!("{output}");
return ExitCode::from(Verdict::Clean.exit_code() as u8);
}
let unimplemented = match &cli.command {
Command::Diff { .. } => "diff",
Command::Explain { .. } => unreachable!("handled above"),
Command::Check { .. } | Command::Analyze { .. } => unreachable!("handled above"),
};
eprintln!(
"brake {}: `{unimplemented}` is not implemented yet.\n\
\n\
The design is complete and the build order is in\n\
design/03-implementation-plan.md — M1 is the walking skeleton.\n\
\n\
Exiting {} (tool failure) rather than 0, deliberately: a gate that\n\
reports clean when it cannot answer is worse than no gate.",
brake::VERSION,
Verdict::ToolFailure.exit_code(),
);
ExitCode::from(Verdict::ToolFailure.exit_code() as u8)
}
fn parse_severity(input: &str) -> Result<Severity, String> {
match input.trim().to_ascii_lowercase().as_str() {
"info" => Ok(Severity::Info),
"warning" | "warn" => Ok(Severity::Warning),
"error" | "err" => Ok(Severity::Error),
_ => Err(format!(
"unknown severity `{input}`; expected one of: info, warning, error"
)),
}
}
fn render_report(report: &brake::report::Report, requested_format: &str) -> Result<String, String> {
match normalize_format(requested_format)? {
OutputFormat::Text => Ok(brake::render::text::render(report)),
OutputFormat::Json => Ok(brake::render::json::render(report)),
OutputFormat::Sarif => Ok(brake::render::sarif::render(report)),
}
}
fn normalize_format(requested_format: &str) -> Result<OutputFormat, String> {
let normalized = requested_format.trim().to_ascii_lowercase();
match normalized.as_str() {
"text" => Ok(OutputFormat::Text),
"json" => Ok(OutputFormat::Json),
"sarif" => Ok(OutputFormat::Sarif),
"auto" => {
if std::io::stdout().is_terminal() {
Ok(OutputFormat::Text)
} else {
Ok(OutputFormat::Json)
}
}
_ => Err(format!(
"unknown format `{requested_format}`; expected one of: auto, text, json, sarif"
)),
}
}
enum OutputFormat {
Text,
Json,
Sarif,
}