#![allow(clippy::expect_used, clippy::panic, clippy::print_stdout)]
use std::path::Path;
use sipx_testkit::certs::Ca;
fn main() {
let mut args = std::env::args().skip(1);
let dir = args.next().expect("usage: issue-certs <directory> <host>");
let host = args.next().expect("usage: issue-certs <directory> <host>");
let dir = Path::new(&dir);
std::fs::create_dir_all(dir).expect("the output directory");
let ca = Ca::new();
let (cert, key) = ca.issue_for(&host);
write(dir, "ca.pem", &ca.pem());
write(dir, "server.pem", &cert);
write(dir, "server.key", &key);
println!("issued a certificate for {host} under {}", dir.display());
}
fn write(dir: &Path, name: &str, contents: &str) {
let path = dir.join(name);
std::fs::write(&path, contents).unwrap_or_else(|e| panic!("writing {}: {e}", path.display()));
}