Skip to main content

perforce_cli/
lib.rs

1use std::path::{Path, PathBuf};
2
3use crate::cmd::Add;
4use crate::cmd::AdminEntry;
5#[cfg(not(feature = "lt2016_1"))]
6use crate::cmd::Aliases;
7use crate::cmd::Annotate;
8use crate::cmd::Archive;
9use crate::cmd::Attribute;
10#[cfg(not(feature = "lt2017_2"))]
11use crate::cmd::BackGroundTask;
12use crate::cmd::Branch;
13use crate::cmd::Branches;
14use crate::cmd::CachePurge;
15use crate::cmd::Change;
16use crate::cmd::Changes;
17use crate::cmd::Clean;
18use crate::cmd::Client;
19use crate::cmd::Clients;
20#[cfg(not(feature = "lt2015_1"))]
21use crate::cmd::Clone;
22use crate::cmd::Configure;
23use crate::cmd::Copy;
24use crate::cmd::Describe;
25use crate::cmd::Diff;
26use crate::cmd::Diff2;
27use crate::cmd::Edit;
28use crate::cmd::FileLog;
29use crate::cmd::Print;
30use crate::cmd::Sync;
31use crate::cmd::Where;
32use crate::global::GlobalOpts;
33
34pub mod cmd;
35pub mod global;
36pub mod spawn;
37
38pub mod prelude {
39    pub use crate::cmd::*;
40    pub use crate::global::*;
41    pub use crate::spawn::*;
42}
43
44#[derive(Debug, Clone)]
45pub struct P4Cli {
46    bin: PathBuf,
47
48    global_opts: GlobalOpts,
49}
50
51impl Default for P4Cli {
52    fn default() -> Self {
53        Self {
54            bin: PathBuf::from("p4"),
55            global_opts: GlobalOpts::default(),
56        }
57    }
58}
59
60impl P4Cli {
61    /// Create a new Perforce command-line client.
62    ///
63    /// `bin` is the path to the Perforce command-line executable.
64    pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
65        Self {
66            bin: bin.into(),
67            global_opts,
68        }
69    }
70
71    /// Set the Perforce command path.
72    pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
73        self.set_bin(bin);
74        self
75    }
76
77    /// Set the global options.
78    pub fn global_opts(mut self, global_opts: GlobalOpts) -> Self {
79        self.set_global_opts(global_opts);
80        self
81    }
82
83    /// Get the Perforce command path.
84    pub fn get_bin(&self) -> &Path {
85        &self.bin
86    }
87
88    /// Get the global options.
89    pub fn get_global_opts(&self) -> &GlobalOpts {
90        &self.global_opts
91    }
92
93    /// Set the Perforce command path.
94    pub fn set_bin(&mut self, bin: impl Into<PathBuf>) {
95        self.bin = bin.into();
96    }
97
98    /// Set the global options.
99    pub fn set_global_opts(&mut self, global_opts: GlobalOpts) {
100        self.global_opts = global_opts;
101    }
102
103    /// Open files in a client workspace for addition to the depot.
104    pub fn cmd_add(&self) -> Add {
105        Add::new(self.bin.clone(), self.global_opts.clone())
106    }
107
108    /// Perform administrative operations on the server.
109    pub fn cmd_admin(&self) -> AdminEntry {
110        AdminEntry::new(self.bin.clone(), self.global_opts.clone())
111    }
112
113    /// Display command aliases that are currently defined in a .p4aliases file.
114    #[cfg(not(feature = "lt2016_1"))]
115    pub fn cmd_aliases(&self) -> Aliases {
116        Aliases::new(self.bin.clone(), self.global_opts.clone())
117    }
118
119    /// Print file lines along with their revisions.
120    pub fn cmd_annotate(&self) -> Annotate {
121        Annotate::new(self.bin.clone(), self.global_opts.clone())
122    }
123
124    /// Archive obsolete revisions to an archive depot.
125    pub fn cmd_archive(&self) -> Archive {
126        Archive::new(self.bin.clone(), self.global_opts.clone())
127    }
128
129    /// Set per-revision attributes on file revisions.
130    pub fn cmd_attribute(&self) -> Attribute {
131        Attribute::new(self.bin.clone(), self.global_opts.clone())
132    }
133
134    /// Run a background command or trigger on the server.
135    ///
136    /// The returned builder starts with no task target; call
137    /// [`BackGroundTask::execute`] (`-e`) or [`BackGroundTask::trigger`]
138    /// (`-t`) to select the required target before spawning.
139    #[cfg(not(feature = "lt2017_2"))]
140    pub fn cmd_bgtask(&self) -> BackGroundTask {
141        BackGroundTask::new(self.bin.clone(), self.global_opts.clone())
142    }
143
144    /// Create or edit a branch mapping and its view.
145    pub fn cmd_branch(&self) -> Branch {
146        Branch::new(self.bin.clone(), self.global_opts.clone())
147    }
148
149    /// List existing branch mappings.
150    pub fn cmd_branches(&self) -> Branches {
151        Branches::new(self.bin.clone(), self.global_opts.clone())
152    }
153
154    /// Reclaim disk space on a replicated server.
155    ///
156    /// The returned builder starts with no purge goal and has no effect;
157    /// call [`CachePurge::purge_all`] (`-a`), [`CachePurge::free_space`]
158    /// (`-f`), [`CachePurge::revision_count`] (`-m`), or
159    /// [`CachePurge::size`] (`-s`) to select the required goal before
160    /// spawning.
161    pub fn cmd_cachepurge(&self) -> CachePurge {
162        CachePurge::new(self.bin.clone(), self.global_opts.clone())
163    }
164
165    /// Restore client workspace files to match the state of corresponding
166    /// depot files.
167    ///
168    /// Without file arguments, the files in your local working directory are
169    /// used; pass file arguments to
170    /// [`ParameterizedSpawn::spawn_with`](crate::spawn::ParameterizedSpawn::spawn_with)
171    /// to limit the scope.
172    pub fn cmd_clean(&self) -> Clean {
173        Clean::new(self.bin.clone(), self.global_opts.clone())
174    }
175
176    /// Create or edit a client workspace specification and its view.
177    ///
178    /// The returned builder starts in the [`GeneralMode`](crate::cmd::client::GeneralMode)
179    /// state (`p4 client [-f] [-t template] [-T type] [name]`); call
180    /// transition methods such as [`Client::delete`](crate::cmd::client::Client::delete)
181    /// or [`Client::stdout`](crate::cmd::client::Client::stdout) to select
182    /// another syntax form.
183    pub fn cmd_client(&self) -> Client {
184        Client::new(self.bin.clone(), self.global_opts.clone())
185    }
186
187    /// List all client workspaces currently known to the system.
188    ///
189    /// The returned builder starts with no option selected and spawns the
190    /// bare `p4 clients` report as is; set any regular option or filter to
191    /// enter the [`RegularMode`](crate::cmd::clients::RegularMode) state, or
192    /// call [`Clients::unloaded`](crate::cmd::clients::Clients::unloaded)
193    /// (`-U`) to list only unloaded client workspaces.
194    pub fn cmd_clients(&self) -> Clients {
195        Clients::new(self.bin.clone(), self.global_opts.clone())
196    }
197
198    /// List all client workspaces currently known to the system.
199    ///
200    /// This is an alias for [`Self::cmd_clients`], corresponding to the `p4
201    /// workspaces` command.
202    pub fn cmd_workspaces(&self) -> Clients {
203        Clients::new(self.bin.clone(), self.global_opts.clone())
204    }
205
206    /// Clone a new local server from a remote server.
207    ///
208    /// The returned builder cannot be assembled until a remote port is
209    /// selected with [`Clone::port`](crate::cmd::clone::Clone::port) (`-p`)
210    /// and exactly one clone target is selected with
211    /// [`Clone::remote`](crate::cmd::clone::Clone::remote) (`-r`)
212    /// or [`Clone::file`](crate::cmd::clone::Clone::file) (`-f`).
213    /// Unlike most commands, `p4 clone` does not accept the global options.
214    #[cfg(not(feature = "lt2015_1"))]
215    pub fn cmd_clone(&self) -> Clone {
216        Clone::new(self.bin.clone())
217    }
218
219    /// Set, view, and manage server configuration variables.
220    ///
221    /// The returned command starts with no operation selected; call
222    /// [`Configure::set`], [`Configure::unset`], or
223    /// [`Configure::show`] to select the operation.
224    #[cfg_attr(not(feature = "lt2019_2"), doc = "")]
225    #[cfg_attr(
226        not(feature = "lt2019_2"),
227        doc = "The [`Configure::history`](Configure::history) operation selects the `p4 configure history` report."
228    )]
229    #[cfg_attr(not(feature = "lt2023_2"), doc = "")]
230    #[cfg_attr(
231        not(feature = "lt2023_2"),
232        doc = "The [`Configure::patch`](Configure::patch) operation retroactively sets a history comment, and [`Configure::help`](Configure::help) prints the configurables reference."
233    )]
234    pub fn cmd_configure(&self) -> Configure {
235        Configure::new(self.bin.clone(), self.global_opts.clone())
236    }
237
238    /// Copy files and/or the stream spec from one location in the depot to
239    /// another.
240    pub fn cmd_copy(&self) -> Copy {
241        Copy::new(self.bin.clone(), self.global_opts.clone())
242    }
243
244    /// Create or edit a client workspace specification and its view.
245    ///
246    /// This is an alias for [`Self::cmd_client`], corresponding to the `p4
247    /// workspace` command.
248    pub fn cmd_workspace(&self) -> Client {
249        Client::new(self.bin.clone(), self.global_opts.clone())
250    }
251
252    /// Create or edit a changelist specification.
253    pub fn cmd_change(&self) -> Change {
254        Change::new(self.bin.clone(), self.global_opts.clone())
255    }
256
257    /// Create or edit a changelist specification.
258    ///
259    /// This is an alias for [`Self::cmd_change`], corresponding to the `p4
260    /// changelist` command.
261    pub fn cmd_changelist(&self) -> Change {
262        Change::new(self.bin.clone(), self.global_opts.clone())
263    }
264
265    /// List submitted and pending changelists.
266    pub fn cmd_changes(&self) -> Changes {
267        Changes::new(self.bin.clone(), self.global_opts.clone())
268    }
269
270    /// List submitted and pending changelists.
271    ///
272    /// This is an alias for [`Self::cmd_changes`], corresponding to the `p4
273    /// changelists` command.
274    pub fn cmd_changelists(&self) -> Changes {
275        Changes::new(self.bin.clone(), self.global_opts.clone())
276    }
277
278    /// Display the details of one or more changelists.
279    pub fn cmd_describe(&self) -> Describe {
280        Describe::new(self.bin.clone(), self.global_opts.clone())
281    }
282
283    /// Diff workspace content against depot content, or diff stream specs.
284    pub fn cmd_diff(&self) -> Diff {
285        Diff::new(self.bin.clone(), self.global_opts.clone())
286    }
287
288    /// Compare the content at two depot paths, or compare two stream specs.
289    pub fn cmd_diff2(&self) -> Diff2 {
290        Diff2::new(self.bin.clone(), self.global_opts.clone())
291    }
292
293    /// Open files in a client workspace for edit.
294    pub fn cmd_edit(&self) -> Edit {
295        Edit::new(self.bin.clone(), self.global_opts.clone())
296    }
297
298    /// Print detailed information about the revisions of files.
299    pub fn cmd_filelog(&self) -> FileLog {
300        FileLog::new(self.bin.clone(), self.global_opts.clone())
301    }
302
303    /// Print the contents of depot file revisions.
304    pub fn cmd_print(&self) -> Print {
305        Print::new(self.bin.clone(), self.global_opts.clone())
306    }
307
308    /// Update the client workspace to reflect the contents of the depot.
309    pub fn cmd_sync(&self) -> Sync {
310        Sync::new(self.bin.clone(), self.global_opts.clone())
311    }
312
313    /// Show where a particular file is located, as determined by the client view.
314    pub fn cmd_where(&self) -> Where {
315        Where::new(self.bin.clone(), self.global_opts.clone())
316    }
317}
318
319#[cfg(test)]
320mod tests {
321    use super::*;
322
323    #[test]
324    fn default_uses_p4_from_path() {
325        let p4 = P4Cli::default();
326
327        assert_eq!(p4.get_bin(), Path::new("p4"));
328    }
329
330    #[test]
331    fn accepts_custom_bin_path() {
332        let p4 = P4Cli::new("C:\\tools\\p4.exe", GlobalOpts::default());
333
334        assert_eq!(p4.get_bin(), Path::new("C:\\tools\\p4.exe"));
335    }
336}