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.

128 lines
3.6 KiB

7 years ago
6 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 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::path::{Path, PathBuf};
  8. use crate::command_runner::CommandRunner;
  9. use crate::resources::Resource;
  10. use crate::symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  11. pub struct WordpressPlugin<'a, P: AsRef<Path>, N: AsRef<str>, R: CommandRunner> {
  12. base: P,
  13. name: N,
  14. command_runner: &'a R,
  15. }
  16. impl<'a, P: AsRef<Path>, N: AsRef<str>, R: CommandRunner> WordpressPlugin<'a, P, N, R> {
  17. pub fn new(base: P, name: N, command_runner: &'a R) -> Self {
  18. WordpressPlugin {
  19. base,
  20. name,
  21. command_runner,
  22. }
  23. }
  24. fn get_path(&self) -> PathBuf {
  25. self
  26. .base
  27. .as_ref()
  28. .join("wp-content/plugins")
  29. .join(self.name.as_ref())
  30. }
  31. }
  32. impl<'a, P: AsRef<Path>, N: AsRef<str>, R: CommandRunner> Symbol for WordpressPlugin<'a, P, N, R> {
  33. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  34. let base_path = self.get_path();
  35. if !base_path.exists() {
  36. return Ok(false);
  37. }
  38. let path = base_path.join(self.name.as_ref().to_owned() + ".php");
  39. let mut version = String::new();
  40. let mut plugin_uri = String::new();
  41. match FsFile::open(path) {
  42. Err(e) => {
  43. // Check if file exists
  44. return if e.kind() == io::ErrorKind::NotFound {
  45. Ok(false)
  46. } else {
  47. Err(Box::new(e))
  48. };
  49. }
  50. Ok(file) => {
  51. let reader = BufReader::new(file);
  52. let regex = Regex::new("(?m)^(Plugin URI|Version): (.+)$")?;
  53. for content in reader.lines() {
  54. for matches in regex.captures_iter(&(content?)) {
  55. if &matches[1] == "Plugin URI" {
  56. plugin_uri = matches[2].to_string();
  57. } else {
  58. version = matches[2].to_string();
  59. }
  60. }
  61. }
  62. }
  63. }
  64. let upstream = self.command_runner.get_output(
  65. "curl",
  66. args![
  67. "--form",
  68. format!(
  69. r###"plugins={{"plugins":{{"{0}/{0}.php":{{"Version":"{1}", "PluginURI":"{2}"}}}}}}"###,
  70. self.name.as_ref(),
  71. version,
  72. plugin_uri
  73. ),
  74. "https://api.wordpress.org/plugins/update-check/1.1/",
  75. ],
  76. )?;
  77. Ok(String::from_utf8(upstream)?.contains(r###""plugins":[]"###))
  78. }
  79. fn execute(&self) -> Result<(), Box<dyn Error>> {
  80. let source = format!(
  81. "https://downloads.wordpress.org/plugin/{}.zip",
  82. self.name.as_ref()
  83. );
  84. let zip = format!("/tmp/{}.zip", self.name.as_ref());
  85. self
  86. .command_runner
  87. .run_successfully("curl", args![source, "-o", zip])?;
  88. self
  89. .command_runner
  90. .run_successfully("rm", args!["-rf", self.get_path()])?;
  91. self.command_runner.run_successfully(
  92. "unzip",
  93. args![zip, "-d", self.base.as_ref().join("wp-content/plugins")],
  94. )
  95. }
  96. fn get_prerequisites(&self) -> Vec<Resource> {
  97. match self.get_path().parent() {
  98. Some(p) => vec![Resource::new("dir", p.to_string_lossy())],
  99. None => vec![],
  100. }
  101. }
  102. fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
  103. Box::new(SymbolAction::new(runner, self))
  104. }
  105. fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
  106. where
  107. Self: 'b,
  108. {
  109. Box::new(OwnedSymbolAction::new(runner, *self))
  110. }
  111. }
  112. impl<'a, P: AsRef<Path>, N: AsRef<str>, R: CommandRunner> fmt::Display
  113. for WordpressPlugin<'a, P, N, R>
  114. {
  115. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
  116. write!(f, "WordpressPlugin {}", self.name.as_ref())
  117. }
  118. }