Skip to main content

perforce_cli/cmd/
counters.rs

1use 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#[cfg_attr(
10    feature = "lt2015_1",
11    doc = "`p4 [g-opts] counters [-e nameFilter -m max]`"
12)]
13#[cfg_attr(
14    not(feature = "lt2015_1"),
15    doc = "`p4 [g-opts] counters [-e nameFilter] [-m max]`"
16)]
17///
18/// Display the list of long-term variables used by P4 Server and associated
19/// scripts.
20///
21/// The bare command reports all counters and their values; [`Self::filter`]
22/// restricts the report to counters whose names match a pattern, and
23/// [`Self::limit`] caps the number of counters listed.
24#[derive(Debug, Clone, Default)]
25pub struct Counters {
26    bin: PathBuf,
27
28    global_opts: GlobalOpts,
29
30    filter: Option<String>,
31
32    limit: Option<u64>,
33}
34
35impl Counters {
36    /// Creates a new `p4 counters` command.
37    ///
38    /// `bin` is the path to the Perforce command-line executable.
39    pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
40        Self {
41            bin: bin.into(),
42            global_opts,
43            filter: None,
44            limit: None,
45        }
46    }
47
48    /// Get the Perforce command path.
49    pub fn get_bin(&self) -> &Path {
50        &self.bin
51    }
52
53    /// Set the Perforce command path.
54    pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
55        self.bin = bin.into();
56        self
57    }
58
59    /// Set the Perforce command path.
60    pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
61        self.set_bin(bin);
62        self
63    }
64
65    /// # Description
66    ///
67    /// g-opts
68    ///
69    #[cfg_attr(
70        feature = "lt2014_2",
71        doc = "See the [Global Options](GlobalOpts) section."
72    )]
73    #[cfg_attr(
74        all(feature = "lt2015_1", not(feature = "lt2014_2")),
75        doc = "See the [“Global Options”](GlobalOpts) section."
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 get_global_opts(&self) -> &GlobalOpts {
87        &self.global_opts
88    }
89
90    /// # Description
91    ///
92    /// g-opts
93    ///
94    #[cfg_attr(
95        feature = "lt2014_2",
96        doc = "See the [Global Options](GlobalOpts) section."
97    )]
98    #[cfg_attr(
99        all(feature = "lt2015_1", not(feature = "lt2014_2")),
100        doc = "See the [“Global Options”](GlobalOpts) section."
101    )]
102    #[cfg_attr(
103        all(feature = "lt2017_1", not(feature = "lt2015_1")),
104        doc = "See [“Global Options”](GlobalOpts)."
105    )]
106    #[cfg_attr(
107        all(feature = "lt2018_2", not(feature = "lt2017_1")),
108        doc = "See [Global Options](GlobalOpts)."
109    )]
110    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
111    pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
112        self.global_opts = v;
113        self
114    }
115
116    /// # Description
117    ///
118    /// g-opts
119    ///
120    #[cfg_attr(
121        feature = "lt2014_2",
122        doc = "See the [Global Options](GlobalOpts) section."
123    )]
124    #[cfg_attr(
125        all(feature = "lt2015_1", not(feature = "lt2014_2")),
126        doc = "See the [“Global Options”](GlobalOpts) section."
127    )]
128    #[cfg_attr(
129        all(feature = "lt2017_1", not(feature = "lt2015_1")),
130        doc = "See [“Global Options”](GlobalOpts)."
131    )]
132    #[cfg_attr(
133        all(feature = "lt2018_2", not(feature = "lt2017_1")),
134        doc = "See [Global Options](GlobalOpts)."
135    )]
136    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
137    pub fn global_opts(mut self, v: GlobalOpts) -> Self {
138        self.global_opts = v;
139        self
140    }
141
142    /// # Description
143    ///
144    /// -e nameFilter
145    ///
146    /// List counters with a name that matches the `nameFilter` pattern, for
147    /// example: `p4 counters -e 'mycounter-*'`
148    pub fn get_filter(&self) -> Option<&str> {
149        self.filter.as_deref()
150    }
151
152    /// # Description
153    ///
154    /// -e nameFilter
155    ///
156    /// List counters with a name that matches the `nameFilter` pattern, for
157    /// example: `p4 counters -e 'mycounter-*'`
158    pub fn set_filter(&mut self, v: impl Into<String>) -> &mut Self {
159        self.filter = Some(v.into());
160        self
161    }
162
163    /// # Description
164    ///
165    /// -e nameFilter
166    ///
167    /// List counters with a name that matches the `nameFilter` pattern, for
168    /// example: `p4 counters -e 'mycounter-*'`
169    pub fn filter(mut self, v: impl Into<String>) -> Self {
170        self.set_filter(v);
171        self
172    }
173
174    /// # Description
175    ///
176    /// -m max
177    ///
178    /// List only the first `max` counters.
179    pub fn get_limit(&self) -> Option<u64> {
180        self.limit
181    }
182
183    /// # Description
184    ///
185    /// -m max
186    ///
187    /// List only the first `max` counters.
188    pub fn set_limit(&mut self, v: u64) -> &mut Self {
189        self.limit = Some(v);
190        self
191    }
192
193    /// # Description
194    ///
195    /// -m max
196    ///
197    /// List only the first `max` counters.
198    pub fn limit(mut self, v: u64) -> Self {
199        self.set_limit(v);
200        self
201    }
202}
203
204impl SubCommand for Counters {
205    fn name(&self) -> &str {
206        "counters"
207    }
208
209    fn inject_local_args(&self, command: &mut Command) {
210        if let Some(filter) = &self.filter {
211            command.arg("-e").arg(filter);
212        }
213
214        if let Some(max) = self.limit {
215            command.arg("-m").arg(max.to_string());
216        }
217    }
218
219    fn global_opts(&self) -> Option<&GlobalOpts> {
220        Some(&self.global_opts)
221    }
222}
223
224impl ParameterizedSpawn<()> for Counters {
225    type Output = Child;
226    type Error = std::io::Error;
227
228    /// Spawns `p4 counters` as a child process with piped standard output and
229    /// error streams; use the returned [`Child`] handle to wait for it or
230    /// interact with it.
231    fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
232        self.setup_command(&self.bin)
233            .stdout(Stdio::piped())
234            .stderr(Stdio::piped())
235            .spawn()
236    }
237}
238
239#[cfg(test)]
240mod tests {
241    use super::*;
242    use crate::cmd::args_of;
243
244    #[test]
245    fn bin_accessors_update_executable_path() {
246        let mut command = Counters::new("p4", GlobalOpts::new());
247        assert_eq!(command.get_bin(), Path::new("p4"));
248
249        command.set_bin("/usr/local/bin/p4");
250        assert_eq!(command.get_bin(), Path::new("/usr/local/bin/p4"));
251
252        let command = command.bin("p4.exe");
253        assert_eq!(command.get_bin(), Path::new("p4.exe"));
254    }
255
256    /// Dry-run checks of the assembled `p4 counters` command line; no process
257    /// is spawned.
258    #[test]
259    fn without_options() {
260        let counters = Counters::new("p4", GlobalOpts::new());
261
262        assert_eq!(args_of(&counters.setup_command("p4")), ["counters"]);
263    }
264
265    #[test]
266    fn with_filter() {
267        let counters = Counters::new("p4", GlobalOpts::new()).filter("mycounter-*");
268
269        assert_eq!(counters.get_filter(), Some("mycounter-*"));
270        assert_eq!(
271            args_of(&counters.setup_command("p4")),
272            ["counters", "-e", "mycounter-*"]
273        );
274    }
275
276    #[test]
277    fn with_limit() {
278        let counters = Counters::new("p4", GlobalOpts::new()).limit(5);
279
280        assert_eq!(counters.get_limit(), Some(5));
281        assert_eq!(
282            args_of(&counters.setup_command("p4")),
283            ["counters", "-m", "5"]
284        );
285    }
286
287    #[test]
288    fn filter_and_limit_in_syntax_order() {
289        let mut counters = Counters::new("p4", GlobalOpts::new()).limit(5);
290        counters.set_filter("mycounter-*");
291
292        assert_eq!(
293            args_of(&counters.setup_command("p4")),
294            ["counters", "-e", "mycounter-*", "-m", "5"]
295        );
296    }
297
298    #[test]
299    fn global_opts_are_injected_once() {
300        let counters = Counters::new(
301            "p4",
302            GlobalOpts::new().port("localhost:1666").quiet_mode(true),
303        )
304        .filter("mycounter-*");
305
306        assert_eq!(
307            args_of(&counters.setup_command("p4")),
308            [
309                "-p",
310                "localhost:1666",
311                "-q",
312                "counters",
313                "-e",
314                "mycounter-*"
315            ]
316        );
317    }
318}