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 #[cfg(not(feature = "lt2023_2"))]
119 comment: None,
120 },
121 }
122 }
123
124 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 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 #[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 #[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 #[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
201impl<T: SubCommand> Configure<T> {
206 pub fn get_bin(&self) -> &Path {
208 &self.bin
209 }
210
211 pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
213 self.bin = bin.into();
214 self
215 }
216
217 pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
219 self.set_bin(bin);
220 self
221 }
222
223 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#[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 server_id: Option<String>,
278
279 variable: String,
281
282 value: String,
284
285 #[cfg(not(feature = "lt2023_2"))]
287 comment: Option<String>,
288}
289
290impl Configure<SetVariable> {
291 pub fn get_server_id(&self) -> Option<&str> {
293 self.sub_command.server_id.as_deref()
294 }
295
296 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 pub fn server_id(mut self, v: impl Into<String>) -> Self {
304 self.set_server_id(v);
305 self
306 }
307
308 pub fn get_variable(&self) -> &str {
310 &self.sub_command.variable
311 }
312
313 pub fn set_variable(&mut self, v: impl Into<String>) -> &mut Self {
315 self.sub_command.variable = v.into();
316 self
317 }
318
319 pub fn variable(mut self, v: impl Into<String>) -> Self {
321 self.set_variable(v);
322 self
323 }
324
325 pub fn get_value(&self) -> &str {
327 &self.sub_command.value
328 }
329
330 pub fn set_value(&mut self, v: impl Into<String>) -> &mut Self {
332 self.sub_command.value = v.into();
333 self
334 }
335
336 pub fn value(mut self, v: impl Into<String>) -> Self {
338 self.set_value(v);
339 self
340 }
341
342 #[cfg(not(feature = "lt2023_2"))]
344 pub fn get_comment(&self) -> Option<&str> {
345 self.sub_command.comment.as_deref()
346 }
347
348 #[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 #[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#[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 server_id: Option<String>,
423
424 variable: String,
426
427 #[cfg(not(feature = "lt2023_2"))]
429 comment: Option<String>,
430}
431
432impl Configure<UnSetVariable> {
433 pub fn get_server_id(&self) -> Option<&str> {
435 self.sub_command.server_id.as_deref()
436 }
437
438 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 pub fn server_id(mut self, v: impl Into<String>) -> Self {
446 self.set_server_id(v);
447 self
448 }
449
450 pub fn get_variable(&self) -> &str {
452 &self.sub_command.variable
453 }
454
455 pub fn set_variable(&mut self, v: impl Into<String>) -> &mut Self {
457 self.sub_command.variable = v.into();
458 self
459 }
460
461 pub fn variable(mut self, v: impl Into<String>) -> Self {
463 self.set_variable(v);
464 self
465 }
466
467 #[cfg(not(feature = "lt2023_2"))]
469 pub fn get_comment(&self) -> Option<&str> {
470 self.sub_command.comment.as_deref()
471 }
472
473 #[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 #[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#[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 target: Option<Target>,
548}
549
550impl Configure<Show> {
551 pub fn get_target(&self) -> Option<&Target> {
553 self.sub_command.target.as_ref()
554 }
555
556 pub fn set_target(&mut self, target: Option<Target>) -> &mut Self {
558 self.sub_command.target = target;
559 self
560 }
561
562 pub fn target(mut self, target: Option<Target>) -> Self {
564 self.set_target(target);
565 self
566 }
567
568 pub fn allservers(mut self) -> Self {
570 self.sub_command.target = Some(Target::AllServers);
571 self
572 }
573
574 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 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#[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 target: Option<Target>,
648}
649
650#[cfg(not(feature = "lt2019_2"))]
651impl Configure<History> {
652 pub fn get_target(&self) -> Option<&Target> {
654 self.sub_command.target.as_ref()
655 }
656
657 pub fn set_target(&mut self, target: Option<Target>) -> &mut Self {
659 self.sub_command.target = target;
660 self
661 }
662
663 pub fn target(mut self, target: Option<Target>) -> Self {
665 self.set_target(target);
666 self
667 }
668
669 pub fn allservers(mut self) -> Self {
671 self.sub_command.target = Some(Target::AllServers);
672 self
673 }
674
675 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 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#[cfg(not(feature = "lt2023_2"))]
736#[derive(Debug, Clone, Default)]
737pub struct Patch {
738 iteration: Option<u64>,
740
741 comment: String,
743
744 variable: String,
746}
747
748#[cfg(not(feature = "lt2023_2"))]
749impl Configure<Patch> {
750 pub fn get_iteration(&self) -> Option<u64> {
752 self.sub_command.iteration
753 }
754
755 pub fn set_iteration(&mut self, v: u64) -> &mut Self {
757 self.sub_command.iteration = Some(v);
758 self
759 }
760
761 pub fn iteration(mut self, v: u64) -> Self {
763 self.set_iteration(v);
764 self
765 }
766
767 pub fn get_comment(&self) -> &str {
769 &self.sub_command.comment
770 }
771
772 pub fn set_comment(&mut self, v: impl Into<String>) -> &mut Self {
774 self.sub_command.comment = v.into();
775 self
776 }
777
778 pub fn comment(mut self, v: impl Into<String>) -> Self {
780 self.set_comment(v);
781 self
782 }
783
784 pub fn get_variable(&self) -> &str {
786 &self.sub_command.variable
787 }
788
789 pub fn set_variable(&mut self, v: impl Into<String>) -> &mut Self {
791 self.sub_command.variable = v.into();
792 self
793 }
794
795 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#[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}