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#[derive(Debug, Clone, PartialEq, Eq)]
17pub enum Target {
18 AllServers,
21
22 Server(String),
24
25 Variable(String),
27}
28
29#[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#[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 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 pub fn get_bin(&self) -> &Path {
86 &self.bin
87 }
88
89 pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
91 self.bin = bin.into();
92 self
93 }
94
95 pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
97 self.set_bin(bin);
98 self
99 }
100
101 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 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 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 #[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 #[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 #[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
199impl<T: SubCommand> Configure<T> {
204 pub fn get_bin(&self) -> &Path {
206 &self.bin
207 }
208
209 pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
211 self.bin = bin.into();
212 self
213 }
214
215 pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
217 self.set_bin(bin);
218 self
219 }
220
221 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#[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 server_id: Option<String>,
276
277 variable: String,
279
280 value: String,
282
283 #[cfg(not(feature = "lt2023_2"))]
285 comment: Option<String>,
286}
287
288impl Configure<SetVariable> {
289 pub fn get_server_id(&self) -> Option<&str> {
291 self.sub_command.server_id.as_deref()
292 }
293
294 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 pub fn server_id(mut self, v: impl Into<String>) -> Self {
302 self.set_server_id(v);
303 self
304 }
305
306 pub fn get_variable(&self) -> &str {
308 &self.sub_command.variable
309 }
310
311 pub fn set_variable(&mut self, v: impl Into<String>) -> &mut Self {
313 self.sub_command.variable = v.into();
314 self
315 }
316
317 pub fn variable(mut self, v: impl Into<String>) -> Self {
319 self.set_variable(v);
320 self
321 }
322
323 pub fn get_value(&self) -> &str {
325 &self.sub_command.value
326 }
327
328 pub fn set_value(&mut self, v: impl Into<String>) -> &mut Self {
330 self.sub_command.value = v.into();
331 self
332 }
333
334 pub fn value(mut self, v: impl Into<String>) -> Self {
336 self.set_value(v);
337 self
338 }
339
340 #[cfg(not(feature = "lt2023_2"))]
342 pub fn get_comment(&self) -> Option<&str> {
343 self.sub_command.comment.as_deref()
344 }
345
346 #[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 #[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#[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 server_id: Option<String>,
421
422 variable: String,
424
425 #[cfg(not(feature = "lt2023_2"))]
427 comment: Option<String>,
428}
429
430impl Configure<UnSetVariable> {
431 pub fn get_server_id(&self) -> Option<&str> {
433 self.sub_command.server_id.as_deref()
434 }
435
436 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 pub fn server_id(mut self, v: impl Into<String>) -> Self {
444 self.set_server_id(v);
445 self
446 }
447
448 pub fn get_variable(&self) -> &str {
450 &self.sub_command.variable
451 }
452
453 pub fn set_variable(&mut self, v: impl Into<String>) -> &mut Self {
455 self.sub_command.variable = v.into();
456 self
457 }
458
459 pub fn variable(mut self, v: impl Into<String>) -> Self {
461 self.set_variable(v);
462 self
463 }
464
465 #[cfg(not(feature = "lt2023_2"))]
467 pub fn get_comment(&self) -> Option<&str> {
468 self.sub_command.comment.as_deref()
469 }
470
471 #[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 #[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#[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 target: Option<Target>,
546}
547
548impl Configure<Show> {
549 pub fn get_target(&self) -> Option<&Target> {
551 self.sub_command.target.as_ref()
552 }
553
554 pub fn set_target(&mut self, target: Option<Target>) -> &mut Self {
556 self.sub_command.target = target;
557 self
558 }
559
560 pub fn target(mut self, target: Option<Target>) -> Self {
562 self.set_target(target);
563 self
564 }
565
566 pub fn allservers(mut self) -> Self {
568 self.sub_command.target = Some(Target::AllServers);
569 self
570 }
571
572 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 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#[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 target: Option<Target>,
646}
647
648#[cfg(not(feature = "lt2019_2"))]
649impl Configure<History> {
650 pub fn get_target(&self) -> Option<&Target> {
652 self.sub_command.target.as_ref()
653 }
654
655 pub fn set_target(&mut self, target: Option<Target>) -> &mut Self {
657 self.sub_command.target = target;
658 self
659 }
660
661 pub fn target(mut self, target: Option<Target>) -> Self {
663 self.set_target(target);
664 self
665 }
666
667 pub fn allservers(mut self) -> Self {
669 self.sub_command.target = Some(Target::AllServers);
670 self
671 }
672
673 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 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#[cfg(not(feature = "lt2023_2"))]
734#[derive(Debug, Clone, Default)]
735pub struct Patch {
736 iteration: Option<u64>,
738
739 comment: String,
741
742 variable: String,
744}
745
746#[cfg(not(feature = "lt2023_2"))]
747impl Configure<Patch> {
748 pub fn get_iteration(&self) -> Option<u64> {
750 self.sub_command.iteration
751 }
752
753 pub fn set_iteration(&mut self, v: u64) -> &mut Self {
755 self.sub_command.iteration = Some(v);
756 self
757 }
758
759 pub fn iteration(mut self, v: u64) -> Self {
761 self.set_iteration(v);
762 self
763 }
764
765 pub fn get_comment(&self) -> &str {
767 &self.sub_command.comment
768 }
769
770 pub fn set_comment(&mut self, v: impl Into<String>) -> &mut Self {
772 self.sub_command.comment = v.into();
773 self
774 }
775
776 pub fn comment(mut self, v: impl Into<String>) -> Self {
778 self.set_comment(v);
779 self
780 }
781
782 pub fn get_variable(&self) -> &str {
784 &self.sub_command.variable
785 }
786
787 pub fn set_variable(&mut self, v: impl Into<String>) -> &mut Self {
789 self.sub_command.variable = v.into();
790 self
791 }
792
793 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#[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}