use regex::Regex; use std::error::Error; use std::fmt; use std::fs::File as FsFile; use std::io; use std::io::{BufRead, BufReader}; use std::ops::Deref; use std::path::{Path, PathBuf}; use command_runner::CommandRunner; use resources::Resource; use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner}; pub struct WordpressPlugin<'a, C, R> where C: Deref, R: 'a + CommandRunner, { base: C, name: C, command_runner: &'a R, } impl<'a, C, R> WordpressPlugin<'a, C, R> where C: Deref, R: CommandRunner, { pub fn new(base: C, name: C, command_runner: &'a R) -> Self { WordpressPlugin { base, name, command_runner, } } fn get_path(&self) -> PathBuf { Path::new(&*self.base) .join("wp-content/plugins") .join(&*self.name) } } impl<'a, C, R> Symbol for WordpressPlugin<'a, C, R> where C: Deref, R: CommandRunner, { fn target_reached(&self) -> Result> { if !self.get_path().exists() { return Ok(false); } let path = self.get_path().join(self.name.to_string() + ".php"); let mut version = String::new(); let mut plugin_uri = String::new(); match FsFile::open(path) { Err(e) => { // Check if file exists return if e.kind() == io::ErrorKind::NotFound { Ok(false) } else { Err(Box::new(e)) }; } Ok(file) => { let reader = BufReader::new(file); let regex = Regex::new("(?m)^(Plugin URI|Version): (.+)$")?; for content in reader.lines() { for matches in regex.captures_iter(&(content?)) { if &matches[1] == "Plugin URI" { plugin_uri = matches[2].to_string(); } else { version = matches[2].to_string(); } } } } } let upstream = self.command_runner.get_output( "curl", &[ "--form", &format!( r###"plugins={{"plugins":{{"{0}/{0}.php":{{"Version":"{1}", "PluginURI":"{2}"}}}}}}"###, &*self.name, version, plugin_uri ), "https://api.wordpress.org/plugins/update-check/1.1/", ], )?; Ok(String::from_utf8(upstream)?.contains(r###""plugins":[]"###)) } fn execute(&self) -> Result<(), Box> { let source = format!("https://downloads.wordpress.org/plugin/{}.zip", &*self.name); let zip = format!("/tmp/{}.zip", &*self.name); self .command_runner .run_successfully("curl", &[source.as_ref() as &str, "-o", zip.as_ref()])?; self .command_runner .run_successfully("rm", &["-rf", &self.get_path().to_string_lossy()])?; self.command_runner.run_successfully( "unzip", &[ zip.as_ref(), "-d".as_ref(), Path::new(&*self.base) .join("wp-content/plugins") .as_os_str(), ], ) } fn get_prerequisites(&self) -> Vec { match self.get_path().parent() { Some(p) => vec![Resource::new("dir", p.to_string_lossy())], None => 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)) } } impl<'a, C, R> fmt::Display for WordpressPlugin<'a, C, R> where C: Deref, R: CommandRunner, { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { write!(f, "WordpressPlugin {}", &*self.name) } }