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.

49 lines
1.3 KiB

use std::borrow::Cow;
use std::error::Error;
use std::fmt;
use command_runner::CommandRunner;
use symbols::Symbol;
pub struct MariaDBUser<'a> {
user_name: Cow<'a, str>,
command_runner: &'a CommandRunner
}
impl<'a> MariaDBUser<'a> {
pub fn new(user_name: Cow<'a, str>, command_runner: &'a CommandRunner) -> MariaDBUser<'a> {
MariaDBUser {
user_name: user_name,
command_runner: command_runner
}
}
fn run_sql(&self, sql: &str) -> Result<String, Box<Error>> {
let output = try!(self.command_runner.run_with_args("mariadb", &["--skip-column-names", "-e", sql]));
if output.status.code() != Some(0) {
return Err(try!(String::from_utf8(output.stderr)).into());
}
Ok(try!(String::from_utf8(output.stdout)))
}
}
impl<'a> fmt::Display for MariaDBUser<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "MariaDB User {}", self.user_name)
}
}
impl<'a> Symbol for MariaDBUser<'a> {
fn target_reached(&self) -> Result<bool, Box<Error>> {
Ok(try!(self.run_sql(&format!("SELECT User FROM mysql.user WHERE User = '{}' AND plugin = 'unix_socket'", self.user_name))).trim_right() == self.user_name)
}
fn execute(&self) -> Result<(), Box<Error>> {
try!(self.run_sql(&format!("GRANT ALL ON {0}.* TO {0} IDENTIFIED VIA unix_socket", self.user_name)));
Ok(())
}
}
#[cfg(test)]
mod test {
}