use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;
pub use version::{Version, VersionError};
use util;
pub struct Runtime {
pub bindir: Option<PathBuf>,
}
impl Default for Runtime {
fn default() -> Self {
Self { bindir: None }
}
}
impl Runtime {
pub fn new<P: AsRef<Path>>(bindir: P) -> Self {
Self {
bindir: Some(bindir.as_ref().to_path_buf()),
}
}
pub fn version(&self) -> Result<Version, VersionError> {
let version_output = self.execute("pg_ctl").arg("--version").output()?;
let version_string = String::from_utf8_lossy(&version_output.stdout);
Ok(version_string.parse()?)
}
pub fn execute(&self, program: &str) -> Command {
let mut command;
match self.bindir {
Some(ref bindir) => {
command = Command::new(bindir.join(program));
command.env(
"PATH",
util::prepend_to_path(&bindir, env::var_os("PATH")).unwrap(),
);
}
None => {
command = Command::new(program);
}
}
command
}
}
#[cfg(test)]
mod tests {
extern crate tempdir;
use super::Runtime;
use std::env;
use std::path::PathBuf;
fn find_bindir() -> PathBuf {
env::split_paths(&env::var_os("PATH").expect("PATH not set"))
.find(|path| path.join("pg_ctl").exists())
.expect("pg_ctl not on PATH")
}
#[test]
fn runtime_new() {
let bindir = find_bindir();
let pg = Runtime::new(&bindir);
assert_eq!(Some(bindir), pg.bindir);
}
#[test]
fn runtime_default() {
let pg = Runtime::default();
assert_eq!(None, pg.bindir);
let pg: Runtime = Default::default(); assert_eq!(None, pg.bindir);
}
}