postgresql_commands/
pg_controldata.rs1use crate::Settings;
2use crate::traits::CommandBuilder;
3use std::ffi::{OsStr, OsString};
4use std::path::PathBuf;
5
6#[derive(Clone, Debug, Default)]
8pub struct PgControlDataBuilder {
9 program_dir: Option<PathBuf>,
10 envs: Vec<(OsString, OsString)>,
11 pgdata: Option<PathBuf>,
12 version: bool,
13 help: bool,
14}
15
16impl PgControlDataBuilder {
17 #[must_use]
19 pub fn new() -> Self {
20 Self::default()
21 }
22
23 pub fn from(settings: &dyn Settings) -> Self {
25 Self::new().program_dir(settings.get_binary_dir())
26 }
27
28 #[must_use]
30 pub fn program_dir<P: Into<PathBuf>>(mut self, path: P) -> Self {
31 self.program_dir = Some(path.into());
32 self
33 }
34
35 #[must_use]
37 pub fn pgdata<P: Into<PathBuf>>(mut self, pgdata: P) -> Self {
38 self.pgdata = Some(pgdata.into());
39 self
40 }
41
42 #[must_use]
44 pub fn version(mut self) -> Self {
45 self.version = true;
46 self
47 }
48
49 #[must_use]
51 pub fn help(mut self) -> Self {
52 self.help = true;
53 self
54 }
55}
56
57impl CommandBuilder for PgControlDataBuilder {
58 fn get_program(&self) -> &'static OsStr {
60 "pg_controldata".as_ref()
61 }
62
63 fn get_program_dir(&self) -> &Option<PathBuf> {
65 &self.program_dir
66 }
67
68 fn get_args(&self) -> Vec<OsString> {
70 let mut args: Vec<OsString> = Vec::new();
71
72 if let Some(pgdata) = &self.pgdata {
73 args.push("--pgdata".into());
74 args.push(pgdata.into());
75 }
76
77 if self.version {
78 args.push("--version".into());
79 }
80
81 if self.help {
82 args.push("--help".into());
83 }
84
85 args
86 }
87
88 fn get_envs(&self) -> Vec<(OsString, OsString)> {
90 self.envs.clone()
91 }
92
93 fn env<S: AsRef<OsStr>>(mut self, key: S, value: S) -> Self {
95 self.envs
96 .push((key.as_ref().to_os_string(), value.as_ref().to_os_string()));
97 self
98 }
99}
100
101#[cfg(test)]
102mod tests {
103 use super::*;
104 use crate::TestSettings;
105 use crate::traits::CommandToString;
106 use test_log::test;
107
108 #[test]
109 fn test_builder_new() {
110 let command = PgControlDataBuilder::new().program_dir(".").build();
111 assert_eq!(
112 PathBuf::from(".").join("pg_controldata"),
113 PathBuf::from(command.to_command_string().replace('"', ""))
114 );
115 }
116
117 #[test]
118 fn test_builder_from() {
119 let command = PgControlDataBuilder::from(&TestSettings).build();
120 #[cfg(not(target_os = "windows"))]
121 let command_prefix = r#""./pg_controldata""#;
122 #[cfg(target_os = "windows")]
123 let command_prefix = r#"".\\pg_controldata""#;
124
125 assert_eq!(format!("{command_prefix}"), command.to_command_string());
126 }
127
128 #[test]
129 fn test_builder() {
130 let command = PgControlDataBuilder::new()
131 .env("PGDATABASE", "database")
132 .pgdata("pgdata")
133 .version()
134 .help()
135 .build();
136 #[cfg(not(target_os = "windows"))]
137 let command_prefix = r#"PGDATABASE="database" "#;
138 #[cfg(target_os = "windows")]
139 let command_prefix = String::new();
140
141 assert_eq!(
142 format!(r#"{command_prefix}"pg_controldata" "--pgdata" "pgdata" "--version" "--help""#),
143 command.to_command_string()
144 );
145 }
146}