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