use std::path::PathBuf;
use clap::Subcommand;
use tokio::io::AsyncReadExt;
use crate::{Backend, BackendError, CustomServer};
#[derive(Subcommand, Debug)]
pub enum Command {
InstallSelfSigned {
#[clap(short, long)]
overwrite: bool,
},
Install {
private_key: PathBuf,
certificate_chain: PathBuf,
},
}
impl Command {
pub async fn execute<B: Backend>(
&self,
server: &CustomServer<B>,
) -> Result<(), BackendError<B::Error>> {
match self {
Self::InstallSelfSigned { overwrite } => {
server.install_self_signed_certificate(*overwrite).await?;
}
Self::Install {
private_key,
certificate_chain,
} => {
let mut private_key_file = tokio::fs::File::open(&private_key).await?;
let mut private_key = Vec::new();
private_key_file.read_to_end(&mut private_key).await?;
let mut certificate_chain_file = tokio::fs::File::open(&certificate_chain).await?;
let mut certificate_chain = Vec::new();
certificate_chain_file
.read_to_end(&mut certificate_chain)
.await?;
server
.install_pem_certificate(&certificate_chain, &private_key)
.await?;
}
}
Ok(())
}
}