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 Input<'a> = ();
32    type Output<'a> = Child;
33    type Error = std::io::Error;
34
35    /// Spawns `p4 aliases` as a child process with piped standard output and
36    /// error streams; use the returned [`Child`] handle to wait for it or
37    /// interact with it.
38    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    /// Creates a new `p4 aliases` command.
48    ///
49    /// `bin` is the path to the Perforce command-line executable.
50    pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
51        Self {
52            bin: bin.into(),
53            global_opts,
54        }
55    }
56
57    /// # Description
58    ///
59    /// g-opts
60    ///
61    #[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    /// # Description
75    ///
76    /// g-opts
77    ///
78    #[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    /// # Description
93    ///
94    /// g-opts
95    ///
96    #[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    /// Dry-run check of the assembled `p4 aliases` command line; no process is
117    /// spawned.
118    #[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}