perforce-cli 0.1.0-alpha.11

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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};

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

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

/// Upper bound (in seconds) of the `-i` interval: the number of seconds in
/// 31 days.
const MAX_INTERVAL_SECONDS: u64 = 2_678_400;

// ---------------------------------------------------------------------------
// Task-target dimension: `-e command` vs `-t triggerName`
// ---------------------------------------------------------------------------

/// The task is given as a command string on the command line (`-e command`).
///
/// Entered with [`BackGroundTask::execute`]; mutually exclusive with
/// [`TriggerMode`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommandMode {
    execute: String,
}

impl ExclusiveOption for CommandMode {
    fn inject_args(&self, command: &mut Command) {
        command.arg("-e").arg(&self.execute);
    }
}

/// The task is a trigger already defined in the triggers table
/// (`-t triggerName`).
///
/// Entered with [`BackGroundTask::trigger`]; mutually exclusive with
/// [`CommandMode`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TriggerMode {
    trigger: String,
}

impl ExclusiveOption for TriggerMode {
    fn inject_args(&self, command: &mut Command) {
        command.arg("-t").arg(&self.trigger);
    }
}

/// Marker for a selected task target: one of [`CommandMode`] or
/// [`TriggerMode`].
///
/// Exactly one of `-e` / `-t` is required, so spawning is only available once
/// [`BackGroundTask`] has transitioned into one of these states.
pub trait TaskTarget: ExclusiveOption {}

impl TaskTarget for CommandMode {}
impl TaskTarget for TriggerMode {}

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

/// Type-safe builder for `p4 bgtask`.
///
/// Run background commands or triggers on the server. The server saves the
/// task output to its log file; this command requires `super` access.
///
/// # Syntax
///
/// ```text
/// p4 [g-opts] bgtask [-b retries] [-d] [-i interval] [-m runCount]
///                    [-w seconds] {-e command | -t triggerName}
/// ```
///
/// The `M` type parameter tracks the required task target at compile time:
/// it starts as [`Unselected`], and one of [`Self::execute`] /
/// [`Self::trigger`] must be used to move into the [`CommandMode`] /
/// [`TriggerMode`] state before the command can be spawned. The two states
/// can be switched freely with the same two methods.
#[derive(Debug, Clone, Default)]
pub struct BackGroundTask<M = Unselected> {
    bin: PathBuf,

    global_opts: GlobalOpts,

    /// `-b retries`
    retries: Option<u64>,

    /// `-d`
    detach: bool,

    /// `-i interval`
    interval: Option<u64>,

    /// `-m runCount`
    runs: Option<u64>,

    /// `-w seconds`
    wait: Option<u64>,

    mode: M,
}

impl BackGroundTask<Unselected> {
    /// Creates a new `p4 bgtask` command with no task target selected.
    ///
    /// `bin` is the path to the Perforce command-line executable. Use
    /// [`Self::execute`] or [`Self::trigger`] to select the required
    /// `-e` / `-t` target.
    pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
        Self {
            bin: bin.into(),
            global_opts,
            ..Default::default()
        }
    }
}

