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.

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