use proof_cli::{Cli, EXIT_ERR, EXIT_OK, USAGE};
fn main() {
let args: Vec<String> = std::env::args().skip(1).collect();
let code = run(&args);
std::process::exit(code);
}
fn run(args: &[String]) -> i32 {
let cli = match Cli::parse(args) {
Ok(c) => c,
Err(e) => {
eprintln!(
"error: {e}
{USAGE}"
);
return EXIT_ERR;
}
};
if cli.command == "help" || cli.has("help") {
println!("{USAGE}");
return EXIT_OK;
}
let result = match cli.command.as_str() {
"create-event" => proof_cli::artifact::write_event(&cli).map(|_| EXIT_OK),
"attest" => proof_cli::artifact::write_attestation(&cli).map(|_| EXIT_OK),
"add-evidence" => proof_cli::artifact::write_evidence(&cli).map(|_| EXIT_OK),
"relate" => proof_cli::artifact::write_relationship(&cli).map(|_| EXIT_OK),
"revoke" => proof_cli::artifact::write_status_object(&cli, "revoke").map(|_| EXIT_OK),
"supersede" => proof_cli::artifact::write_status_object(&cli, "supersede").map(|_| EXIT_OK),
"build" => proof_cli::make::build(&cli).map(|_| EXIT_OK),
"verify" => proof_cli::make::verify(&cli),
"evaluate" => proof_cli::make::evaluate(&cli, false),
"explain" => proof_cli::make::evaluate(&cli, true),
"demo" => {
let dir = cli.opt("out").unwrap_or_else(|| "demo/out".to_string());
proof_cli::demo::run(&dir)
}
other => Err(format!("unknown command `{other}` (try `proof-cli help`)")),
};
match result {
Ok(code) => code,
Err(e) => {
eprintln!("error: {e}");
EXIT_ERR
}
}
}
#[cfg(test)]
mod tests {
use super::run;
fn args(v: &[&str]) -> Vec<String> {
v.iter().map(|s| s.to_string()).collect()
}
fn tmpdir(name: &str) -> String {
let d =
std::env::temp_dir().join(format!("proof-cli-main-test-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&d);
std::fs::create_dir_all(&d).unwrap();
d.to_string_lossy().into_owned()
}
#[test]
fn usage_errors_exit_2_not_1() {
assert_eq!(run(&args(&["bogus-cmd"])), proof_cli::EXIT_ERR);
assert_eq!(
run(&args(&["verify", "--proof", "p.json"])),
proof_cli::EXIT_ERR
);
assert_eq!(
run(&args(&["verify", "--proof", "p.json", "--clock", "soon"])),
proof_cli::EXIT_ERR
);
assert_eq!(
run(&args(&[
"verify",
"--proof",
"/tmp/does-not-exist-proof-cli-test.json",
"--clock",
"1700000300"
])),
proof_cli::EXIT_ERR
);
let w = tmpdir("oversized");
let big = format!("{w}/big.json");
std::fs::write(
&big,
vec![b'x'; (proof_cli::MAX_INPUT_FILE_BYTES + 8) as usize],
)
.unwrap();
assert_eq!(
run(&args(&["verify", "--proof", &big, "--clock", "1700000300"])),
proof_cli::EXIT_ERR
);
}
#[test]
fn help_exits_0() {
assert_eq!(run(&args(&["help"])), proof_cli::EXIT_OK);
}
#[test]
fn truncated_digest_refs_rejected_as_usage_error() {
let full = "1111111111111111111111111111111111111111111111111111111111111111";
let short = &full[..40];
for flag in ["--payload-hex", "--digest-hex"] {
let cmd = if flag == "--payload-hex" {
vec![
"create-event".to_string(),
"--type".to_string(),
"x.y".to_string(),
"--subject".to_string(),
"s:1".to_string(),
"--effective-at".to_string(),
"1700000000".to_string(),
flag.to_string(),
short.to_string(),
"--out".to_string(),
"/tmp/proof-cli-must-not-write.json".to_string(),
]
} else {
vec![
"add-evidence".to_string(),
"--kind".to_string(),
"k".to_string(),
flag.to_string(),
short.to_string(),
"--out".to_string(),
"/tmp/proof-cli-must-not-write.json".to_string(),
]
};
let r = run(&cmd);
assert_eq!(
r,
proof_cli::EXIT_ERR,
"{flag}: truncated digest must be usage error"
);
assert!(
!std::path::Path::new("/tmp/proof-cli-must-not-write.json").exists(),
"no artifact may be written from a truncated digest"
);
}
}
}