Skip to main content

perforce_cli/cmd/
configure.rs

1use std::path::{Path, PathBuf};
2use std::process::{Child, Command, Stdio};
3
4use super::{SubCommand, Unselected};
5
6use crate::global::GlobalOpts;
7use crate::spawn::ParameterizedSpawn;
8
9/// The optional positional argument of `p4 configure show` and
10/// `p4 configure history`.
11///
12/// The argument selects what the report covers: all servers
13/// ([`Target::AllServers`]), a named server ([`Target::Server`]), or a
14/// single configuration variable ([`Target::Variable`]). When no target is
15/// selected, the report covers the current server.
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub enum Target {
18    /// Reports the configuration variables for all servers known to the
19    /// system; injected as the `allservers` keyword.
20    AllServers,
21
22    /// Reports the settings of the named server.
23    Server(String),
24
25    /// Reports the setting(s) of the specified variable.
26    Variable(String),
27}
28
29// ---------------------------------------------------------------------------
30// Entry point
31// ---------------------------------------------------------------------------
32
33/// Type-safe builder for `p4 configure`.
34///
35#[cfg_attr(
36    feature = "lt2019_2",
37    doc = "Set and manage server configuration variables."
38)]
39#[cfg_attr(
40    all(not(feature = "lt2019_2"), feature = "lt2024_1"),
41    doc = "Set, view, and manage server configuration variables."
42)]
43#[cfg_attr(
44    not(feature = "lt2024_1"),
45    doc = "Set, view, and manage server configuration variables, including comments."
46)]
47///
48/// Configuration variables are used to control and customize the behavior
49/// of the Perforce service.
50///
51/// The `T` type parameter tracks the selected operation at compile time: it
52/// starts as [`Unselected`], in which the command has no effect and cannot
53/// be spawned. Use one of the builder methods to select the operation:
54/// [`set`](Self::set), [`unset`](Self::unset), or [`show`](Self::show)
55#[cfg_attr(not(feature = "lt2019_2"), doc = ", [`history`](Self::history)")]
56#[cfg_attr(feature = "lt2023_2", doc = ".")]
57#[cfg_attr(
58    not(feature = "lt2023_2"),
59    doc = ", [`patch`](Self::patch), or [`help`](Self::help)."
60)]
61#[derive(Debug, Clone, Default)]
62pub struct Configure<T = Unselected> {
63    bin: PathBuf,
64
65    global_opts: GlobalOpts,
66
67    sub_command: T,
68}
69
70impl Configure<Unselected> {
71    /// Creates a new `p4 configure` command with no operation selected.
72    ///
73    /// `bin` is the path to the Perforce command-line executable. The
74    /// command has no effect in this state; use one of the builder methods
75    /// to select the operation before spawning.
76    pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
77        Self {
78            bin: bin.into(),
79            global_opts,
80            sub_command: Unselected,
81        }
82    }
83
84    /// Get the Perforce command path.
85    pub fn get_bin(&self) -> &Path {
86        &self.bin
87    }
88
89    /// Set the Perforce command path.
90    pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
91        self.bin = bin.into();
92        self
93    }
94
95    /// Set the Perforce command path.
96    pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
97        self.set_bin(bin);
98        self
99    }
100
101    /// Selects the `set` operation.
102    ///
103    /// The `variable` and `value` are required; the optional `server_id`
104    /// prefix and the `--comment` refinement are set on the returned
105    /// builder.
106    pub fn set(
107        self,
108        variable: impl Into<String>,
109        value: impl Into<String>,
110    ) -> Configure<SetVariable> {
111        Configure {
112            bin: self.bin,
113            global_opts: self.global_opts,
114            sub_command: SetVariable {
115                server_id: None,
116                variable: variable.into(),
117                value: value.into(),
118                ..Default::default()
119            },
120        }
121    }
122
123    /// Selects the `unset` operation.
124    ///
125    /// The `variable` is required; the optional `server_id` prefix and the
126    /// `--comment` refinement are set on the returned builder.
127    pub fn unset(self, variable: impl Into<String>) -> Configure<UnSetVariable> {
128        Configure {
129            bin: self.bin,
130            global_opts: self.global_opts,
131            sub_command: UnSetVariable {
132                server_id: None,
133                variable: variable.into(),
134                ..Default::default()
135            },
136        }
137    }
138
139    /// Selects the `show` operation.
140    ///
141    /// The returned builder spawns the bare `p4 configure show` report; use
142    /// [`allservers`](Configure::allservers), [`server`](Configure::server),
143    /// or [`variable`](Configure::variable) to restrict the report.
144    pub fn show(self) -> Configure<Show> {
145        Configure {
146            bin: self.bin,
147            global_opts: self.global_opts,
148            sub_command: Show::default(),
149        }
150    }
151
152    /// Selects the `history` operation.
153    ///
154    /// The returned builder spawns the bare `p4 configure history` report;
155    /// use [`allservers`](Configure::allservers),
156    /// [`server`](Configure::server), or [`variable`](Configure::variable)
157    /// to restrict the report.
158    #[cfg(not(feature = "lt2019_2"))]
159    pub fn history(self) -> Configure<History> {
160        Configure {
161            bin: self.bin,
162            global_opts: self.global_opts,
163            sub_command: History::default(),
164        }
165    }
166
167    /// Selects the `p4 configure history` comment patch form.
168    ///
169    /// Sets the `comment` on the history of `variable`, optionally targeting
170    /// a specific history entry with [`iteration`](Configure::iteration).
171    #[cfg(not(feature = "lt2023_2"))]
172    pub fn patch(
173        self,
174        variable: impl Into<String>,
175        comment: impl Into<String>,
176    ) -> Configure<Patch> {
177        Configure {
178            bin: self.bin,
179            global_opts: self.global_opts,
180            sub_command: Patch {
181                iteration: None,
182                comment: comment.into(),
183                variable: variable.into(),
184            },
185        }
186    }
187
188    /// Selects the `help` operation.
189    #[cfg(not(feature = "lt2023_2"))]
190    pub fn help(self) -> Configure<Help> {
191        Configure {
192            bin: self.bin,
193            global_opts: self.global_opts,
194            sub_command: Help,
195        }
196    }
197}
198
199// ---------------------------------------------------------------------------
200// Carrier
201// ---------------------------------------------------------------------------
202
203impl<T: SubCommand> Configure<T> {
204    /// Get the Perforce command path.
205    pub fn get_bin(&self) -> &Path {
206        &self.bin
207    }
208
209    /// Set the Perforce command path.
210    pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
211        self.bin = bin.into();
212        self
213    }
214
215    /// Set the Perforce command path.
216    pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
217        self.set_bin(bin);
218        self
219    }
220
221    /// Spawns the assembled `p4 configure` command as a child process with
222    /// piped standard output and error streams; use the returned [`Child`]
223    /// handle to wait for it or interact with it.
224    fn spawn_piped(&mut self) -> Result<Child, std::io::Error> {
225        self.setup_command(&self.bin)
226            .stdout(Stdio::piped())
227            .stderr(Stdio::piped())
228            .spawn()
229    }
230}
231
232impl<T: SubCommand> SubCommand for Configure<T> {
233    fn name(&self) -> &str {
234        "configure"
235    }
236
237    fn inject_local_args(&self, command: &mut Command) {
238        self.sub_command.inject_args(command);
239    }
240
241    fn global_opts(&self) -> Option<&GlobalOpts> {
242        Some(&self.global_opts)
243    }
244}
245
246// ---------------------------------------------------------------------------
247// configure set
248// ---------------------------------------------------------------------------
249
250/// The `p4 configure set` operation.
251///
252/// Sets the named variable to the provided value.
253///
254/// # Syntax
255///
256/// ```text
257#[cfg_attr(
258    feature = "lt2020_1",
259    doc = "p4 [g-opts] configure set [P4NAME#|serverid#]variable=value",
260    doc = "```"
261)]
262#[cfg_attr(
263    all(not(feature = "lt2020_1"), feature = "lt2023_2"),
264    doc = "p4 [g-opts] configure set [serverid#]variable=value",
265    doc = "```"
266)]
267#[cfg_attr(
268    not(feature = "lt2023_2"),
269    doc = "p4 [g-opts] configure set [--comment=comment] [serverid#]variable=value",
270    doc = "```"
271)]
272#[derive(Debug, Clone, Default)]
273pub struct SetVariable {
274    /// `[serverid#]`
275    server_id: Option<String>,
276
277    /// `variable`
278    variable: String,
279
280    /// `value`
281    value: String,
282
283    /// `--comment=comment`
284    #[cfg(not(feature = "lt2023_2"))]
285    comment: Option<String>,
286}
287
288impl Configure<SetVariable> {
289    /// Returns the server whose configuration is being set, if any.
290    pub fn get_server_id(&self) -> Option<&str> {
291        self.sub_command.server_id.as_deref()
292    }
293
294    /// Sets the `[serverid#]` prefix.
295    pub fn set_server_id(&mut self, v: impl Into<String>) -> &mut Self {
296        self.sub_command.server_id = Some(v.into());
297        self
298    }
299
300    /// Sets the `[serverid#]` prefix.
301    pub fn server_id(mut self, v: impl Into<String>) -> Self {
302        self.set_server_id(v);
303        self
304    }
305
306    /// Returns the name of the variable being set.
307    pub fn get_variable(&self) -> &str {
308        &self.sub_command.variable
309    }
310
311    /// Sets the name of the variable being set.
312    pub fn set_variable(&mut self, v: impl Into<String>) -> &mut Self {
313        self.sub_command.variable = v.into();
314        self
315    }
316
317    /// Sets the name of the variable being set.
318    pub fn variable(mut self, v: impl Into<String>) -> Self {
319        self.set_variable(v);
320        self
321    }
322
323    /// Returns the value being assigned.
324    pub fn get_value(&self) -> &str {
325        &self.sub_command.value
326    }
327
328    /// Sets the value to assign.
329    pub fn set_value(&mut self, v: impl Into<String>) -> &mut Self {
330        self.sub_command.value = v.into();
331        self
332    }
333
334    /// Sets the value to assign.
335    pub fn value(mut self, v: impl Into<String>) -> Self {
336        self.set_value(v);
337        self
338    }
339
340    /// Returns the comment attached to the change, if any.
341    #[cfg(not(feature = "lt2023_2"))]
342    pub fn get_comment(&self) -> Option<&str> {
343        self.sub_command.comment.as_deref()
344    }
345
346    /// Sets `--comment=comment`.
347    #[cfg(not(feature = "lt2023_2"))]
348    pub fn set_comment(&mut self, v: impl Into<String>) -> &mut Self {
349        self.sub_command.comment = Some(v.into());
350        self
351    }
352
353    /// Sets `--comment=comment`.
354    #[cfg(not(feature = "lt2023_2"))]
355    pub fn comment(mut self, v: impl Into<String>) -> Self {
356        self.set_comment(v);
357        self
358    }
359}
360
361impl SubCommand for SetVariable {
362    fn name(&self) -> &str {
363        "set"
364    }
365
366    fn inject_local_args(&self, command: &mut Command) {
367        #[cfg(not(feature = "lt2023_2"))]
368        {
369            if let Some(comment) = &self.comment {
370                command.arg(format!("--comment={}", comment));
371            }
372        }
373
374        let variable = match &self.server_id {
375            Some(server_id) => format!("{}#{}", server_id, self.variable),
376            None => self.variable.clone(),
377        };
378        command.arg(format!("{}={}", variable, self.value));
379    }
380}
381
382impl ParameterizedSpawn<()> for Configure<SetVariable> {
383    type Output = Child;
384    type Error = std::io::Error;
385
386    fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
387        self.spawn_piped()
388    }
389}
390
391// ---------------------------------------------------------------------------
392// configure unset
393// ---------------------------------------------------------------------------
394
395/// The `p4 configure unset` operation.
396///
397/// Unsets the named configuration variable.
398///
399/// # Syntax
400///
401/// ```text
402#[cfg_attr(
403    feature = "lt2020_1",
404    doc = "p4 [g-opts] configure unset [P4NAME#|serverid#]variable",
405    doc = "```"
406)]
407#[cfg_attr(
408    all(not(feature = "lt2020_1"), feature = "lt2023_2"),
409    doc = "p4 [g-opts] configure unset [serverid#]variable",
410    doc = "```"
411)]
412#[cfg_attr(
413    not(feature = "lt2023_2"),
414    doc = "p4 [g-opts] configure unset [--comment=comment] [serverid#]variable",
415    doc = "```"
416)]
417#[derive(Debug, Clone, Default)]
418pub struct UnSetVariable {
419    /// `[serverid#]`
420    server_id: Option<String>,
421
422    /// `variable`
423    variable: String,
424
425    /// `--comment=comment`
426    #[cfg(not(feature = "lt2023_2"))]
427    comment: Option<String>,
428}
429
430impl Configure<UnSetVariable> {
431    /// Returns the server whose configuration is being unset, if any.
432    pub fn get_server_id(&self) -> Option<&str> {
433        self.sub_command.server_id.as_deref()
434    }
435
436    /// Sets the `[serverid#]` prefix.
437    pub fn set_server_id(&mut self, v: impl Into<String>) -> &mut Self {
438        self.sub_command.server_id = Some(v.into());
439        self
440    }
441
442    /// Sets the `[serverid#]` prefix.
443    pub fn server_id(mut self, v: impl Into<String>) -> Self {
444        self.set_server_id(v);
445        self
446    }
447
448    /// Returns the name of the variable being unset.
449    pub fn get_variable(&self) -> &str {
450        &self.sub_command.variable
451    }
452
453    /// Sets the name of the variable being unset.
454    pub fn set_variable(&mut self, v: impl Into<String>) -> &mut Self {
455        self.sub_command.variable = v.into();
456        self
457    }
458
459    /// Sets the name of the variable being unset.
460    pub fn variable(mut self, v: impl Into<String>) -> Self {
461        self.set_variable(v);
462        self
463    }
464
465    /// Returns the comment attached to the change, if any.
466    #[cfg(not(feature = "lt2023_2"))]
467    pub fn get_comment(&self) -> Option<&str> {
468        self.sub_command.comment.as_deref()
469    }
470
471    /// Sets `--comment=comment`.
472    #[cfg(not(feature = "lt2023_2"))]
473    pub fn set_comment(&mut self, v: impl Into<String>) -> &mut Self {
474        self.sub_command.comment = Some(v.into());
475        self
476    }
477
478    /// Sets `--comment=comment`.
479    #[cfg(not(feature = "lt2023_2"))]
480    pub fn comment(mut self, v: impl Into<String>) -> Self {
481        self.set_comment(v);
482        self
483    }
484}
485
486impl SubCommand for UnSetVariable {
487    fn name(&self) -> &str {
488        "unset"
489    }
490
491    fn inject_local_args(&self, command: &mut Command) {
492        #[cfg(not(feature = "lt2023_2"))]
493        {
494            if let Some(comment) = &self.comment {
495                command.arg(format!("--comment={}", comment));
496            }
497        }
498
499        let variable = match &self.server_id {
500            Some(server_id) => format!("{}#{}", server_id, self.variable),
501            None => self.variable.clone(),
502        };
503        command.arg(variable);
504    }
505}
506
507impl ParameterizedSpawn<()> for Configure<UnSetVariable> {
508    type Output = Child;
509    type Error = std::io::Error;
510
511    fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
512        self.spawn_piped()
513    }
514}
515
516// ---------------------------------------------------------------------------
517// configure show
518// ---------------------------------------------------------------------------
519
520/// The `p4 configure show` operation.
521///
522/// Displays the current configuration of this server.
523///
524/// # Syntax
525///
526/// ```text
527#[cfg_attr(
528    feature = "lt2020_1",
529    doc = "p4 [g-opts] configure show [allservers | P4NAME | variable]",
530    doc = "```"
531)]
532#[cfg_attr(
533    all(not(feature = "lt2020_1"), feature = "lt2023_2"),
534    doc = "p4 [g-opts] configure show [allservers | variable]",
535    doc = "```"
536)]
537#[cfg_attr(
538    not(feature = "lt2023_2"),
539    doc = "p4 [g-opts] configure show [allservers | serverid | variable]",
540    doc = "```"
541)]
542#[derive(Debug, Clone, Default)]
543pub struct Show {
544    /// The optional positional target of the report.
545    target: Option<Target>,
546}
547
548impl Configure<Show> {
549    /// Returns the selected target, if any.
550    pub fn get_target(&self) -> Option<&Target> {
551        self.sub_command.target.as_ref()
552    }
553
554    /// Sets the positional target of the report.
555    pub fn set_target(&mut self, target: Option<Target>) -> &mut Self {
556        self.sub_command.target = target;
557        self
558    }
559
560    /// Sets the positional target of the report.
561    pub fn target(mut self, target: Option<Target>) -> Self {
562        self.set_target(target);
563        self
564    }
565
566    /// Restricts the report to all servers known to the system.
567    pub fn allservers(mut self) -> Self {
568        self.sub_command.target = Some(Target::AllServers);
569        self
570    }
571
572    /// Restricts the report to the named server.
573    pub fn server(mut self, v: impl Into<String>) -> Self {
574        self.sub_command.target = Some(Target::Server(v.into()));
575        self
576    }
577
578    /// Restricts the report to the specified variable.
579    pub fn variable(mut self, v: impl Into<String>) -> Self {
580        self.sub_command.target = Some(Target::Variable(v.into()));
581        self
582    }
583}
584
585impl SubCommand for Show {
586    fn name(&self) -> &str {
587        "show"
588    }
589
590    fn inject_local_args(&self, command: &mut Command) {
591        match &self.target {
592            Some(Target::AllServers) => {
593                command.arg("allservers");
594            }
595            Some(Target::Server(name)) => {
596                command.arg(name);
597            }
598            Some(Target::Variable(name)) => {
599                command.arg(name);
600            }
601            None => {}
602        }
603    }
604}
605
606impl ParameterizedSpawn<()> for Configure<Show> {
607    type Output = Child;
608    type Error = std::io::Error;
609
610    fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
611        self.spawn_piped()
612    }
613}
614
615// ---------------------------------------------------------------------------
616// configure history
617// ---------------------------------------------------------------------------
618
619/// The `p4 configure history` operation.
620///
621/// Displays the history of configurables' values recorded by the server.
622///
623/// # Syntax
624///
625/// ```text
626#[cfg_attr(
627    feature = "lt2020_1",
628    doc = "p4 [g-opts] configure history [allservers | P4NAME | variable]",
629    doc = "```"
630)]
631#[cfg_attr(
632    all(not(feature = "lt2020_1"), feature = "lt2023_2"),
633    doc = "p4 [g-opts] configure history [allservers | variable]",
634    doc = "```"
635)]
636#[cfg_attr(
637    not(feature = "lt2023_2"),
638    doc = "p4 [g-opts] configure history [allservers | serverid | variable]",
639    doc = "```"
640)]
641#[cfg(not(feature = "lt2019_2"))]
642#[derive(Debug, Clone, Default)]
643pub struct History {
644    /// The optional positional target of the report.
645    target: Option<Target>,
646}
647
648#[cfg(not(feature = "lt2019_2"))]
649impl Configure<History> {
650    /// Returns the selected target, if any.
651    pub fn get_target(&self) -> Option<&Target> {
652        self.sub_command.target.as_ref()
653    }
654
655    /// Sets the positional target of the report.
656    pub fn set_target(&mut self, target: Option<Target>) -> &mut Self {
657        self.sub_command.target = target;
658        self
659    }
660
661    /// Sets the positional target of the report.
662    pub fn target(mut self, target: Option<Target>) -> Self {
663        self.set_target(target);
664        self
665    }
666
667    /// Restricts the report to all servers known to the system.
668    pub fn allservers(mut self) -> Self {
669        self.sub_command.target = Some(Target::AllServers);
670        self
671    }
672
673    /// Restricts the report to the named server.
674    pub fn server(mut self, v: impl Into<String>) -> Self {
675        self.sub_command.target = Some(Target::Server(v.into()));
676        self
677    }
678
679    /// Restricts the report to the specified variable.
680    pub fn variable(mut self, v: impl Into<String>) -> Self {
681        self.sub_command.target = Some(Target::Variable(v.into()));
682        self
683    }
684}
685
686#[cfg(not(feature = "lt2019_2"))]
687impl SubCommand for History {
688    fn name(&self) -> &str {
689        "history"
690    }
691
692    fn inject_local_args(&self, command: &mut Command) {
693        match &self.target {
694            Some(Target::AllServers) => {
695                command.arg("allservers");
696            }
697            Some(Target::Server(name)) => {
698                command.arg(name);
699            }
700            Some(Target::Variable(name)) => {
701                command.arg(name);
702            }
703            None => {}
704        }
705    }
706}
707
708#[cfg(not(feature = "lt2019_2"))]
709impl ParameterizedSpawn<()> for Configure<History> {
710    type Output = Child;
711    type Error = std::io::Error;
712
713    fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
714        self.spawn_piped()
715    }
716}
717
718// ---------------------------------------------------------------------------
719// configure history --comment (comment patch form)
720// ---------------------------------------------------------------------------
721
722/// The `p4 configure history` comment patch operation.
723///
724/// Passing the long option `--comment` sets the comment for the most recent
725/// history; the optional `--iteration=N` option can be used to retroactively
726/// set a comment on a specific history entry.
727///
728/// # Syntax
729///
730/// ```text
731/// p4 configure history [--iteration=N] --comment=comment variable
732/// ```
733#[cfg(not(feature = "lt2023_2"))]
734#[derive(Debug, Clone, Default)]
735pub struct Patch {
736    /// `--iteration=N`
737    iteration: Option<u64>,
738
739    /// `--comment=comment`
740    comment: String,
741
742    /// `variable`
743    variable: String,
744}
745
746#[cfg(not(feature = "lt2023_2"))]
747impl Configure<Patch> {
748    /// Returns the targeted history entry, if any.
749    pub fn get_iteration(&self) -> Option<u64> {
750        self.sub_command.iteration
751    }
752
753    /// Sets `--iteration=N`.
754    pub fn set_iteration(&mut self, v: u64) -> &mut Self {
755        self.sub_command.iteration = Some(v);
756        self
757    }
758
759    /// Sets `--iteration=N`.
760    pub fn iteration(mut self, v: u64) -> Self {
761        self.set_iteration(v);
762        self
763    }
764
765    /// Returns the comment being set.
766    pub fn get_comment(&self) -> &str {
767        &self.sub_command.comment
768    }
769
770    /// Sets the comment being set.
771    pub fn set_comment(&mut self, v: impl Into<String>) -> &mut Self {
772        self.sub_command.comment = v.into();
773        self
774    }
775
776    /// Sets the comment being set.
777    pub fn comment(mut self, v: impl Into<String>) -> Self {
778        self.set_comment(v);
779        self
780    }
781
782    /// Returns the name of the variable whose history is patched.
783    pub fn get_variable(&self) -> &str {
784        &self.sub_command.variable
785    }
786
787    /// Sets the name of the variable whose history is patched.
788    pub fn set_variable(&mut self, v: impl Into<String>) -> &mut Self {
789        self.sub_command.variable = v.into();
790        self
791    }
792
793    /// Sets the name of the variable whose history is patched.
794    pub fn variable(mut self, v: impl Into<String>) -> Self {
795        self.set_variable(v);
796        self
797    }
798}
799
800#[cfg(not(feature = "lt2023_2"))]
801impl SubCommand for Patch {
802    fn name(&self) -> &str {
803        "history"
804    }
805
806    fn inject_local_args(&self, command: &mut Command) {
807        if let Some(iteration) = self.iteration {
808            command.arg(format!("--iteration={}", iteration));
809        }
810
811        command
812            .arg(format!("--comment={}", self.comment))
813            .arg(&self.variable);
814    }
815}
816
817#[cfg(not(feature = "lt2023_2"))]
818impl ParameterizedSpawn<()> for Configure<Patch> {
819    type Output = Child;
820    type Error = std::io::Error;
821
822    fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
823        self.spawn_piped()
824    }
825}
826
827// ---------------------------------------------------------------------------
828// configure help
829// ---------------------------------------------------------------------------
830
831/// The `p4 configure help` operation.
832///
833/// Shows details about all the documented and supported configurables. The
834/// output is best redirected to a file because it has about 4,000 lines.
835#[cfg(not(feature = "lt2023_2"))]
836#[derive(Debug, Clone, Default)]
837pub struct Help;
838
839#[cfg(not(feature = "lt2023_2"))]
840impl SubCommand for Help {
841    fn name(&self) -> &str {
842        "help"
843    }
844
845    fn inject_local_args(&self, _command: &mut Command) {}
846}
847
848#[cfg(not(feature = "lt2023_2"))]
849impl ParameterizedSpawn<()> for Configure<Help> {
850    type Output = Child;
851    type Error = std::io::Error;
852
853    fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
854        self.spawn_piped()
855    }
856}
857
858#[cfg(test)]
859mod tests {
860    use super::*;
861    use crate::cmd::args_of;
862
863    fn entry() -> Configure<Unselected> {
864        Configure::new("p4", GlobalOpts::default())
865    }
866
867    #[cfg(not(feature = "lt2023_2"))]
868    #[test]
869    fn set_injects_options_in_syntax_order() {
870        let command = entry()
871            .set("monitor", "2")
872            .server_id("edge1")
873            .comment("why");
874
875        assert_eq!(
876            args_of(&command.setup_command("p4")),
877            ["configure", "set", "--comment=why", "edge1#monitor=2"]
878        );
879    }
880
881    #[test]
882    fn set_without_optional_parts() {
883        let command = entry().set("monitor", "2");
884
885        assert_eq!(
886            args_of(&command.setup_command("p4")),
887            ["configure", "set", "monitor=2"]
888        );
889    }
890
891    #[test]
892    fn set_accessors_reflect_changes() {
893        let mut command = entry().set("monitor", "2");
894
895        command.set_variable("security").set_value("4");
896        assert_eq!(command.get_variable(), "security");
897        assert_eq!(command.get_value(), "4");
898        assert_eq!(command.get_server_id(), None);
899
900        command.set_server_id("edge1");
901        assert_eq!(command.get_server_id(), Some("edge1"));
902    }
903
904    #[cfg(not(feature = "lt2023_2"))]
905    #[test]
906    fn unset_injects_options_in_syntax_order() {
907        let command = entry()
908            .unset("net.parallel.max")
909            .server_id("edge1")
910            .comment("unused");
911
912        assert_eq!(
913            args_of(&command.setup_command("p4")),
914            [
915                "configure",
916                "unset",
917                "--comment=unused",
918                "edge1#net.parallel.max"
919            ]
920        );
921    }
922
923    #[test]
924    fn show_target_variants() {
925        let bare = entry().show();
926        assert_eq!(args_of(&bare.setup_command("p4")), ["configure", "show"]);
927        assert_eq!(bare.get_target(), None);
928
929        let all = entry().show().allservers();
930        assert_eq!(
931            args_of(&all.setup_command("p4")),
932            ["configure", "show", "allservers"]
933        );
934
935        let server = entry().show().server("paris-edge");
936        assert_eq!(
937            args_of(&server.setup_command("p4")),
938            ["configure", "show", "paris-edge"]
939        );
940
941        let variable = entry().show().variable("monitor");
942        assert_eq!(
943            args_of(&variable.setup_command("p4")),
944            ["configure", "show", "monitor"]
945        );
946        assert_eq!(
947            variable.get_target(),
948            Some(&Target::Variable("monitor".to_string()))
949        );
950    }
951
952    #[test]
953    fn show_set_target_roundtrip() {
954        let mut command = entry().show();
955        command.set_target(Some(Target::Server("headquarters".to_string())));
956
957        assert_eq!(
958            args_of(&command.setup_command("p4")),
959            ["configure", "show", "headquarters"]
960        );
961
962        command.set_target(None);
963        assert_eq!(args_of(&command.setup_command("p4")), ["configure", "show"]);
964    }
965
966    #[cfg(not(feature = "lt2019_2"))]
967    #[test]
968    fn history_target_variants() {
969        let bare = entry().history();
970        assert_eq!(args_of(&bare.setup_command("p4")), ["configure", "history"]);
971
972        let all = entry().history().allservers();
973        assert_eq!(
974            args_of(&all.setup_command("p4")),
975            ["configure", "history", "allservers"]
976        );
977
978        let variable = entry().history().variable("monitor");
979        assert_eq!(
980            args_of(&variable.setup_command("p4")),
981            ["configure", "history", "monitor"]
982        );
983    }
984
985    #[cfg(not(feature = "lt2023_2"))]
986    #[test]
987    fn patch_injects_options_in_syntax_order() {
988        let command = entry().patch("server.allowfetch", "for DVCS").iteration(2);
989
990        assert_eq!(
991            args_of(&command.setup_command("p4")),
992            [
993                "configure",
994                "history",
995                "--iteration=2",
996                "--comment=for DVCS",
997                "server.allowfetch"
998            ]
999        );
1000        assert_eq!(command.get_iteration(), Some(2));
1001    }
1002
1003    #[cfg(not(feature = "lt2023_2"))]
1004    #[test]
1005    fn patch_without_iteration() {
1006        let command = entry().patch("server.allowfetch", "for DVCS");
1007
1008        assert_eq!(
1009            args_of(&command.setup_command("p4")),
1010            [
1011                "configure",
1012                "history",
1013                "--comment=for DVCS",
1014                "server.allowfetch"
1015            ]
1016        );
1017    }
1018
1019    #[cfg(not(feature = "lt2023_2"))]
1020    #[test]
1021    fn help_injects_no_local_arguments() {
1022        let command = entry().help();
1023
1024        assert_eq!(args_of(&command.setup_command("p4")), ["configure", "help"]);
1025    }
1026
1027    #[test]
1028    fn global_opts_are_injected_before_the_subcommand() {
1029        let mut global_opts = GlobalOpts::default();
1030        global_opts.set_user("bruno");
1031
1032        let command = Configure::new("p4", global_opts).set("monitor", "2");
1033
1034        assert_eq!(
1035            args_of(&command.setup_command("p4")),
1036            ["-u", "bruno", "configure", "set", "monitor=2"]
1037        );
1038    }
1039}