perforce-cli 0.2.3

A type-safe builder library for spawning Perforce (p4) commands, with compile-time option state isolation and multi-version support.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};

use super::{ExclusiveOption, SubCommand, Unselected};

use crate::global::GlobalOpts;
use crate::spawn::ParameterizedSpawn;

// ---------------------------------------------------------------------------
// Predefined targets
// ---------------------------------------------------------------------------

/// The `P4ROOT` target: report disk space for the filesystem holding
/// [`P4ROOT`](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/P4ROOT.html).
///
/// Entered with [`DiskSpace::report_p4root`].
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct P4Root;

/// The `P4JOURNAL` target: report disk space for the filesystem holding
/// [`P4JOURNAL`](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/P4JOURNAL.html).
///
/// Entered with [`DiskSpace::report_p4journal`].
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct P4Journal;

/// The `P4LOG` target: report disk space for the filesystem holding
/// [`P4LOG`](https://help.perforce.com/helix-core/server-apps/cmdref/current/Content/CmdRef/P4LOG.html)
/// (the server log).
///
/// Entered with [`DiskSpace::report_p4log`].
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct P4Log;

/// The `TEMP` target: report disk space for the filesystem holding temporary
/// files. If `TEMP` is not defined, `P4ROOT` is used on Windows and `/tmp` on
/// Unix.
///
/// Entered with [`DiskSpace::report_temp`].
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Temp;

// ---------------------------------------------------------------------------
// Argument injection
// ---------------------------------------------------------------------------

impl ExclusiveOption for P4Root {
    fn inject_args(&self, command: &mut Command) {
        command.arg("P4ROOT");
    }
}

impl ExclusiveOption for P4Journal {
    fn inject_args(&self, command: &mut Command) {
        command.arg("P4JOURNAL");
    }
}

impl ExclusiveOption for P4Log {
    fn inject_args(&self, command: &mut Command) {
        command.arg("P4LOG");
    }
}

impl ExclusiveOption for Temp {
    fn inject_args(&self, command: &mut Command) {
        command.arg("TEMP");
    }
}

// ---------------------------------------------------------------------------
// Command struct
// ---------------------------------------------------------------------------

