Adrian Heine
8 years ago
7 changed files with 104 additions and 6 deletions
-
3src/symbols/acme/cert.rs
-
4src/symbols/git/submodules.rs
-
10src/symbols/stored_directory.rs
-
2src/symbols/systemd/node_js_user_service.rs
-
14src/symbols/tls/self_signed_cert.rs
-
1src/symbols/wordpress/mod.rs
-
76src/symbols/wordpress/plugin.rs
@ -1 +1,2 @@ |
|||||
|
pub mod plugin;
|
||||
pub mod translation;
|
pub mod translation;
|
@ -0,0 +1,76 @@ |
|||||
|
use regex::Regex;
|
||||
|
use std::error::Error;
|
||||
|
use std::fmt;
|
||||
|
use std::fs::File as FsFile;
|
||||
|
use std::io::Read;
|
||||
|
use std::ops::Deref;
|
||||
|
use std::path::{Path, PathBuf};
|
||||
|
|
||||
|
use command_runner::CommandRunner;
|
||||
|
use symbols::Symbol;
|
||||
|
use resources::Resource;
|
||||
|
|
||||
|
pub struct WordpressPlugin<'a, C, R> where C: Deref<Target=str> + fmt::Display, R: 'a + CommandRunner {
|
||||
|
base: C,
|
||||
|
name: C,
|
||||
|
command_runner: &'a R
|
||||
|
}
|
||||
|
|
||||
|
impl<'a, C, R> WordpressPlugin<'a, C, R> where C: Deref<Target=str> + fmt::Display, R: CommandRunner {
|
||||
|
pub fn new(base: C, name: C, command_runner: &'a R) -> Self {
|
||||
|
WordpressPlugin {
|
||||
|
base: base,
|
||||
|
name: name,
|
||||
|
command_runner: 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<Target=str> + fmt::Display, R: CommandRunner {
|
||||
|
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
||||
|
if !self.get_path().exists() {
|
||||
|
return Ok(false);
|
||||
|
}
|
||||
|
let path = self.get_path().join(self.name.to_string() + ".php");
|
||||
|
let mut file = try!(FsFile::open(path));
|
||||
|
let mut file_content = String::new();
|
||||
|
try!(file.read_to_string(&mut file_content));
|
||||
|
let mut version = String::new();
|
||||
|
let mut plugin_uri = String::new();
|
||||
|
let regex = try!(Regex::new("(?m)^(Plugin URI|Version): (.+)$"));
|
||||
|
for matches in regex.captures_iter(&file_content) {
|
||||
|
if &matches[1] == "Plugin URI" {
|
||||
|
plugin_uri = matches[2].to_string();
|
||||
|
} else {
|
||||
|
version = matches[2].to_string();
|
||||
|
}
|
||||
|
}
|
||||
|
let upstream = try!(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(try!(String::from_utf8(upstream)).contains(r###""plugins":[]"###))
|
||||
|
}
|
||||
|
|
||||
|
fn execute(&self) -> Result<(), Box<Error>> {
|
||||
|
let zip = format!("/tmp/{}.zip", self.name);
|
||||
|
try!(self.command_runner.run_successfully("curl", &[&format!("https://downloads.wordpress.org/plugin/{}.zip", self.name), "-o", &zip]));
|
||||
|
try!(self.command_runner.run_successfully("rm", &["-rf", &self.get_path().to_string_lossy()]));
|
||||
|
self.command_runner.run_successfully("unzip", &[&zip, "-d", &Path::new(&*self.base).join("wp-content/plugins").to_string_lossy()])
|
||||
|
}
|
||||
|
|
||||
|
fn get_prerequisites(&self) -> Vec<Resource> {
|
||||
|
match self.get_path().parent() {
|
||||
|
Some(p) => vec![ Resource::new("dir", p.to_string_lossy()) ],
|
||||
|
None => vec![]
|
||||
|
}
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
impl<'a, C, R> fmt::Display for WordpressPlugin<'a, C, R> where C: Deref<Target=str> + fmt::Display, R: CommandRunner {
|
||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
|
||||
|
write!(f, "WordpressPlugin {}", self.name)
|
||||
|
}
|
||||
|
}
|
||||
|
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue