perforce_cli/cmd/
aliases.rs1use std::path::{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 Output = Child;
32 type Error = std::io::Error;
33
34 fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
38 self.setup_command(&self.bin)
39 .stdout(Stdio::piped())
40 .stderr(Stdio::piped())
41 .spawn()
42 }
43}
44
45impl Aliases {
46 pub fn get_bin(&self) -> &Path {
48 &self.bin
49 }
50
51 pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
53 self.bin = bin.into();
54 self
55 }
56
57 pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
59 self.set_bin(bin);
60 self
61 }
62
63 pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
67 Self {
68 bin: bin.into(),
69 global_opts,
70 }
71 }
72
73 #[cfg_attr(
78 all(feature = "lt2017_1", not(feature = "lt2015_1")),
79 doc = "See [“Global Options”](GlobalOpts)."
80 )]
81 #[cfg_attr(
82 all(feature = "lt2018_2", not(feature = "lt2017_1")),
83 doc = "See [Global Options](GlobalOpts)."
84 )]
85 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
86 pub fn get_global_opts(&self) -> &GlobalOpts {
87 &self.global_opts
88 }
89
90 #[cfg_attr(
95 all(feature = "lt2017_1", not(feature = "lt2015_1")),
96 doc = "See [“Global Options”](GlobalOpts)."
97 )]
98 #[cfg_attr(
99 all(feature = "lt2018_2", not(feature = "lt2017_1")),
100 doc = "See [Global Options](GlobalOpts)."
101 )]
102 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
103 pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
104 self.global_opts = v;
105 self
106 }
107
108 #[cfg_attr(
113 all(feature = "lt2017_1", not(feature = "lt2015_1")),
114 doc = "See [“Global Options”](GlobalOpts)."
115 )]
116 #[cfg_attr(
117 all(feature = "lt2018_2", not(feature = "lt2017_1")),
118 doc = "See [Global Options](GlobalOpts)."
119 )]
120 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
121 pub fn global_opts(mut self, v: GlobalOpts) -> Self {
122 self.global_opts = v;
123 self
124 }
125}
126
127#[cfg(test)]
128mod tests {
129 use super::*;
130 use crate::cmd::args_of;
131
132 #[test]
133 fn bin_accessors_update_executable_path() {
134 let mut command = Aliases::new("p4", GlobalOpts::new());
135 assert_eq!(command.get_bin(), Path::new("p4"));
136
137 command.set_bin("/usr/local/bin/p4");
138 assert_eq!(command.get_bin(), Path::new("/usr/local/bin/p4"));
139
140 let command = command.bin("p4.exe");
141 assert_eq!(command.get_bin(), Path::new("p4.exe"));
142 }
143
144 #[test]
147 fn without_options() {
148 let aliases = Aliases::new("p4", GlobalOpts::new());
149
150 assert_eq!(args_of(&aliases.setup_command("p4")), ["aliases"]);
151 }
152
153 #[test]
154 fn with_global_opts() {
155 let aliases = Aliases::new(
156 "p4",
157 GlobalOpts::new().port("localhost:1666").quiet_mode(true),
158 );
159
160 assert_eq!(
161 args_of(&aliases.setup_command("p4")),
162 ["-p", "localhost:1666", "-q", "aliases"]
163 );
164 }
165}