Skip to main content

perforce_cli/cmd/
dbschema.rs

1use 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/// `p4 [g-opts] dbschema [tablename[:tableversion]]`
11#[cfg_attr(
12    not(feature = "lt2020_2"),
13    doc = "",
14    doc = "`p4 [g-opts] dbschema [-A] [tablename] ...`"
15)]
16///
17/// Reports information about the database structure in which the server
18/// stores metadata. The command is intended for systems integrators and
19/// returns tagged output.
20///
21#[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    /// Creates a new `p4 dbschema` command.
41    ///
42    /// `bin` is the path to the Perforce command-line executable.
43    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    /// Get the Perforce command path.
53    pub fn get_bin(&self) -> &Path {
54        &self.bin
55    }
56
57    /// Set the Perforce command path.
58    pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
59        self.bin = bin.into();
60        self
61    }
62
63    /// Set the Perforce command path.
64    pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
65        self.set_bin(bin);
66        self
67    }
68
69    /// # Description
70    ///
71    /// g-opts
72    ///
73    #[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    /// # Description
95    ///
96    /// g-opts
97    ///
98    #[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    /// # Description
121    ///
122    /// g-opts
123    ///
124    #[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    /// # Description
147    ///
148    /// -A
149    ///
150    /// Display information about all versions.
151    #[cfg(not(feature = "lt2020_2"))]
152    pub fn get_all_versions(&self) -> bool {
153        self.all_versions
154    }
155
156    /// # Description
157    ///
158    /// -A
159    ///
160    /// Display information about all versions.
161    #[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    /// # Description
168    ///
169    /// -A
170    ///
171    /// Display information about all versions.
172    #[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
191// No table arguments.
192impl ParameterizedSpawn<()> for DBSchema {
193    type Output = Child;
194    type Error = std::io::Error;
195
196    /// Spawns `p4 dbschema` as a child process with piped standard output and
197    /// error streams; use the returned [`Child`] handle to wait for it or
198    /// interact with it.
199    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// 2020.2+: a list of tables.
213#[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    /// Spawns `p4 dbschema` for the given tables as a child process with
223    /// piped standard output and error streams; use the returned [`Child`]
224    /// handle to wait for it or interact with it.
225    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// Up to 2020.1: at most one table.
239#[cfg(feature = "lt2020_2")]
240impl<I: AsRef<OsStr>> ParameterizedSpawn<(I,)> for DBSchema {
241    type Output = Child;
242    type Error = std::io::Error;
243
244    /// Spawns `p4 dbschema` for the given table as a child process with
245    /// piped standard output and error streams; use the returned [`Child`]
246    /// handle to wait for it or interact with it.
247    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    /// Dry-run checks of the assembled `p4 dbschema` command line; no process
274    /// is spawned.
275    #[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}