use std::error::Error; use std::fmt; use std::path::Path; use crate::command_runner::CommandRunner; use crate::resources::Resource; use crate::symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner}; pub struct AcmeAccountKey<'a, P: AsRef, C: CommandRunner> { path: P, command_runner: &'a C, } impl<'a, P: AsRef, C: CommandRunner> AcmeAccountKey<'a, P, C> { pub fn new(path: P, command_runner: &'a C) -> Self { AcmeAccountKey { path, command_runner, } } fn get_bytes(&self) -> u32 { 4096 } } impl, C: CommandRunner> fmt::Display for AcmeAccountKey<'_, P, C> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "AcmeAccountKey {}", self.path.as_ref().display()) } } impl, C: CommandRunner> Symbol for AcmeAccountKey<'_, P, C> { fn target_reached(&self) -> Result> { if !self.path.as_ref().exists() { return Ok(false); } let stdout = self.command_runner.get_output( "openssl", args![ "rsa", "-in", self.path.as_ref(), "-noout", "-check", "-text", ], )?; Ok(stdout.ends_with("RSA key ok\n".as_bytes())) } fn execute(&self) -> Result<(), Box> { self.command_runner.run_successfully( "openssl", args![ "genrsa", "-out", self.path.as_ref(), self.get_bytes().to_string(), ], ) } fn get_prerequisites(&self) -> Vec { if let Some(parent) = self.path.as_ref().parent() { vec![Resource::new("dir", parent.to_str().unwrap())] } else { vec![] } } fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box { Box::new(SymbolAction::new(runner, self)) } fn into_action<'b>(self: Box, runner: &'b dyn SymbolRunner) -> Box where Self: 'b, { Box::new(OwnedSymbolAction::new(runner, *self)) } } #[cfg(test)] mod test {}