/// Type-safe builder for `p4 diskspace`.
///
/// `p4 df` is an alias for `p4 diskspace`.
///
/// # Syntax
///
/// ```text
/// p4 [g-opts] diskspace [P4ROOT | P4JOURNAL | P4LOG | TEMP | journalPrefix | depot]
/// ```
///
/// Shows summary information about the current availability of disk space on
/// the server.
///
/// The output of `p4 diskspace` is in the form:
///
/// ```text
/// name (type type) : xxx GB free, yyy GB used, zzz GB total (ff % full)
/// ```
///
#[cfg_attr(
    feature = "lt2017_2",
    doc = "Where `name` can be either `P4ROOT`, `P4JOURNAL`, `P4LOG`, `TEMP`, a prefix to a non-default Perforce journal file location, or the name of a Perforce depot. The filesystem `type` is that reported by the operating system."
)]
#[cfg_attr(
    all(feature = "lt2019_1", not(feature = "lt2017_2")),
    doc = "Where `name` can be either `P4ROOT`, `P4JOURNAL`, `P4LOG`, `TEMP`, a prefix to a non-default Helix Server journal file location, or the name of a Helix Server depot. The filesystem `type` is that reported by the operating system."
)]
#[cfg_attr(
    all(feature = "lt2021_1", not(feature = "lt2019_1")),
    doc = "Where `name` can be either `P4ROOT`, `P4JOURNAL`, `P4LOG`, `TEMP`, a prefix to a non-default Helix server journal file location, or the name of a Helix server depot. The filesystem `type` is that reported by the operating system."
)]
#[cfg_attr(
    all(feature = "lt2024_1", not(feature = "lt2021_1")),
    doc = "Where `name` can be either `P4ROOT`, `P4JOURNAL`, `P4LOG`, `TEMP`, a prefix to a non-default Helix Server journal file location, or the name of a Helix Server depot. The filesystem `type` is that reported by the operating system."
)]
#[cfg_attr(
    all(feature = "lt2024_2", not(feature = "lt2024_1")),
    doc = "Where `name` can be either `P4ROOT`, `P4JOURNAL`, `P4LOG`, `TEMP`, a prefix to a non-default Helix Core Server journal file location, or the name of a Helix Core Server depot. The filesystem `type` is that reported by the operating system."
)]
#[cfg_attr(
    not(feature = "lt2024_2"),
    doc = "Where `name` can be either `P4ROOT`, `P4JOURNAL`, `P4LOG`, `TEMP`, a prefix to a non-default P4 Server journal file location, or the name of a P4 Server depot. The filesystem `type` is that reported by the operating system."
)]
///
/// If no arguments are specified, disk space information is displayed for all
/// objects.
///
#[cfg_attr(
    all(not(feature = "lt2016_1"), feature = "lt2017_2"),
    doc = "By default, Perforce rejects commands when free space on the filesystems housing the `P4ROOT`, `P4JOURNAL`, `P4LOG`, or `TEMP` falls below 10 megabytes. To change this behavior, set the `filesys.P4ROOT.min` (and corresponding) configurables to your desired limits."
)]
#[cfg_attr(
    all(
        not(feature = "lt2016_1"),
        feature = "lt2019_1",
        not(feature = "lt2017_2")
    ),
    doc = "By default, Helix Server rejects commands when free space on the filesystems housing the `P4ROOT`, `P4JOURNAL`, `P4LOG`, or `TEMP` falls below 10 megabytes. To change this behavior, set the `filesys.P4ROOT.min` (and corresponding) configurables to your desired limits."
)]
#[cfg_attr(
    all(
        not(feature = "lt2016_1"),
        feature = "lt2021_1",
        not(feature = "lt2019_1")
    ),
    doc = "By default, Helix server rejects commands when free space on the filesystems housing the `P4ROOT`, `P4JOURNAL`, `P4LOG`, or `TEMP` falls below 10 megabytes. To change this behavior, set the `filesys.P4ROOT.min` (and corresponding) configurables to your desired limits."
)]
#[cfg_attr(
    all(
        not(feature = "lt2016_1"),
        feature = "lt2024_1",
        not(feature = "lt2021_1")
    ),
    doc = "By default, Helix Server rejects commands when free space on the filesystems housing the `P4ROOT`, `P4JOURNAL`, `P4LOG`, or `TEMP` falls below 10 megabytes. To change this behavior, set the `filesys.P4ROOT.min` (and corresponding) configurables to your desired limits."
)]
#[cfg_attr(
    all(
        not(feature = "lt2016_1"),
        feature = "lt2024_2",
        not(feature = "lt2024_1")
    ),
    doc = "By default, Helix Core Server rejects commands when free space on the filesystems housing the `P4ROOT`, `P4JOURNAL`, `P4LOG`, or `TEMP` falls below 10 megabytes. To change this behavior, set the `filesys.P4ROOT.min` (and corresponding) configurables to your desired limits."
)]
#[cfg_attr(
    all(not(feature = "lt2016_1"), not(feature = "lt2024_2")),
    doc = "By default, P4 Server rejects commands when free space on the filesystems housing the `P4ROOT`, `P4JOURNAL`, `P4LOG`, or `TEMP` falls below 10 megabytes. To change this behavior, set the `filesys.P4ROOT.min` (and corresponding) configurables to your desired limits."
)]
#[cfg_attr(not(feature = "lt2016_1"), doc = "")]
#[cfg_attr(
    all(not(feature = "lt2016_1"), feature = "lt2017_2"),
    doc = "If the user account that runs the Perforce versioning service is subject to disk quotas, the `filesys.*.min` configurables reflect those quotas, regardless of how much physical space actually remains on the filesystem(s) in question."
)]
#[cfg_attr(
    all(
        not(feature = "lt2016_1"),
        feature = "lt2019_1",
        not(feature = "lt2017_2")
    ),
    doc = "If the user account that runs the Helix Server is subject to disk quotas, the `filesys.*.min` configurables reflect those quotas, regardless of how much physical space actually remains on the filesystem(s) in question."
)]
#[cfg_attr(
    all(
        not(feature = "lt2016_1"),
        feature = "lt2021_1",
        not(feature = "lt2019_1")
    ),
    doc = "If the user account that runs the Helix server is subject to disk quotas, the `filesys.*.min` configurables reflect those quotas, regardless of how much physical space actually remains on the filesystem(s) in question."
)]
#[cfg_attr(
    all(
        not(feature = "lt2016_1"),
        feature = "lt2024_1",
        not(feature = "lt2021_1")
    ),
    doc = "If the user account that runs the Helix Server is subject to disk quotas, the `filesys.*.min` configurables reflect those quotas, regardless of how much physical space actually remains on the filesystem(s) in question."
)]
#[cfg_attr(
    all(
        not(feature = "lt2016_1"),
        feature = "lt2024_2",
        not(feature = "lt2024_1")
    ),
    doc = "If the user account that runs the Helix Core Server is subject to disk quotas, the `filesys.*.min` configurables reflect those quotas, regardless of how much physical space actually remains on the filesystem(s) in question."
)]
#[cfg_attr(
    all(not(feature = "lt2016_1"), not(feature = "lt2024_2")),
    doc = "If the user account that runs the P4 Server is subject to disk quotas, the `filesys.*.min` configurables reflect those quotas, regardless of how much physical space actually remains on the filesystem(s) in question."
)]
#[cfg_attr(not(feature = "lt2016_1"), doc = "", doc = "# Note", doc = "")]
#[cfg_attr(
    not(feature = "lt2016_1"),
    doc = "Server releases prior to 16.1 included reserved space in the number displayed for used space in the Size column for Unix filesystems. This has been changed so that the size given now excludes reserved space."
)]
///
/// The `M` type parameter tracks the target at compile time. [`Unselected`]
/// (the state returned by [`DiskSpace::new`]) spawns the bare command, which
/// reports disk space for every object, and can also be spawned with an
/// arbitrary target argument through
/// [`ParameterizedSpawn`] with a one-element tuple, or
/// [`SpawnExt1`](crate::spawn::SpawnExt1); that covers a `journalPrefix` or a
/// depot name. Use [`DiskSpace::report_p4root`], [`DiskSpace::report_p4journal`],
/// [`DiskSpace::report_p4log`], or [`DiskSpace::report_temp`] to select one of
/// the predefined targets; those states spawn without arguments.
#[derive(Debug, Clone, Default)]
pub struct DiskSpace<M = Unselected> {
    bin: PathBuf,

