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 pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
71 Self {
72 bin: bin.into(),
73 global_opts,
74 }
75 }
76
77 pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
79 self.set_bin(bin);
80 self
81 }
82
83 pub fn global_opts(mut self, global_opts: GlobalOpts) -> Self {
85 self.set_global_opts(global_opts);
86 self
87 }
88
89 pub fn get_bin(&self) -> &Path {
91 &self.bin
92 }
93
94 pub fn get_global_opts(&self) -> &GlobalOpts {
96 &self.global_opts
97 }
98
99 pub fn set_bin(&mut self, bin: impl Into<PathBuf>) {
101 self.bin = bin.into();
102 }
103
104 pub fn set_global_opts(&mut self, global_opts: GlobalOpts) {
106 self.global_opts = global_opts;
107 }
108
109 pub fn cmd_add(&self) -> Add {
111 Add::new(self.bin.clone(), self.global_opts.clone())
112 }
113
114 pub fn cmd_admin(&self) -> AdminEntry {
116 AdminEntry::new(self.bin.clone(), self.global_opts.clone())
117 }
118
119 #[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 pub fn cmd_annotate(&self) -> Annotate {
127 Annotate::new(self.bin.clone(), self.global_opts.clone())
128 }
129
130 pub fn cmd_archive(&self) -> Archive {
132 Archive::new(self.bin.clone(), self.global_opts.clone())
133 }
134
135 pub fn cmd_attribute(&self) -> Attribute {
137 Attribute::new(self.bin.clone(), self.global_opts.clone())
138 }
139
140 #[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 pub fn cmd_branch(&self) -> Branch {
152 Branch::new(self.bin.clone(), self.global_opts.clone())
153 }
154
155 pub fn cmd_branches(&self) -> Branches {
157 Branches::new(self.bin.clone(), self.global_opts.clone())
158 }
159
160 pub fn cmd_cachepurge(&self) -> CachePurge {
168 CachePurge::new(self.bin.clone(), self.global_opts.clone())
169 }
170
171 pub fn cmd_clean(&self) -> Clean {
179 Clean::new(self.bin.clone(), self.global_opts.clone())
180 }
181
182 pub fn cmd_client(&self) -> Client {
190 Client::new(self.bin.clone(), self.global_opts.clone())
191 }
192
193 pub fn cmd_clients(&self) -> Clients {
201 Clients::new(self.bin.clone(), self.global_opts.clone())
202 }
203
204 pub fn cmd_workspaces(&self) -> Clients {
209 Clients::new(self.bin.clone(), self.global_opts.clone())
210 }
211
212 #[cfg(not(feature = "lt2015_1"))]
221 pub fn cmd_clone(&self) -> Clone {
222 Clone::new(self.bin.clone())
223 }
224
225 #[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 pub fn cmd_copy(&self) -> Copy {
247 Copy::new(self.bin.clone(), self.global_opts.clone())
248 }
249
250 #[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 pub fn cmd_counters(&self) -> Counters {
267 Counters::new(self.bin.clone(), self.global_opts.clone())
268 }
269
270 pub fn cmd_cstat(&self) -> CStat {
272 CStat::new(self.bin.clone(), self.global_opts.clone())
273 }
274
275 pub fn cmd_dbschema(&self) -> DBSchema {
277 DBSchema::new(self.bin.clone(), self.global_opts.clone())
278 }
279
280 pub fn cmd_dbstat(&self) -> DBStat {
282 DBStat::new(self.bin.clone(), self.global_opts.clone())
283 }
284
285 pub fn cmd_dbverify(&self) -> DBVerify {
287 DBVerify::new(self.bin.clone(), self.global_opts.clone())
288 }
289
290 pub fn cmd_workspace(&self) -> Client {
295 Client::new(self.bin.clone(), self.global_opts.clone())
296 }
297
298 pub fn cmd_change(&self) -> Change {
300 Change::new(self.bin.clone(), self.global_opts.clone())
301 }
302
303 pub fn cmd_changelist(&self) -> Change {
308 Change::new(self.bin.clone(), self.global_opts.clone())
309 }
310
311 pub fn cmd_changes(&self) -> Changes {
313 Changes::new(self.bin.clone(), self.global_opts.clone())
314 }
315
316 pub fn cmd_changelists(&self) -> Changes {
321 Changes::new(self.bin.clone(), self.global_opts.clone())
322 }
323
324 pub fn cmd_describe(&self) -> Describe {
326 Describe::new(self.bin.clone(), self.global_opts.clone())
327 }
328
329 pub fn cmd_diff(&self) -> Diff {
331 Diff::new(self.bin.clone(), self.global_opts.clone())
332 }
333
334 pub fn cmd_diff2(&self) -> Diff2 {
336 Diff2::new(self.bin.clone(), self.global_opts.clone())
337 }
338
339 pub fn cmd_edit(&self) -> Edit {
341 Edit::new(self.bin.clone(), self.global_opts.clone())
342 }
343
344 pub fn cmd_filelog(&self) -> FileLog {
346 FileLog::new(self.bin.clone(), self.global_opts.clone())
347 }
348
349 pub fn cmd_print(&self) -> Print {
351 Print::new(self.bin.clone(), self.global_opts.clone())
352 }
353
354 pub fn cmd_sync(&self) -> Sync {
356 Sync::new(self.bin.clone(), self.global_opts.clone())
357 }
358
359 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}