use std::borrow::{Borrow, Cow}; use std::error::Error; use std::fmt; use std::path::Path; use command_runner::CommandRunner; use resources::Resource; use symbols::Symbol; pub struct AcmeAccountKey<'a> { path: Cow<'a, str>, command_runner: &'a CommandRunner } impl<'a> AcmeAccountKey<'a> { pub fn new(path: Cow<'a, str>, command_runner: &'a CommandRunner) -> AcmeAccountKey<'a> { AcmeAccountKey { path: path, command_runner: command_runner } } fn get_path(&self) -> &str { self.path.borrow() } fn get_bytes(&self) -> u32 { 4096 } } impl<'a> fmt::Display for AcmeAccountKey<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "AcmeAccountKey {}", self.path) } } impl<'a> Symbol for AcmeAccountKey<'a> { fn target_reached(&self) -> Result> { if !Path::new(self.get_path()).exists() { return Ok(false); } let stdout = try!(self.command_runner.get_output("openssl", &["rsa", "-in", self.get_path(), "-noout", "-check", "-text"])); Ok(stdout.starts_with(&format!("Private-Key: ({} bit)\n", self.get_bytes()).as_bytes())) } fn execute(&self) -> Result<(), Box> { self.command_runner.run_successfully("openssl", &["genrsa", "-out", self.get_path(), &self.get_bytes().to_string()]) } fn get_prerequisites(&self) -> Vec { vec![ Resource::new("dir", Path::new(self.get_path()).parent().unwrap().to_string_lossy() ) ] } } #[cfg(test)] mod test { }