perforce_cli/cmd/
dbschema.rs1use std::ffi::OsStr;
2use std::path::{Path, PathBuf};
3use std::process::{Child, Command, Stdio};
4
5use super::SubCommand;
6
7use crate::global::GlobalOpts;
8use crate::spawn::ParameterizedSpawn;
9
10#[cfg_attr(
12 not(feature = "lt2020_2"),
13 doc = "",
14 doc = "`p4 [g-opts] dbschema [-A] [tablename] ...`"
15)]
16#[cfg_attr(
22 feature = "lt2020_2",
23 doc = "Spawn with at most one `tablename`, optionally followed by `:tableversion`; by default all current tables are reported."
24)]
25#[cfg_attr(
26 not(feature = "lt2020_2"),
27 doc = "Spawn with a list of `tablename` entries, each optionally followed by `:tableversion`; by default all current tables are reported. Use [`Self::all_versions`] to supply `-A` and report all versions instead."
28)]
29#[derive(Debug, Clone, Default)]
30pub struct DBSchema {
31 bin: PathBuf,
32
33 global_opts: GlobalOpts,
34
35 #[cfg(not(feature = "lt2020_2"))]
36 all_versions: bool,
37}
38
39impl DBSchema {
40 pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
44 Self {
45 bin: bin.into(),
46 global_opts,
47 #[cfg(not(feature = "lt2020_2"))]
48 all_versions: false,
49 }
50 }
51
52 pub fn get_bin(&self) -> &Path {
54 &self.bin
55 }
56
57 pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
59 self.bin = bin.into();
60 self
61 }
62
63 pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
65 self.set_bin(bin);
66 self
67 }
68
69 #[cfg_attr(
74 feature = "lt2014_2",
75 doc = "See the [Global Options](GlobalOpts) section."
76 )]
77 #[cfg_attr(
78 all(feature = "lt2015_1", not(feature = "lt2014_2")),
79 doc = "See the [“Global Options”](GlobalOpts) section."
80 )]
81 #[cfg_attr(
82 all(feature = "lt2017_1", not(feature = "lt2015_1")),
83 doc = "See [“Global Options”](GlobalOpts)."
84 )]
85 #[cfg_attr(
86 all(feature = "lt2018_2", not(feature = "lt2017_1")),
87 doc = "See [Global Options](GlobalOpts)."
88 )]
89 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
90 pub fn get_global_opts(&self) -> &GlobalOpts {
91 &self.global_opts
92 }
93
94 #[cfg_attr(
99 feature = "lt2014_2",
100 doc = "See the [Global Options](GlobalOpts) section."
101 )]
102 #[cfg_attr(
103 all(feature = "lt2015_1", not(feature = "lt2014_2")),
104 doc = "See the [“Global Options”](GlobalOpts) section."
105 )]
106 #[cfg_attr(
107 all(feature = "lt2017_1", not(feature = "lt2015_1")),
108 doc = "See [“Global Options”](GlobalOpts)."
109 )]
110 #[cfg_attr(
111 all(feature = "lt2018_2", not(feature = "lt2017_1")),
112 doc = "See [Global Options](GlobalOpts)."
113 )]
114 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
115 pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
116 self.global_opts = v;
117 self
118 }
119
120 #[cfg_attr(
125 feature = "lt2014_2",
126 doc = "See the [Global Options](GlobalOpts) section."
127 )]
128 #[cfg_attr(
129 all(feature = "lt2015_1", not(feature = "lt2014_2")),
130 doc = "See the [“Global Options”](GlobalOpts) section."
131 )]
132 #[cfg_attr(
133 all(feature = "lt2017_1", not(feature = "lt2015_1")),
134 doc = "See [“Global Options”](GlobalOpts)."
135 )]
136 #[cfg_attr(
137 all(feature = "lt2018_2", not(feature = "lt2017_1")),
138 doc = "See [Global Options](GlobalOpts)."
139 )]
140 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
141 pub fn global_opts(mut self, v: GlobalOpts) -> Self {
142 self.global_opts = v;
143 self
144 }
145
146 #[cfg(not(feature = "lt2020_2"))]
152 pub fn get_all_versions(&self) -> bool {
153 self.all_versions
154 }
155
156 #[cfg(not(feature = "lt2020_2"))]
162 pub fn set_all_versions(&mut self, v: bool) -> &mut Self {
163 self.all_versions = v;
164 self
165 }
166
167 #[cfg(not(feature = "lt2020_2"))]
173 pub fn all_versions(mut self, v: bool) -> Self {
174 self.all_versions = v;
175 self
176 }
177}
178
179impl SubCommand for DBSchema {
180 fn name(&self) -> &str {
181 "dbschema"
182 }
183
184 fn inject_local_args(&self, _: &mut Command) {}
185
186 fn global_opts(&self) -> Option<&GlobalOpts> {
187 Some(&self.global_opts)
188 }
189}
190
191impl ParameterizedSpawn<()> for DBSchema {
193 type Output = Child;
194 type Error = std::io::Error;
195
196 fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
200 let mut command = self.setup_command(&self.bin);
201 #[cfg(not(feature = "lt2020_2"))]
202 if self.all_versions {
203 command.arg("-A");
204 }
205 command
206 .stdout(Stdio::piped())
207 .stderr(Stdio::piped())
208 .spawn()
209 }
210}
211
212#[cfg(not(feature = "lt2020_2"))]
214impl<S, I> ParameterizedSpawn<(S,)> for DBSchema
215where
216 S: IntoIterator<Item = I>,
217 I: AsRef<OsStr>,
218{
219 type Output = Child;
220 type Error = std::io::Error;
221
222 fn spawn_with(&mut self, (tables,): (S,)) -> Result<Self::Output, Self::Error> {
226 let mut command = self.setup_command(&self.bin);
227 if self.all_versions {
228 command.arg("-A");
229 }
230 command
231 .args(tables)
232 .stdout(Stdio::piped())
233 .stderr(Stdio::piped())
234 .spawn()
235 }
236}
237
238#[cfg(feature = "lt2020_2")]
240impl<I: AsRef<OsStr>> ParameterizedSpawn<(I,)> for DBSchema {
241 type Output = Child;
242 type Error = std::io::Error;
243
244 fn spawn_with(&mut self, (table,): (I,)) -> Result<Self::Output, Self::Error> {
248 self.setup_command(&self.bin)
249 .arg(table)
250 .stdout(Stdio::piped())
251 .stderr(Stdio::piped())
252 .spawn()
253 }
254}
255
256#[cfg(test)]
257mod tests {
258 use super::*;
259 use crate::cmd::args_of;
260
261 #[test]
262 fn bin_accessors_update_executable_path() {
263 let mut command = DBSchema::new("p4", GlobalOpts::new());
264 assert_eq!(command.get_bin(), Path::new("p4"));
265
266 command.set_bin("/usr/local/bin/p4");
267 assert_eq!(command.get_bin(), Path::new("/usr/local/bin/p4"));
268
269 let command = command.bin("p4.exe");
270 assert_eq!(command.get_bin(), Path::new("p4.exe"));
271 }
272
273 #[test]
276 fn without_arguments() {
277 let dbschema = DBSchema::new("p4", GlobalOpts::new());
278
279 assert_eq!(args_of(&dbschema.setup_command("p4")), ["dbschema"]);
280 }
281
282 #[cfg(not(feature = "lt2020_2"))]
283 #[test]
284 fn all_versions_accessors() {
285 let mut dbschema = DBSchema::new("p4", GlobalOpts::new());
286 assert!(!dbschema.get_all_versions());
287
288 dbschema.set_all_versions(true);
289 assert!(dbschema.get_all_versions());
290
291 let dbschema = dbschema.all_versions(false);
292 assert!(!dbschema.get_all_versions());
293 }
294
295 #[test]
296 fn global_opts_are_injected_once() {
297 let dbschema = DBSchema::new(
298 "p4",
299 GlobalOpts::new().port("localhost:1666").quiet_mode(true),
300 );
301
302 assert_eq!(
303 args_of(&dbschema.setup_command("p4")),
304 ["-p", "localhost:1666", "-q", "dbschema"]
305 );
306 }
307}