Skip to main content

perforce_cli/cmd/
depots.rs

1use std::{
2    path::{Path, PathBuf},
3    process::{Child, Command, Stdio},
4};
5
6use super::{ExclusiveOption, SubCommand, Unselected};
7
8#[cfg(not(feature = "lt2017_1"))]
9use super::depot::DepotType;
10
11use crate::global::GlobalOpts;
12use crate::spawn::ParameterizedSpawn;
13
14/// The `-e` / `-E` name filter: list only depots whose name matches the
15/// filter pattern.
16///
17/// Created by [`Depots::filter`]; the `case_insensitive` parameter selects
18/// [`Filter::CaseSensitiveMode`] (`-e`) or [`Filter::InCaseSensitiveMode`]
19/// (`-E`).
20#[cfg(not(feature = "lt2017_2"))]
21#[derive(Debug, Clone, PartialEq, Eq)]
22enum Filter {
23    /// `-e`: match the pattern using the server's normal case-sensitivity
24    /// rules.
25    CaseSensitiveMode(String),
26
27    /// `-E`: match the pattern case-insensitively, even on a case-sensitive
28    /// server.
29    InCaseSensitiveMode(String),
30}
31
32#[cfg(not(feature = "lt2017_2"))]
33impl ExclusiveOption for Filter {
34    fn inject_args(&self, command: &mut Command) {
35        match self {
36            Filter::CaseSensitiveMode(pattern) => {
37                command.arg("-e").arg(pattern);
38            }
39            Filter::InCaseSensitiveMode(pattern) => {
40                command.arg("-E").arg(pattern);
41            }
42        }
43    }
44}
45
46/// List-all mode of `p4 depots` (`-a`): lists depots of `all` types,
47/// included hidden depots.
48///
49/// Entered with [`Depots::list_all`].
50#[cfg(not(feature = "lt2024_1"))]
51#[derive(Debug, Clone, Copy, Default)]
52pub struct ListAll {}
53
54#[cfg(not(feature = "lt2024_1"))]
55impl ExclusiveOption for ListAll {
56    fn inject_args(&self, command: &mut Command) {
57        command.arg("-a");
58    }
59}
60
61/// List-specified mode of `p4 depots` (`[-t type]`): list only the depots
62/// of the specified type.
63///
64/// Entered with [`Depots::depot_type`].
65#[cfg(not(feature = "lt2017_1"))]
66#[derive(Debug, Clone, Copy, Default)]
67pub struct ListSpecified {
68    depot_type: Option<DepotType>,
69}
70
71#[cfg(not(feature = "lt2017_1"))]
72impl ExclusiveOption for ListSpecified {
73    fn inject_args(&self, command: &mut Command) {
74        if let Some(depot_type) = &self.depot_type {
75            #[cfg(feature = "lt2017_2")]
76            command.arg(format!("--depot-type={}", depot_type.as_str()));
77            #[cfg(not(feature = "lt2017_2"))]
78            command.arg("-t").arg(depot_type.as_str());
79        }
80    }
81}
82
83#[cfg_attr(feature = "lt2017_1", doc = "`p4 [g-opts] depots`")]
84#[cfg_attr(
85    all(feature = "lt2017_2", not(feature = "lt2017_1")),
86    doc = "`p4 [g-opts] depots [--depot-type=type]`"
87)]
88#[cfg_attr(
89    all(feature = "lt2024_1", not(feature = "lt2017_2")),
90    doc = "`p4 [g-opts] depots [[-e | -E] nameFilter] [-t type]`"
91)]
92#[cfg_attr(
93    not(feature = "lt2024_1"),
94    doc = "`p4 [g-opts] depots [[-e | -E] nameFilter] [-t type] [-a]`"
95)]
96///
97#[cfg_attr(
98    feature = "lt2017_2",
99    doc = "Display a list of depots known to the Perforce versioning service."
100)]
101#[cfg_attr(
102    all(feature = "lt2019_1", not(feature = "lt2017_2")),
103    doc = "Display a list of depots known to the Helix Server."
104)]
105#[cfg_attr(
106    all(feature = "lt2021_1", not(feature = "lt2019_1")),
107    doc = "Display a list of depots known to the Helix server."
108)]
109#[cfg_attr(
110    all(feature = "lt2024_1", not(feature = "lt2021_1")),
111    doc = "Display a list of depots known to the Helix Server."
112)]
113#[cfg_attr(
114    all(feature = "lt2024_2", not(feature = "lt2024_1")),
115    doc = "Display a list of depots known to the Helix Core Server."
116)]
117#[cfg_attr(
118    not(feature = "lt2024_2"),
119    doc = "Display a list of depots known to the P4 Server."
120)]
121///
122#[cfg_attr(
123    feature = "lt2017_1",
124    doc = "Lists all the remote and local depots known to the Perforce service, in the form:",
125    doc = "",
126    doc = "`Depot name date type address map description`",
127    doc = "",
128    doc = "where name, date, type, address, map, and description are as defined in the [`p4 depot`](crate::cmd::Depot) form.",
129    doc = "",
130    doc = "If a depot is excluded in the protections table for a given user, the user denied access will no longer see the depot in the output of `p4 depots`.",
131    doc = "",
132    doc = "`Operator` users may now run the `p4 depots` command."
133)]
134#[cfg_attr(not(feature = "lt2017_1"), doc = "Display a list of depots.")]
135#[cfg_attr(
136    all(feature = "lt2022_1", not(feature = "lt2020_2")),
137    doc = "",
138    doc = "The list excludes the default graph depot named `repo` unless you use the command, `p4 depot -t graph`."
139)]
140#[cfg_attr(
141    not(feature = "lt2022_1"),
142    doc = "",
143    doc = "The list excludes",
144    doc = "",
145    doc = "- the default graph depot named `repo` unless you use the `p4 depots -t graph` command",
146    doc = "- the extension depot unless a user with the `super` protection uses the `p4 depots -t extension` command"
147)]
148#[cfg_attr(
149    not(feature = "lt2017_1"),
150    doc = "",
151    doc = "If a depot is excluded in the protections table for a given user, that user does not see the depot in the output of this command.",
152    doc = "",
153    doc = "`Operator` users can run this command. (To learn about `operator` users, see the `p4 user` usage notes.)"
154)]
155///
156/// The `M` type parameter tracks the command form at compile time. The
157/// default [`Unselected`] state is the bare listing form with no local
158/// options
159#[cfg_attr(feature = "lt2017_1", doc = ".")]
160#[cfg_attr(
161    all(feature = "lt2024_1", not(feature = "lt2017_1")),
162    doc = "; [`Self::depot_type`] transitions to the [`ListSpecified`] mode."
163)]
164#[cfg_attr(
165    not(feature = "lt2024_1"),
166    doc = "; [`Self::depot_type`] and [`Self::list_all`] transition to the [`ListSpecified`] and [`ListAll`] modes."
167)]
168#[derive(Debug, Clone, Default)]
169pub struct Depots<M = Unselected> {
170    bin: PathBuf,
171
172    global_opts: GlobalOpts,
173
174    #[cfg(not(feature = "lt2017_2"))]
175    filter: Option<Filter>,
176
177    mode: M,
178}
179
180impl Depots<Unselected> {
181    /// Creates a new `p4 depots` command.
182    ///
183    /// `bin` is the path to the Perforce command-line executable.
184    pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
185        Self {
186            bin: bin.into(),
187            global_opts,
188            #[cfg(not(feature = "lt2017_2"))]
189            filter: None,
190            mode: Unselected,
191        }
192    }
193
194    /// # Description
195    ///
196    #[cfg_attr(feature = "lt2017_2", doc = "--depot-type=type")]
197    #[cfg_attr(not(feature = "lt2017_2"), doc = "-t type")]
198    ///
199    #[cfg_attr(
200        feature = "lt2024_1",
201        doc = "List only the depots of the specified *type*"
202    )]
203    #[cfg_attr(
204        not(feature = "lt2024_1"),
205        doc = "List only the depots of the specified *type*."
206    )]
207    ///
208    /// Transitions this command to the [`ListSpecified`] mode.
209    #[cfg(not(feature = "lt2017_1"))]
210    pub fn depot_type(self, v: DepotType) -> Depots<ListSpecified> {
211        Depots {
212            bin: self.bin,
213            global_opts: self.global_opts,
214            #[cfg(not(feature = "lt2017_2"))]
215            filter: self.filter,
216            mode: ListSpecified {
217                depot_type: Some(v),
218            },
219        }
220    }
221
222    /// # Description
223    ///
224    /// -a
225    ///
226    /// Lists depots of `all` types, included hidden depots. This is
227    /// especially useful when the user needs to check map paths for all
228    /// depots.
229    ///
230    /// Transitions this command to the [`ListAll`] mode.
231    #[cfg(not(feature = "lt2024_1"))]
232    pub fn list_all(self) -> Depots<ListAll> {
233        Depots {
234            bin: self.bin,
235            global_opts: self.global_opts,
236            filter: self.filter,
237            mode: ListAll {},
238        }
239    }
240}
241
242#[cfg(not(feature = "lt2017_1"))]
243impl Depots<ListSpecified> {
244    /// # Description
245    ///
246    #[cfg_attr(feature = "lt2017_2", doc = "--depot-type=type")]
247    #[cfg_attr(not(feature = "lt2017_2"), doc = "-t type")]
248    ///
249    #[cfg_attr(
250        feature = "lt2024_1",
251        doc = "List only the depots of the specified *type*"
252    )]
253    #[cfg_attr(
254        not(feature = "lt2024_1"),
255        doc = "List only the depots of the specified *type*."
256    )]
257    pub fn get_depot_type(&self) -> Option<DepotType> {
258        self.mode.depot_type
259    }
260
261    /// # Description
262    ///
263    #[cfg_attr(feature = "lt2017_2", doc = "--depot-type=type")]
264    #[cfg_attr(not(feature = "lt2017_2"), doc = "-t type")]
265    ///
266    #[cfg_attr(
267        feature = "lt2024_1",
268        doc = "List only the depots of the specified *type*"
269    )]
270    #[cfg_attr(
271        not(feature = "lt2024_1"),
272        doc = "List only the depots of the specified *type*."
273    )]
274    pub fn set_depot_type(&mut self, v: DepotType) -> &mut Self {
275        self.mode.depot_type = Some(v);
276        self
277    }
278
279    /// # Description
280    ///
281    #[cfg_attr(feature = "lt2017_2", doc = "--depot-type=type")]
282    #[cfg_attr(not(feature = "lt2017_2"), doc = "-t type")]
283    ///
284    #[cfg_attr(
285        feature = "lt2024_1",
286        doc = "List only the depots of the specified *type*"
287    )]
288    #[cfg_attr(
289        not(feature = "lt2024_1"),
290        doc = "List only the depots of the specified *type*."
291    )]
292    pub fn depot_type(mut self, v: DepotType) -> Self {
293        self.mode.depot_type = Some(v);
294        self
295    }
296}
297
298impl<M: ExclusiveOption> Depots<M> {
299    /// Get the Perforce command path.
300    pub fn get_bin(&self) -> &Path {
301        &self.bin
302    }
303
304    /// Set the Perforce command path.
305    pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
306        self.bin = bin.into();
307        self
308    }
309
310    /// Set the Perforce command path.
311    pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
312        self.set_bin(bin);
313        self
314    }
315
316    /// # Description
317    ///
318    /// -e nameFilter
319    ///
320    #[cfg_attr(
321        feature = "lt2024_2",
322        doc = "Lists depot specs with a name that matches the *nameFilter* pattern. `p4 depots -e h*x*` finds depots with names like `helix` and `hxadm`. This option follows the case-sensitivity of the server."
323    )]
324    #[cfg_attr(
325        not(feature = "lt2024_2"),
326        doc = "Lists depot specs with a name that matches the *nameFilter* pattern. `p4 depots -e \"h*x*\"` finds depots with names like `helix` and `hxadm`. If you use the `*` wildcard, enclose the search pattern in double quotes (\"). This option follows the case-sensitivity of the server."
327    )]
328    ///
329    /// -E nameFilter
330    ///
331    /// Makes the matching case-insensitive, even on a case-sensitive server.
332    ///
333    /// The `case_insensitive` parameter selects `-E` over `-e`.
334    #[cfg(not(feature = "lt2017_2"))]
335    pub fn set_filter(&mut self, v: impl Into<String>, case_insensitive: bool) -> &mut Self {
336        self.filter = Some(if case_insensitive {
337            Filter::InCaseSensitiveMode(v.into())
338        } else {
339            Filter::CaseSensitiveMode(v.into())
340        });
341        self
342    }
343
344    /// # Description
345    ///
346    /// -e nameFilter
347    ///
348    #[cfg_attr(
349        feature = "lt2024_2",
350        doc = "Lists depot specs with a name that matches the *nameFilter* pattern. `p4 depots -e h*x*` finds depots with names like `helix` and `hxadm`. This option follows the case-sensitivity of the server."
351    )]
352    #[cfg_attr(
353        not(feature = "lt2024_2"),
354        doc = "Lists depot specs with a name that matches the *nameFilter* pattern. `p4 depots -e \"h*x*\"` finds depots with names like `helix` and `hxadm`. If you use the `*` wildcard, enclose the search pattern in double quotes (\"). This option follows the case-sensitivity of the server."
355    )]
356    ///
357    /// -E nameFilter
358    ///
359    /// Makes the matching case-insensitive, even on a case-sensitive server.
360    ///
361    /// The `case_insensitive` parameter selects `-E` over `-e`.
362    #[cfg(not(feature = "lt2017_2"))]
363    pub fn filter(mut self, v: impl Into<String>, case_insensitive: bool) -> Self {
364        self.set_filter(v, case_insensitive);
365        self
366    }
367
368    /// # Description
369    ///
370    /// g-opts
371    ///
372    #[cfg_attr(
373        feature = "lt2014_2",
374        doc = "See the [Global Options](GlobalOpts) section."
375    )]
376    #[cfg_attr(
377        all(feature = "lt2015_1", not(feature = "lt2014_2")),
378        doc = "See the [“Global Options”](GlobalOpts) section."
379    )]
380    #[cfg_attr(
381        all(feature = "lt2017_1", not(feature = "lt2015_1")),
382        doc = "See [“Global Options”](GlobalOpts)."
383    )]
384    #[cfg_attr(
385        all(feature = "lt2018_2", not(feature = "lt2017_1")),
386        doc = "See [Global Options](GlobalOpts)."
387    )]
388    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
389    pub fn get_global_opts(&self) -> &GlobalOpts {
390        &self.global_opts
391    }
392
393    /// # Description
394    ///
395    /// g-opts
396    ///
397    #[cfg_attr(
398        feature = "lt2014_2",
399        doc = "See the [Global Options](GlobalOpts) section."
400    )]
401    #[cfg_attr(
402        all(feature = "lt2015_1", not(feature = "lt2014_2")),
403        doc = "See the [“Global Options”](GlobalOpts) section."
404    )]
405    #[cfg_attr(
406        all(feature = "lt2017_1", not(feature = "lt2015_1")),
407        doc = "See [“Global Options”](GlobalOpts)."
408    )]
409    #[cfg_attr(
410        all(feature = "lt2018_2", not(feature = "lt2017_1")),
411        doc = "See [Global Options](GlobalOpts)."
412    )]
413    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
414    pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
415        self.global_opts = v;
416        self
417    }
418
419    /// # Description
420    ///
421    /// g-opts
422    ///
423    #[cfg_attr(
424        feature = "lt2014_2",
425        doc = "See the [Global Options](GlobalOpts) section."
426    )]
427    #[cfg_attr(
428        all(feature = "lt2015_1", not(feature = "lt2014_2")),
429        doc = "See the [“Global Options”](GlobalOpts) section."
430    )]
431    #[cfg_attr(
432        all(feature = "lt2017_1", not(feature = "lt2015_1")),
433        doc = "See [“Global Options”](GlobalOpts)."
434    )]
435    #[cfg_attr(
436        all(feature = "lt2018_2", not(feature = "lt2017_1")),
437        doc = "See [Global Options](GlobalOpts)."
438    )]
439    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
440    pub fn global_opts(mut self, v: GlobalOpts) -> Self {
441        self.global_opts = v;
442        self
443    }
444}
445
446impl<M: ExclusiveOption> SubCommand for Depots<M> {
447    fn name(&self) -> &str {
448        "depots"
449    }
450
451    fn inject_local_args(&self, command: &mut Command) {
452        #[cfg(not(feature = "lt2017_2"))]
453        if let Some(filter) = &self.filter {
454            filter.inject_args(command);
455        }
456        self.mode.inject_args(command);
457    }
458
459    fn global_opts(&self) -> Option<&GlobalOpts> {
460        Some(&self.global_opts)
461    }
462}
463
464impl<M: ExclusiveOption> ParameterizedSpawn<()> for Depots<M> {
465    type Output = Child;
466    type Error = std::io::Error;
467
468    /// Spawns `p4 depots` as a child process with piped standard output and
469    /// error streams; use the returned [`Child`] handle to wait for it or
470    /// interact with it.
471    fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
472        self.setup_command(&self.bin)
473            .stdout(Stdio::piped())
474            .stderr(Stdio::piped())
475            .spawn()
476    }
477}