use anyhow::Result;
use serde::Serialize;
use std::path::Path;
use crate::release_ops::{
run_contract_checks, run_db_drift_check, run_db_lifecycle_check, run_db_rollback_check,
run_db_rollback_rehearsal, run_dependency_gate, run_protocol_contract_checks,
run_release_certify, run_release_check,
};
use crate::{ContractAction, DbAction, ReleaseAction, SecurityAction};
#[derive(Debug, Serialize)]
struct GateEnvelope<'a> {
command: &'a str,
status: &'static str,
error: Option<String>,
}
fn finish_gate(command: &str, json: bool, result: Result<()>) -> Result<()> {
if !json {
return result;
}
let envelope = match &result {
Ok(()) => GateEnvelope {
command,
status: "passed",
error: None,
},
Err(err) => GateEnvelope {
command,
status: "failed",
error: Some(format!("{err:#}")),
},
};
if let Ok(rendered) = serde_json::to_string_pretty(&envelope) {
println!("{rendered}");
}
result
}
const FRAMEWORK_MARKER_MEMBER: &str = "crates/framework/krab_core";
pub(crate) fn is_framework_workspace_at(root: &Path) -> bool {
let Ok(raw) = std::fs::read_to_string(root.join("Cargo.toml")) else {
return false;
};
let Ok(manifest) = toml::from_str::<toml::Value>(&raw) else {
return false;
};
let declares_core = manifest
.get("workspace")
.and_then(|workspace| workspace.get("members"))
.and_then(toml::Value::as_array)
.is_some_and(|members| {
members
.iter()
.filter_map(toml::Value::as_str)
.any(|member| {
member.replace('\\', "/").trim_end_matches('/') == FRAMEWORK_MARKER_MEMBER
})
});
declares_core && root.join(FRAMEWORK_MARKER_MEMBER).is_dir()
}
pub(crate) fn framework_only_refusal(command: &str) -> String {
format!(
"`krab {command}` validates the Krab framework's own reference services \
(service_auth, service_users, krab_core) and only runs inside a Krab framework \
checkout; this directory is not one (./Cargo.toml has no `{FRAMEWORK_MARKER_MEMBER}` \
workspace member). For your project, run `cargo test`, `krab doctor --strict`, \
`krab topology doctor` and `krab security dependency-gate` instead."
)
}
fn require_framework_workspace(command: &str) -> Result<()> {
if is_framework_workspace_at(Path::new(".")) {
Ok(())
} else {
anyhow::bail!("{}", framework_only_refusal(command))
}
}
fn framework_gate(command: &str, json: bool, gate: impl FnOnce() -> Result<()>) -> Result<()> {
let result = require_framework_workspace(command).and_then(|()| gate());
finish_gate(command, json, result)
}
pub(super) fn dispatch_contract_action(
action: &ContractAction,
diagnostics: bool,
json: bool,
) -> Result<()> {
match action {
ContractAction::Check => {
framework_gate("contract check", json, || run_contract_checks(diagnostics))
}
ContractAction::ProtocolCheck => framework_gate("contract protocol-check", json, || {
run_protocol_contract_checks(diagnostics)
}),
}
}
pub(super) fn dispatch_db_action(action: &DbAction, diagnostics: bool, json: bool) -> Result<()> {
match action {
DbAction::Lifecycle => {
framework_gate("db lifecycle", json, || run_db_lifecycle_check(diagnostics))
}
DbAction::Rollback => {
framework_gate("db rollback", json, || run_db_rollback_check(diagnostics))
}
DbAction::Drift => framework_gate("db drift", json, || run_db_drift_check(diagnostics)),
DbAction::Rehearsal { out } => framework_gate("db rehearsal", json, || {
let out = out
.clone()
.unwrap_or_else(crate::artifacts::default_rehearsal_evidence_path);
run_db_rollback_rehearsal(&out, diagnostics)
}),
}
}
pub(super) fn dispatch_security_action(
action: &SecurityAction,
diagnostics: bool,
json: bool,
) -> Result<()> {
match action {
SecurityAction::DependencyGate => finish_gate(
"security dependency-gate",
json,
run_dependency_gate(diagnostics),
),
}
}
pub(super) fn dispatch_release_action(
action: &ReleaseAction,
diagnostics: bool,
json: bool,
) -> Result<()> {
match action {
ReleaseAction::Check => {
require_framework_workspace("release check")?;
run_release_check(diagnostics, json)
}
ReleaseAction::Certify { out } => {
require_framework_workspace("release certify")?;
let out = out
.clone()
.unwrap_or_else(crate::artifacts::default_release_certify_dir);
run_release_certify(&out, diagnostics, json)
}
}
}
#[cfg(test)]
mod tests {
use super::{finish_gate, framework_only_refusal, is_framework_workspace_at};
use std::fs;
use std::path::Path;
fn workspace_fixture(manifest: &str, create_core_dir: bool) -> tempfile::TempDir {
let dir = tempfile::tempdir().expect("tempdir");
fs::write(dir.path().join("Cargo.toml"), manifest).expect("write manifest");
if create_core_dir {
fs::create_dir_all(dir.path().join("crates/framework/krab_core")).expect("mkdir");
}
dir
}
#[test]
fn this_repository_is_recognised_as_the_framework_workspace() {
let repo_root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../..");
assert!(is_framework_workspace_at(&repo_root));
}
#[test]
fn a_generated_project_manifest_is_not_the_framework_workspace() {
let dir = workspace_fixture(
"[package]\nname = \"demo\"\nversion = \"0.1.0\"\n\n[dependencies]\n\
krab_core = { path = \"../krab/crates/framework/krab_core\" }\n",
false,
);
assert!(!is_framework_workspace_at(dir.path()));
}
#[test]
fn a_directory_without_a_manifest_is_not_the_framework_workspace() {
let dir = tempfile::tempdir().expect("tempdir");
assert!(!is_framework_workspace_at(dir.path()));
}
#[test]
fn the_member_declaration_alone_or_the_directory_alone_is_not_enough() {
let declared_only = workspace_fixture(
"[workspace]\nmembers = [\"crates/framework/krab_core\"]\n",
false,
);
assert!(!is_framework_workspace_at(declared_only.path()));
let directory_only = workspace_fixture("[workspace]\nmembers = [\"services/app\"]\n", true);
assert!(!is_framework_workspace_at(directory_only.path()));
let both = workspace_fixture(
"[workspace]\nmembers = [\n \"crates/framework/krab_core\",\n]\n",
true,
);
assert!(is_framework_workspace_at(both.path()));
}
#[test]
fn an_unparseable_manifest_is_not_the_framework_workspace() {
let dir = workspace_fixture("this is [not toml", true);
assert!(!is_framework_workspace_at(dir.path()));
}
#[test]
fn the_refusal_names_the_command_its_purpose_and_the_alternatives() {
let message = framework_only_refusal("db lifecycle");
assert!(message.contains("`krab db lifecycle`"), "{message}");
assert!(
message.contains("Krab framework's own reference services"),
"{message}"
);
for alternative in [
"cargo test",
"krab doctor",
"krab topology doctor",
"krab security dependency-gate",
] {
assert!(message.contains(alternative), "{message}");
}
assert_eq!(message.lines().count(), 1, "{message}");
}
#[test]
fn the_json_envelope_does_not_change_the_exit_outcome() {
assert!(finish_gate("db drift", true, Ok(())).is_ok());
assert!(finish_gate("db drift", false, Ok(())).is_ok());
assert!(finish_gate("db drift", true, Err(anyhow::anyhow!("boom"))).is_err());
assert!(finish_gate("db drift", false, Err(anyhow::anyhow!("boom"))).is_err());
}
#[test]
fn the_envelope_carries_the_command_name_and_error_text() {
let envelope = super::GateEnvelope {
command: "security dependency-gate",
status: "failed",
error: Some("Command failed: cargo deny".to_string()),
};
let rendered = serde_json::to_string(&envelope).expect("envelope serializes");
let parsed: serde_json::Value =
serde_json::from_str(&rendered).expect("envelope round-trips");
assert_eq!(parsed["command"], "security dependency-gate");
assert_eq!(parsed["status"], "failed");
assert_eq!(parsed["error"], "Command failed: cargo deny");
}
}