perforce_cli/cmd/
aliases.rs1use std::path::PathBuf;
2use std::process::{Child, Command, Stdio};
3
4use super::SubCommand;
5
6use crate::global::GlobalOpts;
7use crate::spawn::ParameterizedSpawn;
8
9#[derive(Debug, Clone, Default)]
12pub struct Aliases {
13 bin: PathBuf,
14
15 global_opts: GlobalOpts,
16}
17
18impl SubCommand for Aliases {
19 fn name(&self) -> &str {
20 "aliases"
21 }
22
23 fn inject_local_args(&self, _: &mut Command) {}
24
25 fn global_opts(&self) -> Option<&GlobalOpts> {
26 Some(&self.global_opts)
27 }
28}
29
30impl ParameterizedSpawn for Aliases {
31 type Input<'a> = ();
32 type Output<'a> = Child;
33 type Error = std::io::Error;
34
35 fn spawn_with<'a>(&mut self, (): Self::Input<'a>) -> Result<Self::Output<'a>, Self::Error> {
39 self.setup_command(&self.bin)
40 .stdout(Stdio::piped())
41 .stderr(Stdio::piped())
42 .spawn()
43 }
44}
45
46impl Aliases {
47 pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
51 Self {
52 bin: bin.into(),
53 global_opts,
54 }
55 }
56
57 #[cfg_attr(
62 all(feature = "lt2017_1", not(feature = "lt2015_1")),
63 doc = "See [“Global Options”](GlobalOpts)."
64 )]
65 #[cfg_attr(
66 all(feature = "lt2018_2", not(feature = "lt2017_1")),
67 doc = "See [Global Options](GlobalOpts)."
68 )]
69 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
70 pub fn get_global_opts(&self) -> &GlobalOpts {
71 &self.global_opts
72 }
73
74 #[cfg_attr(
79 all(feature = "lt2017_1", not(feature = "lt2015_1")),
80 doc = "See [“Global Options”](GlobalOpts)."
81 )]
82 #[cfg_attr(
83 all(feature = "lt2018_2", not(feature = "lt2017_1")),
84 doc = "See [Global Options](GlobalOpts)."
85 )]
86 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
87 pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
88 self.global_opts = v;
89 self
90 }
91
92 #[cfg_attr(
97 all(feature = "lt2017_1", not(feature = "lt2015_1")),
98 doc = "See [“Global Options”](GlobalOpts)."
99 )]
100 #[cfg_attr(
101 all(feature = "lt2018_2", not(feature = "lt2017_1")),
102 doc = "See [Global Options](GlobalOpts)."
103 )]
104 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
105 pub fn global_opts(mut self, v: GlobalOpts) -> Self {
106 self.global_opts = v;
107 self
108 }
109}
110
111#[cfg(test)]
112mod tests {
113 use super::*;
114 use crate::cmd::args_of;
115
116 #[test]
119 fn without_options() {
120 let aliases = Aliases::new("p4", GlobalOpts::new());
121
122 assert_eq!(args_of(&aliases.setup_command("p4")), ["aliases"]);
123 }
124
125 #[test]
126 fn with_global_opts() {
127 let aliases = Aliases::new(
128 "p4",
129 GlobalOpts::new().port("localhost:1666").quiet_mode(true),
130 );
131
132 assert_eq!(
133 args_of(&aliases.setup_command("p4")),
134 ["-p", "localhost:1666", "-q", "aliases"]
135 );
136 }
137}