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.

47 lines
1.2 KiB

use std::io::Result as IoResult;
use std::process::Command;
use std::process::Output;
pub trait CommandRunner {
fn run_with_args(&self, program: &str, args: &[&str]) -> IoResult<Output>;
}
#[derive(Debug)]
pub struct StdCommandRunner;
impl CommandRunner for StdCommandRunner {
fn run_with_args(&self, program: &str, args: &[&str]) -> IoResult<Output> {
// FIXME: logger
println!("{} {:?}", program, args);
let res = Command::new(program).args(args).output();
println!("{:?}", res);
res
}
}
#[derive(Debug)]
pub struct UserCommandRunner<'a, C> where C: 'a + CommandRunner {
command_runner: &'a C,
user_name: &'a str
}
impl<'a, C> UserCommandRunner<'a, C> where C: 'a + CommandRunner {
pub fn new(user_name: &'a str, command_runner: &'a C) -> UserCommandRunner<'a, C> {
UserCommandRunner {
command_runner: command_runner,
user_name: user_name
}
}
}
impl<'a, C> CommandRunner for UserCommandRunner<'a, C> where C: 'a + CommandRunner {
fn run_with_args(&self, program: &str, args: &[&str]) -> IoResult<Output> {
let mut new_args = vec![self.user_name, "-s", "/usr/bin/env", "--", program];
new_args.extend_from_slice(args);
self.command_runner.run_with_args("su", &new_args)
}
}
#[cfg(test)]
mod test {
}