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::Delete;
31use crate::cmd::Depot;
32use crate::cmd::Depots;
33use crate::cmd::Describe;
34#[cfg(not(feature = "lt2024_2"))]
35use crate::cmd::Diagnostics;
36use crate::cmd::Diff;
37use crate::cmd::Diff2;
38use crate::cmd::Dirs;
39use crate::cmd::DiskSpace;
40use crate::cmd::Edit;
41use crate::cmd::Export;
42#[cfg(not(feature = "lt2019_1"))]
43use crate::cmd::ExtensionEntry;
44use crate::cmd::FileLog;
45use crate::cmd::Print;
46use crate::cmd::Sync;
47use crate::cmd::Where;
48use crate::global::GlobalOpts;
49
50pub mod cmd;
51pub mod global;
52pub mod spawn;
53
54pub mod prelude {
55 pub use crate::cmd::*;
56 pub use crate::global::*;
57 pub use crate::spawn::*;
58}
59
60#[derive(Debug, Clone)]
61pub struct P4Cli {
62 bin: PathBuf,
63
64 global_opts: GlobalOpts,
65}
66
67impl Default for P4Cli {
68 fn default() -> Self {
69 Self {
70 bin: PathBuf::from("p4"),
71 global_opts: GlobalOpts::default(),
72 }
73 }
74}
75
76impl P4Cli {
77 pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
81 Self {
82 bin: bin.into(),
83 global_opts,
84 }
85 }
86
87 pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
89 self.set_bin(bin);
90 self
91 }
92
93 pub fn global_opts(mut self, global_opts: GlobalOpts) -> Self {
95 self.set_global_opts(global_opts);
96 self
97 }
98
99 pub fn get_bin(&self) -> &Path {
101 &self.bin
102 }
103
104 pub fn get_global_opts(&self) -> &GlobalOpts {
106 &self.global_opts
107 }
108
109 pub fn set_bin(&mut self, bin: impl Into<PathBuf>) {
111 self.bin = bin.into();
112 }
113
114 pub fn set_global_opts(&mut self, global_opts: GlobalOpts) {
116 self.global_opts = global_opts;
117 }
118
119 pub fn cmd_add(&self) -> Add {
121 Add::new(self.bin.clone(), self.global_opts.clone())
122 }
123
124 pub fn cmd_admin(&self) -> AdminEntry {
126 AdminEntry::new(self.bin.clone(), self.global_opts.clone())
127 }
128
129 #[cfg(not(feature = "lt2016_1"))]
131 pub fn cmd_aliases(&self) -> Aliases {
132 Aliases::new(self.bin.clone(), self.global_opts.clone())
133 }
134
135 pub fn cmd_annotate(&self) -> Annotate {
137 Annotate::new(self.bin.clone(), self.global_opts.clone())
138 }
139
140 pub fn cmd_archive(&self) -> Archive {
142 Archive::new(self.bin.clone(), self.global_opts.clone())
143 }
144
145 pub fn cmd_attribute(&self) -> Attribute {
147 Attribute::new(self.bin.clone(), self.global_opts.clone())
148 }
149
150 #[cfg(not(feature = "lt2017_2"))]
156 pub fn cmd_bgtask(&self) -> BackGroundTask {
157 BackGroundTask::new(self.bin.clone(), self.global_opts.clone())
158 }
159
160 pub fn cmd_branch(&self) -> Branch {
162 Branch::new(self.bin.clone(), self.global_opts.clone())
163 }
164
165 pub fn cmd_branches(&self) -> Branches {
167 Branches::new(self.bin.clone(), self.global_opts.clone())
168 }
169
170 pub fn cmd_cachepurge(&self) -> CachePurge {
178 CachePurge::new(self.bin.clone(), self.global_opts.clone())
179 }
180
181 pub fn cmd_clean(&self) -> Clean {
189 Clean::new(self.bin.clone(), self.global_opts.clone())
190 }
191
192 pub fn cmd_client(&self) -> Client {
200 Client::new(self.bin.clone(), self.global_opts.clone())
201 }
202
203 pub fn cmd_clients(&self) -> Clients {
211 Clients::new(self.bin.clone(), self.global_opts.clone())
212 }
213
214 pub fn cmd_workspaces(&self) -> Clients {
219 Clients::new(self.bin.clone(), self.global_opts.clone())
220 }
221
222 #[cfg(not(feature = "lt2015_1"))]
231 pub fn cmd_clone(&self) -> Clone {
232 Clone::new(self.bin.clone())
233 }
234
235 #[cfg_attr(not(feature = "lt2019_2"), doc = "")]
241 #[cfg_attr(
242 not(feature = "lt2019_2"),
243 doc = "The [`Configure::history`](Configure::history) operation selects the `p4 configure history` report."
244 )]
245 #[cfg_attr(not(feature = "lt2023_2"), doc = "")]
246 #[cfg_attr(
247 not(feature = "lt2023_2"),
248 doc = "The [`Configure::patch`](Configure::patch) operation retroactively sets a history comment, and [`Configure::help`](Configure::help) prints the configurables reference."
249 )]
250 pub fn cmd_configure(&self) -> Configure {
251 Configure::new(self.bin.clone(), self.global_opts.clone())
252 }
253
254 pub fn cmd_copy(&self) -> Copy {
257 Copy::new(self.bin.clone(), self.global_opts.clone())
258 }
259
260 #[cfg_attr(
263 feature = "lt2016_1",
264 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."
265 )]
266 #[cfg_attr(
267 not(feature = "lt2016_1"),
268 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."
269 )]
270 pub fn cmd_counter(&self) -> Counter {
271 Counter::new(self.bin.clone(), self.global_opts.clone())
272 }
273
274 pub fn cmd_counters(&self) -> Counters {
277 Counters::new(self.bin.clone(), self.global_opts.clone())
278 }
279
280 pub fn cmd_cstat(&self) -> CStat {
282 CStat::new(self.bin.clone(), self.global_opts.clone())
283 }
284
285 pub fn cmd_dbschema(&self) -> DBSchema {
287 DBSchema::new(self.bin.clone(), self.global_opts.clone())
288 }
289
290 pub fn cmd_dbstat(&self) -> DBStat {
292 DBStat::new(self.bin.clone(), self.global_opts.clone())
293 }
294
295 pub fn cmd_dbverify(&self) -> DBVerify {
297 DBVerify::new(self.bin.clone(), self.global_opts.clone())
298 }
299
300 pub fn cmd_workspace(&self) -> Client {
305 Client::new(self.bin.clone(), self.global_opts.clone())
306 }
307
308 pub fn cmd_change(&self) -> Change {
310 Change::new(self.bin.clone(), self.global_opts.clone())
311 }
312
313 pub fn cmd_changelist(&self) -> Change {
318 Change::new(self.bin.clone(), self.global_opts.clone())
319 }
320
321 pub fn cmd_changes(&self) -> Changes {
323 Changes::new(self.bin.clone(), self.global_opts.clone())
324 }
325
326 pub fn cmd_changelists(&self) -> Changes {
331 Changes::new(self.bin.clone(), self.global_opts.clone())
332 }
333
334 pub fn cmd_delete(&self) -> Delete {
336 Delete::new(self.bin.clone(), self.global_opts.clone())
337 }
338
339 pub fn cmd_depot(&self) -> Depot {
341 Depot::new(self.bin.clone(), self.global_opts.clone())
342 }
343
344 pub fn cmd_depots(&self) -> Depots {
346 Depots::new(self.bin.clone(), self.global_opts.clone())
347 }
348
349 pub fn cmd_describe(&self) -> Describe {
351 Describe::new(self.bin.clone(), self.global_opts.clone())
352 }
353
354 #[cfg(not(feature = "lt2024_2"))]
356 pub fn cmd_diagnostics(&self) -> Diagnostics {
357 Diagnostics::new(self.bin.clone(), self.global_opts.clone())
358 }
359
360 pub fn cmd_diff(&self) -> Diff {
362 Diff::new(self.bin.clone(), self.global_opts.clone())
363 }
364
365 pub fn cmd_diff2(&self) -> Diff2 {
367 Diff2::new(self.bin.clone(), self.global_opts.clone())
368 }
369
370 pub fn cmd_dirs(&self) -> Dirs {
372 Dirs::new(self.bin.clone(), self.global_opts.clone())
373 }
374
375 pub fn cmd_diskspace(&self) -> DiskSpace {
377 DiskSpace::new(self.bin.clone(), self.global_opts.clone())
378 }
379
380 pub fn cmd_df(&self) -> DiskSpace {
385 DiskSpace::new(self.bin.clone(), self.global_opts.clone())
386 }
387
388 pub fn cmd_edit(&self) -> Edit {
390 Edit::new(self.bin.clone(), self.global_opts.clone())
391 }
392
393 pub fn cmd_export(&self) -> Export {
399 Export::new(self.bin.clone(), self.global_opts.clone())
400 }
401
402 #[cfg(not(feature = "lt2019_1"))]
408 pub fn cmd_extension(&self) -> ExtensionEntry {
409 ExtensionEntry::new(self.bin.clone(), self.global_opts.clone())
410 }
411
412 pub fn cmd_filelog(&self) -> FileLog {
414 FileLog::new(self.bin.clone(), self.global_opts.clone())
415 }
416
417 pub fn cmd_print(&self) -> Print {
419 Print::new(self.bin.clone(), self.global_opts.clone())
420 }
421
422 pub fn cmd_sync(&self) -> Sync {
424 Sync::new(self.bin.clone(), self.global_opts.clone())
425 }
426
427 pub fn cmd_where(&self) -> Where {
429 Where::new(self.bin.clone(), self.global_opts.clone())
430 }
431}
432
433#[cfg(test)]
434mod tests {
435 use super::*;
436
437 #[test]
438 fn default_uses_p4_from_path() {
439 let p4 = P4Cli::default();
440
441 assert_eq!(p4.get_bin(), Path::new("p4"));
442 }
443
444 #[test]
445 fn accepts_custom_bin_path() {
446 let p4 = P4Cli::new("C:\\tools\\p4.exe", GlobalOpts::default());
447
448 assert_eq!(p4.get_bin(), Path::new("C:\\tools\\p4.exe"));
449 }
450}