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.

71 lines
1.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 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
7 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
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
  1. use std::borrow::Cow;
  2. use std::error::Error;
  3. use std::fmt;
  4. use command_runner::CommandRunner;
  5. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  6. pub struct MariaDBDatabase<'a, C: 'a + CommandRunner> {
  7. db_name: Cow<'a, str>,
  8. seed_file: Cow<'a, str>,
  9. command_runner: &'a C,
  10. }
  11. impl<'a, C: CommandRunner> MariaDBDatabase<'a, C> {
  12. pub fn new(db_name: Cow<'a, str>, seed_file: Cow<'a, str>, command_runner: &'a C) -> Self {
  13. MariaDBDatabase {
  14. db_name,
  15. seed_file,
  16. command_runner,
  17. }
  18. }
  19. fn run_sql(&self, sql: &str) -> Result<String, Box<dyn Error>> {
  20. let b = self
  21. .command_runner
  22. .get_output("mariadb", &["--skip-column-names", "-B", "-e", sql])?;
  23. Ok(String::from_utf8(b)?)
  24. }
  25. }
  26. impl<'a, C: CommandRunner> fmt::Display for MariaDBDatabase<'a, C> {
  27. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  28. write!(f, "MariaDB Database {}", self.db_name)
  29. }
  30. }
  31. impl<'a, C: CommandRunner> Symbol for MariaDBDatabase<'a, C> {
  32. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  33. Ok(
  34. self
  35. .run_sql(&format!("SHOW DATABASES LIKE '{}'", self.db_name))?
  36. .trim_end()
  37. == self.db_name,
  38. )
  39. }
  40. fn execute(&self) -> Result<(), Box<dyn Error>> {
  41. self.run_sql(&format!("CREATE DATABASE {}", self.db_name))?;
  42. self.command_runner.run_successfully(
  43. "sh",
  44. &[
  45. "-c",
  46. &format!("mariadb '{}' < {}", self.db_name, self.seed_file),
  47. ],
  48. )
  49. }
  50. fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
  51. Box::new(SymbolAction::new(runner, self))
  52. }
  53. fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
  54. where
  55. Self: 'b,
  56. {
  57. Box::new(OwnedSymbolAction::new(runner, *self))
  58. }
  59. }
  60. #[cfg(test)]
  61. mod test {}