mod cache;
mod error;
pub mod strategy;
use std::env;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::Command;
use crate::util;
use crate::version;
pub use error::RuntimeError;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Runtime {
pub bindir: PathBuf,
pub version: version::Version,
}
impl Runtime {
pub fn new<P: AsRef<Path>>(bindir: P) -> Result<Self, RuntimeError> {
let version = cache::version(bindir.as_ref().join("pg_ctl"))?;
Ok(Self { bindir: bindir.as_ref().to_owned(), version })
}
pub fn execute<T: AsRef<OsStr>>(&self, program: T) -> Command {
let mut command = Command::new(self.bindir.join(program.as_ref()));
command.env(
"PATH",
util::prepend_to_path(&self.bindir, env::var_os("PATH")).unwrap(),
);
command
}
pub fn command<T: AsRef<OsStr>>(&self, program: T) -> Command {
let mut command = Command::new(program);
command.env(
"PATH",
util::prepend_to_path(&self.bindir, env::var_os("PATH")).unwrap(),
);
command
}
}
#[cfg(test)]
mod tests {
use super::{Runtime, RuntimeError};
use std::env;
use std::path::PathBuf;
type TestResult = Result<(), RuntimeError>;
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() -> TestResult {
let bindir = find_bindir();
let pg = Runtime::new(&bindir)?;
assert_eq!(bindir, pg.bindir);
Ok(())
}
}