use std::io::Write;
use std::process::ExitCode;
pub(crate) const USAGE: &str = "\
USAGE:
sail cache kani-pack --receipt KEY --closure DIGEST --scope SCOPE --key FILE --out BUNDLE
";
pub(crate) fn run(args: &[String], out: &mut dyn Write, err: &mut dyn Write) -> ExitCode {
match pack(args).and_then(|signer| writeln!(out, "{signer}").map_err(|error| error.to_string()))
{
Ok(()) => ExitCode::SUCCESS,
Err(error) => {
let _ = writeln!(err, "error[SAIL6373]: {error}");
ExitCode::from(2)
}
}
}
#[cfg(any(
all(target_os = "linux", target_arch = "x86_64"),
all(target_os = "macos", target_arch = "aarch64")
))]
fn pack(args: &[String]) -> Result<String, String> {
use std::collections::BTreeMap;
use std::path::Path;
let mut values = BTreeMap::new();
let (pairs, remainder) = args.as_chunks::<2>();
if !remainder.is_empty() {
return Err(USAGE.trim_end().to_owned());
}
for [flag, value] in pairs {
if !matches!(
flag.as_str(),
"--receipt" | "--closure" | "--scope" | "--key" | "--out"
) || values.insert(flag.as_str(), value.as_str()).is_some()
{
return Err(USAGE.trim_end().to_owned());
}
}
let required = |flag| {
values
.get(flag)
.copied()
.ok_or_else(|| format!("missing {flag}\n{USAGE}"))
};
let root = crate::kani_setup::root()?;
let store = sail_kani::receipts::Store::open(&root).map_err(|error| error.to_string())?;
store.sign_ci(
required("--receipt")?,
required("--closure")?,
required("--scope")?,
Path::new(required("--key")?),
Path::new(required("--out")?),
)
}
#[cfg(not(any(
all(target_os = "linux", target_arch = "x86_64"),
all(target_os = "macos", target_arch = "aarch64")
)))]
fn pack(_args: &[String]) -> Result<String, String> {
Err("the pinned Kani runtime supports native Linux x86-64 and macOS arm64".into())
}