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.

77 lines
2.0 KiB

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