Skip to main content

perforce_cli/cmd/
delete.rs

1use std::{
2    ffi::OsStr,
3    path::{Path, PathBuf},
4    process::{Child, Stdio},
5};
6
7use super::SubCommand;
8
9use crate::global::GlobalOpts;
10use crate::spawn::ParameterizedSpawn;
11
12#[cfg_attr(
13    feature = "lt2016_1",
14    doc = "`p4 [g-opts] delete [-c changelist] [-n -k -v] file ...`"
15)]
16#[cfg_attr(
17    all(feature = "lt2024_1", not(feature = "lt2016_1")),
18    doc = "`p4 [g-opts] delete [-c changelist] [-n -k -v] [--remote=remote] file ...`"
19)]
20#[cfg_attr(
21    not(feature = "lt2024_1"),
22    doc = "`p4 [g-opts] delete [-c PendingChangelist] [-n -k -v] [--remote=remote] FileSpec1 [[FileSpec]2 ...]`"
23)]
24///
25#[cfg_attr(
26    feature = "lt2024_1",
27    doc = "Open file(s) in a client workspace for deletion from the depot."
28)]
29#[cfg_attr(
30    not(feature = "lt2024_1"),
31    doc = "Open files in a client workspace for deletion from the depot."
32)]
33#[derive(Debug, Clone, Default)]
34pub struct Delete {
35    bin: PathBuf,
36
37    global_opts: GlobalOpts,
38
39    changelist: Option<String>,
40
41    preview: bool,
42
43    keep_localfile: bool,
44
45    delete_unsynced: bool,
46
47    #[cfg(not(feature = "lt2016_1"))]
48    remote: Option<String>,
49}
50
51impl SubCommand for Delete {
52    fn name(&self) -> &str {
53        "delete"
54    }
55
56    fn inject_local_args(&self, command: &mut std::process::Command) {
57        if let Some(changelist) = self.changelist.as_ref() {
58            command.arg("-c").arg(changelist);
59        }
60        if self.preview {
61            command.arg("-n");
62        }
63        if self.keep_localfile {
64            command.arg("-k");
65        }
66        if self.delete_unsynced {
67            command.arg("-v");
68        }
69        #[cfg(not(feature = "lt2016_1"))]
70        if let Some(remote) = self.remote.as_ref() {
71            command.arg(format!("--remote={remote}"));
72        }
73    }
74
75    fn global_opts(&self) -> Option<&GlobalOpts> {
76        Some(&self.global_opts)
77    }
78}
79
80impl<S, I> ParameterizedSpawn<(S,)> for Delete
81where
82    S: IntoIterator<Item = I>,
83    I: AsRef<OsStr>,
84{
85    type Output = Child;
86    type Error = std::io::Error;
87
88    /// Spawns `p4 delete` for the given files as a child process.
89    ///
90    /// The child process inherits the standard input, output, and error
91    /// streams of the current process, and runs asynchronously; use the
92    /// returned [`Child`] handle to wait for it or interact with it.
93    fn spawn_with(&mut self, (input,): (S,)) -> Result<Self::Output, Self::Error> {
94        self.setup_command(&self.bin)
95            .args(input)
96            .stdout(Stdio::piped())
97            .stderr(Stdio::piped())
98            .spawn()
99    }
100}
101
102impl Delete {
103    /// Get the Perforce command path.
104    pub fn get_bin(&self) -> &Path {
105        &self.bin
106    }
107
108    /// Set the Perforce command path.
109    pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
110        self.bin = bin.into();
111        self
112    }
113
114    /// Set the Perforce command path.
115    pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
116        self.set_bin(bin);
117        self
118    }
119
120    #[cfg_attr(
121        feature = "lt2024_1",
122        doc = "Open file(s) in a client workspace for deletion from the depot."
123    )]
124    #[cfg_attr(
125        not(feature = "lt2024_1"),
126        doc = "Open files in a client workspace for deletion from the depot."
127    )]
128    ///
129    /// `bin` is the path to the Perforce command-line executable.
130    pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
131        Self {
132            bin: bin.into(),
133            global_opts,
134            ..Default::default()
135        }
136    }
137
138    /// # Description
139    ///
140    /// g-opts
141    ///
142    #[cfg_attr(
143        feature = "lt2014_2",
144        doc = "See the [Global Options](GlobalOpts) section."
145    )]
146    #[cfg_attr(
147        all(feature = "lt2015_1", not(feature = "lt2014_2")),
148        doc = "See the [“Global Options”](GlobalOpts) section."
149    )]
150    #[cfg_attr(
151        all(feature = "lt2017_1", not(feature = "lt2015_1")),
152        doc = "See [“Global Options”](GlobalOpts)."
153    )]
154    #[cfg_attr(
155        all(feature = "lt2018_2", not(feature = "lt2017_1")),
156        doc = "See [Global Options](GlobalOpts)."
157    )]
158    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
159    pub fn get_global_opts(&self) -> &GlobalOpts {
160        &self.global_opts
161    }
162
163    /// # Description
164    ///
165    /// g-opts
166    ///
167    #[cfg_attr(
168        feature = "lt2014_2",
169        doc = "See the [Global Options](GlobalOpts) section."
170    )]
171    #[cfg_attr(
172        all(feature = "lt2015_1", not(feature = "lt2014_2")),
173        doc = "See the [“Global Options”](GlobalOpts) section."
174    )]
175    #[cfg_attr(
176        all(feature = "lt2017_1", not(feature = "lt2015_1")),
177        doc = "See [“Global Options”](GlobalOpts)."
178    )]
179    #[cfg_attr(
180        all(feature = "lt2018_2", not(feature = "lt2017_1")),
181        doc = "See [Global Options](GlobalOpts)."
182    )]
183    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
184    pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
185        self.global_opts = v;
186        self
187    }
188
189    /// # Description
190    ///
191    /// g-opts
192    ///
193    #[cfg_attr(
194        feature = "lt2014_2",
195        doc = "See the [Global Options](GlobalOpts) section."
196    )]
197    #[cfg_attr(
198        all(feature = "lt2015_1", not(feature = "lt2014_2")),
199        doc = "See the [“Global Options”](GlobalOpts) section."
200    )]
201    #[cfg_attr(
202        all(feature = "lt2017_1", not(feature = "lt2015_1")),
203        doc = "See [“Global Options”](GlobalOpts)."
204    )]
205    #[cfg_attr(
206        all(feature = "lt2018_2", not(feature = "lt2017_1")),
207        doc = "See [Global Options](GlobalOpts)."
208    )]
209    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
210    pub fn global_opts(mut self, v: GlobalOpts) -> Self {
211        self.global_opts = v;
212        self
213    }
214
215    /// # Description
216    ///
217    #[cfg_attr(
218        feature = "lt2014_2",
219        doc = "-c changelist",
220        doc = "",
221        doc = "Opens the files for delete within the specified changelist.",
222        doc = "",
223        doc = "If this flag is not provided, the files are linked to the default changelist."
224    )]
225    #[cfg_attr(
226        all(feature = "lt2024_1", not(feature = "lt2014_2")),
227        doc = "-c changelist",
228        doc = "",
229        doc = "Opens the files for delete within the specified changelist.",
230        doc = "",
231        doc = "If this option is not provided, the files are linked to the default changelist."
232    )]
233    #[cfg_attr(
234        not(feature = "lt2024_1"),
235        doc = "-c PendingChangelist",
236        doc = "",
237        doc = "Opens files for `delete` and associates the deletion action with the specified pending changelist.",
238        doc = "",
239        doc = "If no pending changelist is specified, the deletion action is associated with the default changelist."
240    )]
241    pub fn get_changelist(&self) -> Option<&String> {
242        self.changelist.as_ref()
243    }
244
245    /// # Description
246    ///
247    #[cfg_attr(
248        feature = "lt2014_2",
249        doc = "-c changelist",
250        doc = "",
251        doc = "Opens the files for delete within the specified changelist.",
252        doc = "",
253        doc = "If this flag is not provided, the files are linked to the default changelist."
254    )]
255    #[cfg_attr(
256        all(feature = "lt2024_1", not(feature = "lt2014_2")),
257        doc = "-c changelist",
258        doc = "",
259        doc = "Opens the files for delete within the specified changelist.",
260        doc = "",
261        doc = "If this option is not provided, the files are linked to the default changelist."
262    )]
263    #[cfg_attr(
264        not(feature = "lt2024_1"),
265        doc = "-c PendingChangelist",
266        doc = "",
267        doc = "Opens files for `delete` and associates the deletion action with the specified pending changelist.",
268        doc = "",
269        doc = "If no pending changelist is specified, the deletion action is associated with the default changelist."
270    )]
271    pub fn set_changelist(&mut self, v: impl Into<String>) -> &mut Self {
272        self.changelist = Some(v.into());
273        self
274    }
275
276    /// # Description
277    ///
278    #[cfg_attr(
279        feature = "lt2014_2",
280        doc = "-c changelist",
281        doc = "",
282        doc = "Opens the files for delete within the specified changelist.",
283        doc = "",
284        doc = "If this flag is not provided, the files are linked to the default changelist."
285    )]
286    #[cfg_attr(
287        all(feature = "lt2024_1", not(feature = "lt2014_2")),
288        doc = "-c changelist",
289        doc = "",
290        doc = "Opens the files for delete within the specified changelist.",
291        doc = "",
292        doc = "If this option is not provided, the files are linked to the default changelist."
293    )]
294    #[cfg_attr(
295        not(feature = "lt2024_1"),
296        doc = "-c PendingChangelist",
297        doc = "",
298        doc = "Opens files for `delete` and associates the deletion action with the specified pending changelist.",
299        doc = "",
300        doc = "If no pending changelist is specified, the deletion action is associated with the default changelist."
301    )]
302    pub fn changelist(mut self, v: impl Into<String>) -> Self {
303        self.changelist = Some(v.into());
304        self
305    }
306
307    /// # Description
308    ///
309    /// -n
310    ///
311    /// Preview which files would be opened for delete, without actually
312    /// changing any files or metadata.
313    pub fn get_preview(&self) -> bool {
314        self.preview
315    }
316
317    /// # Description
318    ///
319    /// -n
320    ///
321    /// Preview which files would be opened for delete, without actually
322    /// changing any files or metadata.
323    pub fn set_preview(&mut self, v: bool) -> &mut Self {
324        self.preview = v;
325        self
326    }
327
328    /// # Description
329    ///
330    /// -n
331    ///
332    /// Preview which files would be opened for delete, without actually
333    /// changing any files or metadata.
334    pub fn preview(mut self, v: bool) -> Self {
335        self.preview = v;
336        self
337    }
338
339    /// # Description
340    ///
341    /// -k
342    ///
343    /// Delete the file on the shared versioning service, but keep a copy of
344    /// the deleted file in your workspace.
345    pub fn get_keep_localfile(&self) -> bool {
346        self.keep_localfile
347    }
348
349    /// # Description
350    ///
351    /// -k
352    ///
353    /// Delete the file on the shared versioning service, but keep a copy of
354    /// the deleted file in your workspace.
355    pub fn set_keep_localfile(&mut self, v: bool) -> &mut Self {
356        self.keep_localfile = v;
357        self
358    }
359
360    /// # Description
361    ///
362    /// -k
363    ///
364    /// Delete the file on the shared versioning service, but keep a copy of
365    /// the deleted file in your workspace.
366    pub fn keep_localfile(mut self, v: bool) -> Self {
367        self.keep_localfile = v;
368        self
369    }
370
371    /// # Description
372    ///
373    /// -v
374    ///
375    /// Delete a file that is not synced into the client workspace.
376    ///
377    #[cfg_attr(
378        feature = "lt2014_2",
379        doc = "To use this flag, specify these files in depot syntax; because such files are not synced, client syntax or local syntax can introduce ambiguities in the list of files to delete. (If the files are synced, `p4 delete -v file` removes the files from your workspace in addition to opening them for deletion.) To delete a set of files without transferring them to your workstation, use `p4 sync -k file`, followed by `p4 delete -k file`."
380    )]
381    #[cfg_attr(
382        all(feature = "lt2016_1", not(feature = "lt2014_2")),
383        doc = "To use this option, specify these files in depot syntax; because such files are not synced, client syntax or local syntax can introduce ambiguities in the list of files to delete. (If the files are synced, `p4 delete -v file` removes the files from your workspace in addition to opening them for deletion.) To delete a set of files without transferring them to your workstation, use `p4 sync -k file`, followed by `p4 delete -k file`."
384    )]
385    #[cfg_attr(
386        not(feature = "lt2016_1"),
387        doc = "To use this option, specify these files in depot syntax; because such files are not synced, client syntax or local syntax can introduce ambiguities in the list of files to delete. (If the files are synced, `p4 delete -v file` removes the files from your workspace in addition to opening them for deletion.)",
388        doc = "",
389        doc = "To delete a set of files without transferring them to your workstation when another version of these files already exists in your workspace, use `p4 sync -k file`, followed by `p4 delete -k file`. This allows you to delete a file that is in the depot without affecting the file (by the same name) in your workspace. For example, if you have `myfile` version 5 in the depot and `myfile` version 6 in your workspace, this sequence of commands, allows you to keep version 6 but delete version 5. If you don't do this and just do `p4 delete -v`, it will delete version 5 in the depot and version 6 in the workspace."
390    )]
391    pub fn get_delete_unsynced(&self) -> bool {
392        self.delete_unsynced
393    }
394
395    /// # Description
396    ///
397    /// -v
398    ///
399    /// Delete a file that is not synced into the client workspace.
400    ///
401    #[cfg_attr(
402        feature = "lt2014_2",
403        doc = "To use this flag, specify these files in depot syntax; because such files are not synced, client syntax or local syntax can introduce ambiguities in the list of files to delete. (If the files are synced, `p4 delete -v file` removes the files from your workspace in addition to opening them for deletion.) To delete a set of files without transferring them to your workstation, use `p4 sync -k file`, followed by `p4 delete -k file`."
404    )]
405    #[cfg_attr(
406        all(feature = "lt2016_1", not(feature = "lt2014_2")),
407        doc = "To use this option, specify these files in depot syntax; because such files are not synced, client syntax or local syntax can introduce ambiguities in the list of files to delete. (If the files are synced, `p4 delete -v file` removes the files from your workspace in addition to opening them for deletion.) To delete a set of files without transferring them to your workstation, use `p4 sync -k file`, followed by `p4 delete -k file`."
408    )]
409    #[cfg_attr(
410        not(feature = "lt2016_1"),
411        doc = "To use this option, specify these files in depot syntax; because such files are not synced, client syntax or local syntax can introduce ambiguities in the list of files to delete. (If the files are synced, `p4 delete -v file` removes the files from your workspace in addition to opening them for deletion.)",
412        doc = "",
413        doc = "To delete a set of files without transferring them to your workstation when another version of these files already exists in your workspace, use `p4 sync -k file`, followed by `p4 delete -k file`. This allows you to delete a file that is in the depot without affecting the file (by the same name) in your workspace. For example, if you have `myfile` version 5 in the depot and `myfile` version 6 in your workspace, this sequence of commands, allows you to keep version 6 but delete version 5. If you don't do this and just do `p4 delete -v`, it will delete version 5 in the depot and version 6 in the workspace."
414    )]
415    pub fn set_delete_unsynced(&mut self, v: bool) -> &mut Self {
416        self.delete_unsynced = v;
417        self
418    }
419
420    /// # Description
421    ///
422    /// -v
423    ///
424    /// Delete a file that is not synced into the client workspace.
425    ///
426    #[cfg_attr(
427        feature = "lt2014_2",
428        doc = "To use this flag, specify these files in depot syntax; because such files are not synced, client syntax or local syntax can introduce ambiguities in the list of files to delete. (If the files are synced, `p4 delete -v file` removes the files from your workspace in addition to opening them for deletion.) To delete a set of files without transferring them to your workstation, use `p4 sync -k file`, followed by `p4 delete -k file`."
429    )]
430    #[cfg_attr(
431        all(feature = "lt2016_1", not(feature = "lt2014_2")),
432        doc = "To use this option, specify these files in depot syntax; because such files are not synced, client syntax or local syntax can introduce ambiguities in the list of files to delete. (If the files are synced, `p4 delete -v file` removes the files from your workspace in addition to opening them for deletion.) To delete a set of files without transferring them to your workstation, use `p4 sync -k file`, followed by `p4 delete -k file`."
433    )]
434    #[cfg_attr(
435        not(feature = "lt2016_1"),
436        doc = "To use this option, specify these files in depot syntax; because such files are not synced, client syntax or local syntax can introduce ambiguities in the list of files to delete. (If the files are synced, `p4 delete -v file` removes the files from your workspace in addition to opening them for deletion.)",
437        doc = "",
438        doc = "To delete a set of files without transferring them to your workstation when another version of these files already exists in your workspace, use `p4 sync -k file`, followed by `p4 delete -k file`. This allows you to delete a file that is in the depot without affecting the file (by the same name) in your workspace. For example, if you have `myfile` version 5 in the depot and `myfile` version 6 in your workspace, this sequence of commands, allows you to keep version 6 but delete version 5. If you don't do this and just do `p4 delete -v`, it will delete version 5 in the depot and version 6 in the workspace."
439    )]
440    pub fn delete_unsynced(mut self, v: bool) -> Self {
441        self.delete_unsynced = v;
442        self
443    }
444
445    /// # Description
446    ///
447    /// `--remote=remote`
448    ///
449    /// Opens the file for delete in your personal server, and additionally —
450    /// if the file is of type `+l` — takes a global exclusive lock on the file
451    /// in the shared server from which you cloned the file.
452    #[cfg(not(feature = "lt2016_1"))]
453    pub fn get_remote(&self) -> Option<&String> {
454        self.remote.as_ref()
455    }
456
457    /// # Description
458    ///
459    /// `--remote=remote`
460    ///
461    /// Opens the file for delete in your personal server, and additionally —
462    /// if the file is of type `+l` — takes a global exclusive lock on the file
463    /// in the shared server from which you cloned the file.
464    #[cfg(not(feature = "lt2016_1"))]
465    pub fn set_remote(&mut self, v: impl Into<String>) -> &mut Self {
466        self.remote = Some(v.into());
467        self
468    }
469
470    /// # Description
471    ///
472    /// `--remote=remote`
473    ///
474    /// Opens the file for delete in your personal server, and additionally —
475    /// if the file is of type `+l` — takes a global exclusive lock on the file
476    /// in the shared server from which you cloned the file.
477    #[cfg(not(feature = "lt2016_1"))]
478    pub fn remote(mut self, v: impl Into<String>) -> Self {
479        self.remote = Some(v.into());
480        self
481    }
482}