    global_opts: GlobalOpts,

    mode: M,
}

// ---------------------------------------------------------------------------
// Constructors and target transitions
// ---------------------------------------------------------------------------

impl DiskSpace<Unselected> {
    /// Creates a new `p4 diskspace` command.
    ///
    /// `bin` is the path to the Perforce command-line executable. The
    /// resulting command has no target argument and reports disk space for
    /// every object.
    pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
        Self {
            bin: bin.into(),
            global_opts,
            mode: Unselected,
        }
    }
}

impl<M> DiskSpace<M> {
    fn with_mode<N>(self, mode: N) -> DiskSpace<N> {
        DiskSpace {
            bin: self.bin,
            global_opts: self.global_opts,
            mode,
        }
    }

    /// # Description
    ///
    /// `P4ROOT`
    ///
    /// Report disk space available for filesystem holding `P4ROOT`.
    ///
    /// Transitions to the [`P4Root`] state, which spawns without arguments.
    pub fn report_p4root(self) -> DiskSpace<P4Root> {
        self.with_mode(P4Root)
    }

    /// # Description
    ///
    /// `P4JOURNAL`
    ///
    /// Report disk space available for filesystem holding `P4JOURNAL`.
    ///
    /// Transitions to the [`P4Journal`] state, which spawns without
    /// arguments.
    pub fn report_p4journal(self) -> DiskSpace<P4Journal> {
        self.with_mode(P4Journal)
    }

    /// # Description
    ///
    /// `P4LOG`
    ///
    /// Report disk space available for filesystem holding `P4LOG` (server
    /// log).
    ///
    /// Transitions to the [`P4Log`] state, which spawns without arguments.
    pub fn report_p4log(self) -> DiskSpace<P4Log> {
        self.with_mode(P4Log)
    }

    /// # Description
    ///
    /// `TEMP`
    ///
    /// Report disk space available for filesystem holding temporary files. If
    /// not defined, uses `P4ROOT` on Windows, and `/tmp` on Unix.
    ///
    /// Transitions to the [`Temp`] state, which spawns without arguments.
    pub fn report_temp(self) -> DiskSpace<Temp> {
        self.with_mode(Temp)
    }
}

// ---------------------------------------------------------------------------
// Accessors
// ---------------------------------------------------------------------------

impl<M: ExclusiveOption> DiskSpace<M> {
    /// Get the Perforce command path.
    pub fn get_bin(&self) -> &Path {
        &self.bin
    }

