A library for writing host-specific, single-binary configuration management and deployment tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. use regex::Regex;
  2. use std::error::Error;
  3. use std::fmt;
  4. use std::fs::File as FsFile;
  5. use std::io::Read;
  6. use std::ops::Deref;
  7. use std::path::{Path, PathBuf};
  8. use command_runner::CommandRunner;
  9. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  10. use resources::Resource;
  11. pub struct WordpressPlugin<'a, C, R> where C: Deref<Target=str> + fmt::Display, R: 'a + CommandRunner {
  12. base: C,
  13. name: C,
  14. command_runner: &'a R
  15. }
  16. impl<'a, C, R> WordpressPlugin<'a, C, R> where C: Deref<Target=str> + fmt::Display, R: CommandRunner {
  17. pub fn new(base: C, name: C, command_runner: &'a R) -> Self {
  18. WordpressPlugin {
  19. base: base,
  20. name: name,
  21. command_runner: command_runner
  22. }
  23. }
  24. fn get_path(&self) -> PathBuf {
  25. Path::new(&*self.base).join("wp-content/plugins").join(&*self.name)
  26. }
  27. }
  28. impl<'a, C, R> Symbol for WordpressPlugin<'a, C, R> where C: Deref<Target=str> + fmt::Display, R: CommandRunner {
  29. fn target_reached(&self) -> Result<bool, Box<Error>> {
  30. if !self.get_path().exists() {
  31. return Ok(false);
  32. }
  33. let path = self.get_path().join(self.name.to_string() + ".php");
  34. let mut file = try!(FsFile::open(path));
  35. let mut file_content = String::new();
  36. try!(file.read_to_string(&mut file_content));
  37. let mut version = String::new();
  38. let mut plugin_uri = String::new();
  39. let regex = try!(Regex::new("(?m)^(Plugin URI|Version): (.+)$"));
  40. for matches in regex.captures_iter(&file_content) {
  41. if &matches[1] == "Plugin URI" {
  42. plugin_uri = matches[2].to_string();
  43. } else {
  44. version = matches[2].to_string();
  45. }
  46. }
  47. 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/"]));
  48. Ok(try!(String::from_utf8(upstream)).contains(r###""plugins":[]"###))
  49. }
  50. fn execute(&self) -> Result<(), Box<Error>> {
  51. let zip = format!("/tmp/{}.zip", self.name);
  52. try!(self.command_runner.run_successfully("curl", &[&format!("https://downloads.wordpress.org/plugin/{}.zip", self.name), "-o", &zip]));
  53. try!(self.command_runner.run_successfully("rm", &["-rf", &self.get_path().to_string_lossy()]));
  54. self.command_runner.run_successfully("unzip", &[&zip, "-d", &Path::new(&*self.base).join("wp-content/plugins").to_string_lossy()])
  55. }
  56. fn get_prerequisites(&self) -> Vec<Resource> {
  57. match self.get_path().parent() {
  58. Some(p) => vec![ Resource::new("dir", p.to_string_lossy()) ],
  59. None => vec![]
  60. }
  61. }
  62. fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
  63. Box::new(SymbolAction::new(runner, self))
  64. }
  65. fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
  66. Box::new(OwnedSymbolAction::new(runner, *self))
  67. }
  68. }
  69. impl<'a, C, R> fmt::Display for WordpressPlugin<'a, C, R> where C: Deref<Target=str> + fmt::Display, R: CommandRunner {
  70. fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
  71. write!(f, "WordpressPlugin {}", self.name)
  72. }
  73. }