use crate::Settings;
use crate::traits::CommandBuilder;
use std::convert::AsRef;
use std::ffi::{OsStr, OsString};
use std::path::PathBuf;
#[derive(Clone, Debug, Default)]
pub struct PgWalDumpBuilder {
program_dir: Option<PathBuf>,
envs: Vec<(OsString, OsString)>,
start_segment: Option<OsString>,
end_segment: Option<OsString>,
backkup_details: bool,
block: Option<OsString>,
end: Option<OsString>,
follow: bool,
fork: Option<OsString>,
limit: Option<OsString>,
path: Option<OsString>,
quiet: bool,
rmgr: Vec<OsString>,
relation: Option<OsString>,
start: Option<OsString>,
timeline: Option<OsString>,
version: bool,
fullpage: bool,
xid: Option<OsString>,
stats: Option<OsString>,
save_fullpage: Option<OsString>,
help: bool,
}
impl PgWalDumpBuilder {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn from(settings: &dyn Settings) -> Self {
Self::new().program_dir(settings.get_binary_dir())
}
#[must_use]
pub fn program_dir<P: Into<PathBuf>>(mut self, path: P) -> Self {
self.program_dir = Some(path.into());
self
}
#[must_use]
pub fn backup_details(mut self) -> Self {
self.backkup_details = true;
self
}
#[must_use]
pub fn block<S: AsRef<OsStr>>(mut self, block: S) -> Self {
self.block = Some(block.as_ref().to_os_string());
self
}
#[must_use]
pub fn end<S: AsRef<OsStr>>(mut self, end: S) -> Self {
self.end = Some(end.as_ref().to_os_string());
self
}
#[must_use]
pub fn follow(mut self) -> Self {
self.follow = true;
self
}
#[must_use]
pub fn fork<S: AsRef<OsStr>>(mut self, fork: S) -> Self {
self.fork = Some(fork.as_ref().to_os_string());
self
}
#[must_use]
pub fn limit<S: AsRef<OsStr>>(mut self, limit: S) -> Self {
self.limit = Some(limit.as_ref().to_os_string());
self
}
#[must_use]
pub fn path<S: AsRef<OsStr>>(mut self, path: S) -> Self {
self.path = Some(path.as_ref().to_os_string());
self
}
#[must_use]
pub fn quiet(mut self) -> Self {
self.quiet = true;
self
}
#[must_use]
pub fn rmgr<S: AsRef<OsStr>>(mut self, rmgr: S) -> Self {
self.rmgr.push(rmgr.as_ref().to_os_string());
self
}
#[must_use]
pub fn relation<S: AsRef<OsStr>>(mut self, relation: S) -> Self {
self.relation = Some(relation.as_ref().to_os_string());
self
}
#[must_use]
pub fn start<S: AsRef<OsStr>>(mut self, start: S) -> Self {
self.start = Some(start.as_ref().to_os_string());
self
}
#[must_use]
pub fn timeline<S: AsRef<OsStr>>(mut self, timeline: S) -> Self {
self.timeline = Some(timeline.as_ref().to_os_string());
self
}
#[must_use]
pub fn version(mut self) -> Self {
self.version = true;
self
}
#[must_use]
pub fn fullpage(mut self) -> Self {
self.fullpage = true;
self
}
#[must_use]
pub fn xid<S: AsRef<OsStr>>(mut self, xid: S) -> Self {
self.xid = Some(xid.as_ref().to_os_string());
self
}
#[must_use]
pub fn stats<S: AsRef<OsStr>>(mut self, stats: S) -> Self {
self.stats = Some(stats.as_ref().to_os_string());
self
}
#[must_use]
pub fn save_fullpage<S: AsRef<OsStr>>(mut self, save_fullpage: S) -> Self {
self.save_fullpage = Some(save_fullpage.as_ref().to_os_string());
self
}
#[must_use]
pub fn help(mut self) -> Self {
self.help = true;
self
}
#[must_use]
pub fn start_segment<S: AsRef<OsStr>>(mut self, start_segment: S) -> Self {
self.start_segment = Some(start_segment.as_ref().to_os_string());
self
}
#[must_use]
pub fn end_segment<S: AsRef<OsStr>>(mut self, end_segment: S) -> Self {
self.end_segment = Some(end_segment.as_ref().to_os_string());
self
}
}
impl CommandBuilder for PgWalDumpBuilder {
fn get_program(&self) -> &'static OsStr {
"pg_waldump".as_ref()
}
fn get_program_dir(&self) -> &Option<PathBuf> {
&self.program_dir
}
fn get_args(&self) -> Vec<OsString> {
let mut args: Vec<OsString> = Vec::new();
if self.version {
args.push("--version".into());
}
if self.help {
args.push("--help".into());
}
if self.backkup_details {
args.push("--bkp-details".into());
}
if let Some(block) = &self.block {
args.push("--block".into());
args.push(block.into());
}
if let Some(end) = &self.end {
args.push("--end".into());
args.push(end.into());
}
if self.follow {
args.push("--follow".into());
}
if let Some(fork) = &self.fork {
args.push("--fork".into());
args.push(fork.into());
}
if let Some(limit) = &self.limit {
args.push("--limit".into());
args.push(limit.into());
}
if let Some(path) = &self.path {
args.push("--path".into());
args.push(path.into());
}
if self.quiet {
args.push("--quiet".into());
}
for rmgr in &self.rmgr {
args.push("--rmgr".into());
args.push(rmgr.into());
}
if let Some(relation) = &self.relation {
args.push("--relation".into());
args.push(relation.into());
}
if let Some(start) = &self.start {
args.push("--start".into());
args.push(start.into());
}
if let Some(timeline) = &self.timeline {
args.push("--timeline".into());
args.push(timeline.into());
}
if self.fullpage {
args.push("--fullpage".into());
}
if let Some(xid) = &self.xid {
args.push("--xid".into());
args.push(xid.into());
}
if let Some(stats) = &self.stats {
let mut option = OsString::from("--stats");
if !stats.is_empty() {
option.push("=");
option.push(stats);
}
args.push(option);
}
if let Some(save_fullpage) = &self.save_fullpage {
args.push("--save-fullpage".into());
args.push(save_fullpage.into());
}
if let Some(start_segment) = &self.start_segment {
args.push(start_segment.into());
}
if let Some(end_segment) = &self.end_segment {
args.push(end_segment.into());
}
args
}
fn get_envs(&self) -> Vec<(OsString, OsString)> {
self.envs.clone()
}
fn env<S: AsRef<OsStr>>(mut self, key: S, value: S) -> Self {
self.envs
.push((key.as_ref().to_os_string(), value.as_ref().to_os_string()));
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::CommandExecutor;
use crate::TestSettings;
use crate::test_utils::{TestResult, assert_args, binary_dir};
use crate::traits::CommandToString;
use test_log::test;
#[test]
fn test_builder_new() {
let command = PgWalDumpBuilder::new().program_dir(".").build();
assert_eq!(
PathBuf::from(".").join("pg_waldump"),
PathBuf::from(command.to_command_string().replace('"', ""))
);
}
#[test]
fn test_builder_from() {
let command = PgWalDumpBuilder::from(&TestSettings).build();
#[cfg(not(target_os = "windows"))]
let command_prefix = r#""./pg_waldump""#;
#[cfg(target_os = "windows")]
let command_prefix = r#"".\\pg_waldump""#;
assert_eq!(format!("{command_prefix}"), command.to_command_string());
}
#[test]
fn test_builder() {
let command = PgWalDumpBuilder::new()
.env("PGDATABASE", "database")
.backup_details()
.block("block")
.end("end")
.follow()
.fork("fork")
.limit("limit")
.path("path")
.quiet()
.rmgr("rmgr")
.relation("relation")
.start("start")
.timeline("timeline")
.version()
.fullpage()
.xid("xid")
.stats("stats")
.save_fullpage("save_fullpage")
.help()
.start_segment("000000010000000000000001")
.end_segment("000000010000000000000002")
.build();
#[cfg(not(target_os = "windows"))]
let command_prefix = r#"PGDATABASE="database" "#;
#[cfg(target_os = "windows")]
let command_prefix = String::new();
assert_eq!(
format!(
r#"{command_prefix}"pg_waldump" "--version" "--help" "--bkp-details" "--block" "block" "--end" "end" "--follow" "--fork" "fork" "--limit" "limit" "--path" "path" "--quiet" "--rmgr" "rmgr" "--relation" "relation" "--start" "start" "--timeline" "timeline" "--fullpage" "--xid" "xid" "--stats=stats" "--save-fullpage" "save_fullpage" "000000010000000000000001" "000000010000000000000002""#
),
command.to_command_string()
);
}
#[test]
fn test_optional_arguments_are_attached_to_the_option() {
assert_args(PgWalDumpBuilder::new().stats(""), &["--stats"]);
assert_args(PgWalDumpBuilder::new().stats("record"), &["--stats=record"]);
}
#[test]
fn test_positional_arguments_follow_options() {
assert_args(
PgWalDumpBuilder::new()
.end_segment("end")
.start_segment("start")
.stats("record"),
&["--stats=record", "start", "end"],
);
}
#[test]
fn test_options() {
assert_args(
PgWalDumpBuilder::new().start_segment("value with spaces"),
&["value with spaces"],
);
assert_args(
PgWalDumpBuilder::new().end_segment("value with spaces"),
&["value with spaces"],
);
}
#[test]
fn test_repeated_options() {
assert_args(
PgWalDumpBuilder::new().rmgr("first").rmgr("second"),
&["--rmgr", "first", "--rmgr", "second"],
);
}
#[test]
fn test_informational_options_come_first_on_configured_builders() {
let version = PgWalDumpBuilder::from(&TestSettings).version().get_args();
assert_eq!(version.first(), Some(&OsString::from("--version")));
let help = PgWalDumpBuilder::from(&TestSettings).help().get_args();
assert_eq!(help.first(), Some(&OsString::from("--help")));
}
#[test]
fn test_cli_help_and_version() -> TestResult {
let Some(bin) = binary_dir() else {
return Ok(());
};
PgWalDumpBuilder::new()
.program_dir(&bin)
.version()
.build()
.execute()?;
PgWalDumpBuilder::new()
.program_dir(&bin)
.help()
.build()
.execute()?;
Ok(())
}
}