    /// Set the Perforce command path.
    pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
        self.bin = bin.into();
        self
    }

    /// Set the Perforce command path.
    pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
        self.set_bin(bin);
        self
    }

    /// # Description
    ///
    /// g-opts
    ///
    #[cfg_attr(
        feature = "lt2014_2",
        doc = "See the [Global Options](GlobalOpts) section."
    )]
    #[cfg_attr(
        all(feature = "lt2015_1", not(feature = "lt2014_2")),
        doc = "See the [“Global Options”](GlobalOpts) section."
    )]
    #[cfg_attr(
        all(feature = "lt2017_1", not(feature = "lt2015_1")),
        doc = "See [“Global Options”](GlobalOpts)."
    )]
    #[cfg_attr(
        all(feature = "lt2018_2", not(feature = "lt2017_1")),
        doc = "See [Global Options](GlobalOpts)."
    )]
    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
    pub fn get_global_opts(&self) -> &GlobalOpts {
        &self.global_opts
    }

    /// # Description
    ///
    /// g-opts
    ///
    #[cfg_attr(
        feature = "lt2014_2",
        doc = "See the [Global Options](GlobalOpts) section."
    )]
    #[cfg_attr(
        all(feature = "lt2015_1", not(feature = "lt2014_2")),
        doc = "See the [“Global Options”](GlobalOpts) section."
    )]
    #[cfg_attr(
        all(feature = "lt2017_1", not(feature = "lt2015_1")),
        doc = "See [“Global Options”](GlobalOpts)."
    )]
    #[cfg_attr(
        all(feature = "lt2018_2", not(feature = "lt2017_1")),
        doc = "See [Global Options](GlobalOpts)."
    )]
    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
    pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
        self.global_opts = v;
        self
    }

    /// # Description
    ///
    /// g-opts
    ///
    #[cfg_attr(
        feature = "lt2014_2",
        doc = "See the [Global Options](GlobalOpts) section."
    )]
    #[cfg_attr(
        all(feature = "lt2015_1", not(feature = "lt2014_2")),
        doc = "See the [“Global Options”](GlobalOpts) section."
    )]
    #[cfg_attr(
        all(feature = "lt2017_1", not(feature = "lt2015_1")),
        doc = "See [“Global Options”](GlobalOpts)."
    )]
    #[cfg_attr(
        all(feature = "lt2018_2", not(feature = "lt2017_1")),
        doc = "See [Global Options](GlobalOpts)."
    )]
    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
    pub fn global_opts(mut self, v: GlobalOpts) -> Self {
        self.global_opts = v;
        self
    }
}

// ---------------------------------------------------------------------------
// SubCommand and spawning
// ---------------------------------------------------------------------------

impl<M: ExclusiveOption> SubCommand for DiskSpace<M> {
    fn name(&self) -> &str {
        "diskspace"
    }

    fn inject_local_args(&self, command: &mut Command) {
        self.mode.inject_args(command);
    }

    fn global_opts(&self) -> Option<&GlobalOpts> {
        Some(&self.global_opts)
    }
}

impl<M: ExclusiveOption> ParameterizedSpawn<()> for DiskSpace<M> {
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 diskspace` as a child process with piped standard output
    /// and error streams; use the returned [`Child`] handle to wait for it or
    /// interact with it.
    fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
        self.setup_command(&self.bin)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

impl<S: AsRef<OsStr>> ParameterizedSpawn<(S,)> for DiskSpace<Unselected> {
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 diskspace <target>` as a child process with piped standard
    /// output and error streams, where `target` is a `journalPrefix` or a
    /// depot name.
    fn spawn_with(&mut self, (target,): (S,)) -> Result<Self::Output, Self::Error> {
        self.setup_command(&self.bin)
            .arg(target)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cmd::args_of;

    fn args(cmd: &impl SubCommand) -> Vec<String> {
        args_of(&cmd.setup_command("p4"))
    }

    /// Dry-run checks of the assembled `p4 diskspace` command line; no
    /// process is spawned.
    #[test]
    fn bare_form_has_no_argument() {
        let cmd = DiskSpace::new("p4", GlobalOpts::new());

        assert_eq!(args(&cmd), ["diskspace"]);
    }

    #[test]
    fn predefined_targets() {
        assert_eq!(
            args(&DiskSpace::new("p4", GlobalOpts::new()).report_p4root()),
            ["diskspace", "P4ROOT"]
        );
        assert_eq!(
            args(&DiskSpace::new("p4", GlobalOpts::new()).report_p4journal()),
            ["diskspace", "P4JOURNAL"]
        );
        assert_eq!(
            args(&DiskSpace::new("p4", GlobalOpts::new()).report_p4log()),
            ["diskspace", "P4LOG"]
        );
        assert_eq!(
            args(&DiskSpace::new("p4", GlobalOpts::new()).report_temp()),
            ["diskspace", "TEMP"]
        );
    }

    #[test]
    fn targets_can_be_switched() {
        let cmd = DiskSpace::new("p4", GlobalOpts::new()).report_p4root();
        let cmd = cmd.report_temp();

        assert_eq!(args(&cmd), ["diskspace", "TEMP"]);
    }

    #[test]
    fn global_opts_are_injected_once() {
        let cmd = DiskSpace::new(
            "p4",
            GlobalOpts::new().port("localhost:1666").quiet_mode(true),
        )
        .report_p4root();

        assert_eq!(
            args(&cmd),
            ["-p", "localhost:1666", "-q", "diskspace", "P4ROOT"]
        );
    }

    #[test]
    fn bin_accessors_update_executable_path() {
        let mut cmd = DiskSpace::new("p4", GlobalOpts::new()).report_temp();
        assert_eq!(cmd.get_bin(), Path::new("p4"));

        cmd.set_bin("/usr/local/bin/p4");
        assert_eq!(cmd.get_bin(), Path::new("/usr/local/bin/p4"));

        let cmd = cmd.bin("p4.exe");
        assert_eq!(cmd.get_bin(), Path::new("p4.exe"));
    }
}