impl<M> BackGroundTask<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
    }

    /// Selects `-e command`: run the given command string on the server.
    ///
    /// This transitions into the [`CommandMode`] state, discarding any
    /// previously selected `-e` / `-t` target; the scheduling options are
    /// preserved.
    pub fn execute(self, command: impl Into<String>) -> BackGroundTask<CommandMode> {
        BackGroundTask {
            bin: self.bin,
            global_opts: self.global_opts,
            retries: self.retries,
            detach: self.detach,
            interval: self.interval,
            runs: self.runs,
            wait: self.wait,
            mode: CommandMode {
                execute: command.into(),
            },
        }
    }

    /// Selects `-t triggerName`: run the named bgtask trigger, already defined
    /// in the triggers table, on the server.
    ///
    /// This transitions into the [`TriggerMode`] state, discarding any
    /// previously selected `-e` / `-t` target; the scheduling options are
    /// preserved.
    pub fn trigger(self, name: impl Into<String>) -> BackGroundTask<TriggerMode> {
        BackGroundTask {
            bin: self.bin,
            global_opts: self.global_opts,
            retries: self.retries,
            detach: self.detach,
            interval: self.interval,
            runs: self.runs,
            wait: self.wait,
            mode: TriggerMode {
                trigger: name.into(),
            },
        }
    }

    /// # Description
    ///
    /// g-opts
    ///
    #[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(
        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(
        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
    }

    /// # Description
    ///
    /// -b retries
    ///
    /// Maximum number of execution errors before ceasing to attempt
    /// execution. The default is `1`.
    pub fn get_retries(&self) -> Option<u64> {
        self.retries
    }

    /// # Description
    ///
    /// -b retries
    ///
    /// Maximum number of execution errors before ceasing to attempt
    /// execution. The default is `1`.
    pub fn set_retries(&mut self, v: u64) -> &mut Self {
        self.retries = Some(v);
        self
    }

    /// # Description
    ///
    /// -b retries
    ///
    /// Maximum number of execution errors before ceasing to attempt
    /// execution. The default is `1`.
    pub fn retries(mut self, v: u64) -> Self {
        self.retries = Some(v);
        self
    }

    /// # Description
    ///
    /// -d
    ///
    /// Detach the client so it does not see the output of the server-side
    /// task execution. Not allowed when the task is specified in a
    /// `startup.N` configurable.
    pub fn get_detach(&self) -> bool {
        self.detach
    }

    /// # Description
    ///
    /// -d
    ///
    /// Detach the client so it does not see the output of the server-side
    /// task execution. Not allowed when the task is specified in a
    /// `startup.N` configurable.
    pub fn set_detach(&mut self, v: bool) -> &mut Self {
        self.detach = v;
        self
    }

    /// # Description
    ///
    /// -d
    ///
    /// Detach the client so it does not see the output of the server-side
    /// task execution. Not allowed when the task is specified in a
    /// `startup.N` configurable.
    pub fn detach(mut self, v: bool) -> Self {
        self.detach = v;
        self
    }

    /// # Description
    ///
    /// -i interval
    ///
    /// Seconds between command invocations. The default is `1` second; the
    /// maximum is 2,678,400 (the number of seconds in 31 days).
    pub fn get_interval(&self) -> Option<u64> {
        self.interval
    }

    /// # Description
    ///
    /// -i interval
    ///
    /// Seconds between command invocations. The default is `1` second; the
    /// maximum is 2,678,400 (the number of seconds in 31 days).
    pub fn set_interval(&mut self, v: u64) -> &mut Self {
        debug_assert!(
            v <= MAX_INTERVAL_SECONDS,
            "bgtask -i interval must not exceed {MAX_INTERVAL_SECONDS} seconds (31 days)"
        );
        self.interval = Some(v);
        self
    }

    /// # Description
    ///
    /// -i interval
    ///
    /// Seconds between command invocations. The default is `1` second; the
    /// maximum is 2,678,400 (the number of seconds in 31 days).
    pub fn interval(mut self, v: u64) -> Self {
        debug_assert!(
            v <= MAX_INTERVAL_SECONDS,
            "bgtask -i interval must not exceed {MAX_INTERVAL_SECONDS} seconds (31 days)"
        );
        self.interval = Some(v);
        self
    }

    /// # Description
    ///
    /// -m runCount
    ///
    /// Maximum number of times the command is run. The default is `1`.
    pub fn get_runs(&self) -> Option<u64> {
        self.runs
    }

    /// # Description
    ///
    /// -m runCount
    ///
    /// Maximum number of times the command is run. The default is `1`.
    pub fn set_runs(&mut self, v: u64) -> &mut Self {
        self.runs = Some(v);
        self
    }

    /// # Description
    ///
    /// -m runCount
    ///
    /// Maximum number of times the command is run. The default is `1`.
    pub fn runs(mut self, v: u64) -> Self {
        self.runs = Some(v);
        self
    }

    /// # Description
    ///
    /// -w seconds
    ///
    /// Seconds to wait after an execution error before attempting the next
    /// execution. The default is `5`.
    pub fn get_wait(&self) -> Option<u64> {
        self.wait
    }

    /// # Description
    ///
    /// -w seconds
    ///
    /// Seconds to wait after an execution error before attempting the next
    /// execution. The default is `5`.
    pub fn set_wait(&mut self, v: u64) -> &mut Self {
        self.wait = Some(v);
        self
    }

    /// # Description
    ///
    /// -w seconds
    ///
    /// Seconds to wait after an execution error before attempting the next
    /// execution. The default is `5`.
    pub fn wait(mut self, v: u64) -> Self {
        self.wait = Some(v);
        self
    }
}

impl BackGroundTask<CommandMode> {
    /// The command string selected with `-e`.
    pub fn get_command(&self) -> &str {
        &self.mode.execute
    }
}

impl BackGroundTask<TriggerMode> {
    /// The trigger name selected with `-t`.
    pub fn get_trigger(&self) -> &str {
        &self.mode.trigger
    }
}

