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.

98 lines
3.4 KiB

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