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.

110 lines
2.8 KiB

7 years ago
6 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
7 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
7 years ago
5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
  1. use crate::command_runner::CommandRunner;
  2. use crate::symbols::Symbol;
  3. use async_trait::async_trait;
  4. use regex::Regex;
  5. use std::error::Error;
  6. use std::fs::File as FsFile;
  7. use std::io;
  8. use std::io::{BufRead, BufReader};
  9. use std::path::{Path, PathBuf};
  10. #[derive(Debug)]
  11. pub struct Plugin<'a, P, N, R> {
  12. base: P,
  13. name: N,
  14. command_runner: &'a R,
  15. }
  16. impl<'a, P: AsRef<Path>, N: AsRef<str>, R: CommandRunner> Plugin<'a, P, N, R> {
  17. pub fn new(base: P, name: N, command_runner: &'a R) -> Self {
  18. Self {
  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. #[async_trait(?Send)]
  33. impl<P: AsRef<Path>, N: AsRef<str>, R: CommandRunner> Symbol for Plugin<'_, P, N, R> {
  34. async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  35. let base_path = self.get_path();
  36. if !base_path.exists() {
  37. return Ok(false);
  38. }
  39. let path = base_path.join(self.name.as_ref().to_owned() + ".php");
  40. let mut version = String::new();
  41. let mut plugin_uri = String::new();
  42. match FsFile::open(path) {
  43. Err(e) => {
  44. // Check if file exists
  45. return if e.kind() == io::ErrorKind::NotFound {
  46. Ok(false)
  47. } else {
  48. Err(Box::new(e))
  49. };
  50. }
  51. Ok(file) => {
  52. let reader = BufReader::new(file);
  53. let regex = Regex::new("(?m)^(Plugin URI|Version): (.+)$")?;
  54. for content in reader.lines() {
  55. for matches in regex.captures_iter(&(content?)) {
  56. if &matches[1] == "Plugin URI" {
  57. plugin_uri = matches[2].to_string();
  58. } else {
  59. version = matches[2].to_string();
  60. }
  61. }
  62. }
  63. }
  64. }
  65. let upstream = self
  66. .command_runner
  67. .get_output(
  68. "curl",
  69. args![
  70. "--form",
  71. format!(
  72. r###"plugins={{"plugins":{{"{0}/{0}.php":{{"Version":"{1}", "PluginURI":"{2}"}}}}}}"###,
  73. self.name.as_ref(),
  74. version,
  75. plugin_uri
  76. ),
  77. "https://api.wordpress.org/plugins/update-check/1.1/",
  78. ],
  79. )
  80. .await?;
  81. Ok(String::from_utf8(upstream)?.contains(r###""plugins":[]"###))
  82. }
  83. async fn execute(&self) -> Result<(), Box<dyn Error>> {
  84. let source = format!(
  85. "https://downloads.wordpress.org/plugin/{}.zip",
  86. self.name.as_ref()
  87. );
  88. let zip = format!("/tmp/{}.zip", self.name.as_ref());
  89. self
  90. .command_runner
  91. .run_successfully("curl", args![source, "-o", zip])
  92. .await?;
  93. self
  94. .command_runner
  95. .run_successfully("rm", args!["-rf", self.get_path()])
  96. .await?;
  97. self
  98. .command_runner
  99. .run_successfully(
  100. "unzip",
  101. args![zip, "-d", self.base.as_ref().join("wp-content/plugins")],
  102. )
  103. .await
  104. }
  105. }