Skip to main content

perforce_cli/cmd/
aliases.rs

1use std::path::PathBuf;
2use std::process::{Child, Command, Stdio};
3
4use super::SubCommand;
5
6use crate::global::GlobalOpts;
7use crate::spawn::ParameterizedSpawn;
8
9/// `p4 [g-opts] aliases`: display command aliases that are currently defined
10/// in a `.p4aliases` file.
11#[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    /// Spawns `p4 aliases` as a child process with piped standard output and
35    /// error streams; use the returned [`Child`] handle to wait for it or
36    /// interact with it.
37    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    /// Creates a new `p4 aliases` command.
47    ///
48    /// `bin` is the path to the Perforce command-line executable.
49    pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
50        Self {
51            bin: bin.into(),
52            global_opts,
53        }
54    }
55
56    /// # Description
57    ///
58    /// g-opts
59    ///
60    #[cfg_attr(
61        all(feature = "lt2017_1", not(feature = "lt2015_1")),
62        doc = "See [“Global Options”](GlobalOpts)."
63    )]
64    #[cfg_attr(
65        all(feature = "lt2018_2", not(feature = "lt2017_1")),
66        doc = "See [Global Options](GlobalOpts)."
67    )]
68    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
69    pub fn get_global_opts(&self) -> &GlobalOpts {
70        &self.global_opts
71    }
72
73    /// # Description
74    ///
75    /// g-opts
76    ///
77    #[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 set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
87        self.global_opts = v;
88        self
89    }
90
91    /// # Description
92    ///
93    /// g-opts
94    ///
95    #[cfg_attr(
96        all(feature = "lt2017_1", not(feature = "lt2015_1")),
97        doc = "See [“Global Options”](GlobalOpts)."
98    )]
99    #[cfg_attr(
100        all(feature = "lt2018_2", not(feature = "lt2017_1")),
101        doc = "See [Global Options](GlobalOpts)."
102    )]
103    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
104    pub fn global_opts(mut self, v: GlobalOpts) -> Self {
105        self.global_opts = v;
106        self
107    }
108}
109
110#[cfg(test)]
111mod tests {
112    use super::*;
113    use crate::cmd::args_of;
114
115    /// Dry-run check of the assembled `p4 aliases` command line; no process is
116    /// spawned.
117    #[test]
118    fn without_options() {
119        let aliases = Aliases::new("p4", GlobalOpts::new());
120
121        assert_eq!(args_of(&aliases.setup_command("p4")), ["aliases"]);
122    }
123
124    #[test]
125    fn with_global_opts() {
126        let aliases = Aliases::new(
127            "p4",
128            GlobalOpts::new().port("localhost:1666").quiet_mode(true),
129        );
130
131        assert_eq!(
132            args_of(&aliases.setup_command("p4")),
133            ["-p", "localhost:1666", "-q", "aliases"]
134        );
135    }
136}