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 pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
65 Self {
66 bin: bin.into(),
67 global_opts,
68 }
69 }
70
71 pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
73 self.set_bin(bin);
74 self
75 }
76
77 pub fn global_opts(mut self, global_opts: GlobalOpts) -> Self {
79 self.set_global_opts(global_opts);
80 self
81 }
82
83 pub fn get_bin(&self) -> &Path {
85 &self.bin
86 }
87
88 pub fn get_global_opts(&self) -> &GlobalOpts {
90 &self.global_opts
91 }
92
93 pub fn set_bin(&mut self, bin: impl Into<PathBuf>) {
95 self.bin = bin.into();
96 }
97
98 pub fn set_global_opts(&mut self, global_opts: GlobalOpts) {
100 self.global_opts = global_opts;
101 }
102
103 pub fn cmd_add(&self) -> Add {
105 Add::new(self.bin.clone(), self.global_opts.clone())
106 }
107
108 pub fn cmd_admin(&self) -> AdminEntry {
110 AdminEntry::new(self.bin.clone(), self.global_opts.clone())
111 }
112
113 #[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 pub fn cmd_annotate(&self) -> Annotate {
121 Annotate::new(self.bin.clone(), self.global_opts.clone())
122 }
123
124 pub fn cmd_archive(&self) -> Archive {
126 Archive::new(self.bin.clone(), self.global_opts.clone())
127 }
128
129 pub fn cmd_attribute(&self) -> Attribute {
131 Attribute::new(self.bin.clone(), self.global_opts.clone())
132 }
133
134 #[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 pub fn cmd_branch(&self) -> Branch {
146 Branch::new(self.bin.clone(), self.global_opts.clone())
147 }
148
149 pub fn cmd_branches(&self) -> Branches {
151 Branches::new(self.bin.clone(), self.global_opts.clone())
152 }
153
154 pub fn cmd_cachepurge(&self) -> CachePurge {
162 CachePurge::new(self.bin.clone(), self.global_opts.clone())
163 }
164
165 pub fn cmd_clean(&self) -> Clean {
173 Clean::new(self.bin.clone(), self.global_opts.clone())
174 }
175
176 pub fn cmd_client(&self) -> Client {
184 Client::new(self.bin.clone(), self.global_opts.clone())
185 }
186
187 pub fn cmd_clients(&self) -> Clients {
195 Clients::new(self.bin.clone(), self.global_opts.clone())
196 }
197
198 pub fn cmd_workspaces(&self) -> Clients {
203 Clients::new(self.bin.clone(), self.global_opts.clone())
204 }
205
206 #[cfg(not(feature = "lt2015_1"))]
215 pub fn cmd_clone(&self) -> Clone {
216 Clone::new(self.bin.clone())
217 }
218
219 #[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 pub fn cmd_copy(&self) -> Copy {
241 Copy::new(self.bin.clone(), self.global_opts.clone())
242 }
243
244 pub fn cmd_workspace(&self) -> Client {
249 Client::new(self.bin.clone(), self.global_opts.clone())
250 }
251
252 pub fn cmd_change(&self) -> Change {
254 Change::new(self.bin.clone(), self.global_opts.clone())
255 }
256
257 pub fn cmd_changelist(&self) -> Change {
262 Change::new(self.bin.clone(), self.global_opts.clone())
263 }
264
265 pub fn cmd_changes(&self) -> Changes {
267 Changes::new(self.bin.clone(), self.global_opts.clone())
268 }
269
270 pub fn cmd_changelists(&self) -> Changes {
275 Changes::new(self.bin.clone(), self.global_opts.clone())
276 }
277
278 pub fn cmd_describe(&self) -> Describe {
280 Describe::new(self.bin.clone(), self.global_opts.clone())
281 }
282
283 pub fn cmd_diff(&self) -> Diff {
285 Diff::new(self.bin.clone(), self.global_opts.clone())
286 }
287
288 pub fn cmd_diff2(&self) -> Diff2 {
290 Diff2::new(self.bin.clone(), self.global_opts.clone())
291 }
292
293 pub fn cmd_edit(&self) -> Edit {
295 Edit::new(self.bin.clone(), self.global_opts.clone())
296 }
297
298 pub fn cmd_filelog(&self) -> FileLog {
300 FileLog::new(self.bin.clone(), self.global_opts.clone())
301 }
302
303 pub fn cmd_print(&self) -> Print {
305 Print::new(self.bin.clone(), self.global_opts.clone())
306 }
307
308 pub fn cmd_sync(&self) -> Sync {
310 Sync::new(self.bin.clone(), self.global_opts.clone())
311 }
312
313 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}