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.

73 lines
1.9 KiB

use std::error::Error;
use std::fmt;
use crate::command_runner::CommandRunner;
use crate::resources::Resource;
use crate::symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
pub struct MariaDBUser<'a, U: AsRef<str>, C: CommandRunner> {
user_name: U,
command_runner: &'a C,
}
impl<'a, U: AsRef<str>, C: CommandRunner> MariaDBUser<'a, U, C> {
pub fn new(user_name: U, command_runner: &'a C) -> Self {
MariaDBUser {
user_name,
command_runner,
}
}
fn run_sql(&self, sql: &str) -> Result<String, Box<dyn Error>> {
let b = self
.command_runner
.get_output("mariadb", args!["--skip-column-names", "-B", "-e", sql])?;
Ok(String::from_utf8(b)?)
}
}
impl<U: AsRef<str>, C: CommandRunner> fmt::Display for MariaDBUser<'_, U, C> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "MariaDB User {}", self.user_name.as_ref())
}
}
impl<U: AsRef<str>, C: CommandRunner> Symbol for MariaDBUser<'_, U, C> {
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
Ok(
self
.run_sql(&format!(
"SELECT User FROM mysql.user WHERE User = '{}' AND plugin = 'unix_socket'",
self.user_name.as_ref()
))?
.trim_end()
== self.user_name.as_ref(),
)
}
fn execute(&self) -> Result<(), Box<dyn Error>> {
self.run_sql(&format!(
"GRANT ALL ON {0}.* TO {0} IDENTIFIED VIA unix_socket",
self.user_name.as_ref()
))?;
Ok(())
}
fn get_prerequisites(&self) -> Vec<Resource> {
vec![Resource::new("user", self.user_name.as_ref())]
}
fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
Box::new(SymbolAction::new(runner, self))
}
fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
where
Self: 'b,
{
Box::new(OwnedSymbolAction::new(runner, *self))
}
}
#[cfg(test)]
mod test {}