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