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