// ---------------------------------------------------------------------------
// SubCommand
// ---------------------------------------------------------------------------

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

    fn inject_local_args(&self, command: &mut Command) {
        if let Some(retries) = self.retries {
            command.arg("-b").arg(retries.to_string());
        }

        if self.detach {
            command.arg("-d");
        }

        if let Some(interval) = self.interval {
            command.arg("-i").arg(interval.to_string());
        }

        if let Some(runs) = self.runs {
            command.arg("-m").arg(runs.to_string());
        }

        if let Some(wait) = self.wait {
            command.arg("-w").arg(wait.to_string());
        }

        self.mode.inject_args(command);
    }

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

// ---------------------------------------------------------------------------
// Spawning: only after a task target (`-e` / `-t`) has been selected
// ---------------------------------------------------------------------------

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

    /// Spawns `p4 bgtask` 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()
    }
}

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

    #[test]
    fn bin_accessors_update_executable_path() {
        let mut command = BackGroundTask::new("p4", GlobalOpts::new()).trigger("verify");
        assert_eq!(command.get_bin(), Path::new("p4"));

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

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

    /// Dry-run checks of the assembled `p4 bgtask` command line; no process
    /// is spawned.
    #[test]
    fn execute_target_only() {
        let task = BackGroundTask::new("p4", GlobalOpts::new()).execute("top -b -n 1");

        assert_eq!(
            args_of(&task.setup_command("p4")),
            ["bgtask", "-e", "top -b -n 1"]
        );
        assert_eq!(task.get_command(), "top -b -n 1");
    }

    #[test]
    fn trigger_target_only() {
        let task = BackGroundTask::new("p4", GlobalOpts::new()).trigger("log_checker");

        assert_eq!(
            args_of(&task.setup_command("p4")),
            ["bgtask", "-t", "log_checker"]
        );
        assert_eq!(task.get_trigger(), "log_checker");
    }

    #[test]
    fn all_options_injected_in_syntax_order() {
        let task = BackGroundTask::new("p4", GlobalOpts::new())
            .retries(3)
            .detach(true)
            .interval(86_400)
            .runs(5)
            .wait(10)
            .trigger("p4dstate");

        assert_eq!(
            args_of(&task.setup_command("p4")),
            [
                "bgtask", "-b", "3", "-d", "-i", "86400", "-m", "5", "-w", "10", "-t", "p4dstate"
            ]
        );
    }

    #[test]
    fn global_opts_are_injected_once() {
        let task =
            BackGroundTask::new("p4", GlobalOpts::new().port("localhost:1666")).execute("verify");

        assert_eq!(
            args_of(&task.setup_command("p4")),
            ["-p", "localhost:1666", "bgtask", "-e", "verify"]
        );
    }

    #[test]
    fn switching_targets_replaces_selection() {
        let task = BackGroundTask::new("p4", GlobalOpts::new())
            .runs(2)
            .execute("old command")
            .trigger("verify");

        assert_eq!(
            args_of(&task.setup_command("p4")),
            ["bgtask", "-m", "2", "-t", "verify"]
        );
        assert_eq!(task.get_trigger(), "verify");

        let task = task.execute("new command");
        assert_eq!(
            args_of(&task.setup_command("p4")),
            ["bgtask", "-m", "2", "-e", "new command"]
        );
        assert_eq!(task.get_command(), "new command");
    }

    #[test]
    fn interval_accepts_the_document_maximum() {
        let task = BackGroundTask::new("p4", GlobalOpts::new())
            .interval(MAX_INTERVAL_SECONDS)
            .trigger("verify");

        assert_eq!(
            args_of(&task.setup_command("p4")),
            ["bgtask", "-i", "2678400", "-t", "verify"]
        );
    }

    #[test]
    #[should_panic(expected = "31 days")]
    fn interval_above_maximum_panics_in_debug() {
        let _ = BackGroundTask::new("p4", GlobalOpts::new()).interval(MAX_INTERVAL_SECONDS + 1);
    }

    #[test]
    fn setters_update_the_same_command() {
        let mut task = BackGroundTask::new("p4", GlobalOpts::new()).trigger("verify");
        task.set_retries(4).set_detach(true).set_wait(30);

        assert_eq!(task.get_retries(), Some(4));
        assert!(task.get_detach());
        assert_eq!(task.get_wait(), Some(30));
        assert_eq!(
            args_of(&task.setup_command("p4")),
            ["bgtask", "-b", "4", "-d", "-w", "30", "-t", "verify"]
        );
    }
}