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 pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
64 Self {
65 bin: bin.into(),
66 global_opts,
67 }
68 }
69
70 pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
72 self.set_bin(bin);
73 self
74 }
75
76 pub fn global_opts(mut self, global_opts: GlobalOpts) -> Self {
78 self.set_global_opts(global_opts);
79 self
80 }
81
82 pub fn get_bin(&self) -> &Path {
84 &self.bin
85 }
86
87 pub fn get_global_opts(&self) -> &GlobalOpts {
89 &self.global_opts
90 }
91
92 pub fn set_bin(&mut self, bin: impl Into<PathBuf>) {
94 self.bin = bin.into();
95 }
96
97 pub fn set_global_opts(&mut self, global_opts: GlobalOpts) {
99 self.global_opts = global_opts;
100 }
101
102 pub fn cmd_add(&self) -> Add {
104 Add::new(self.bin.clone(), self.global_opts.clone())
105 }
106
107 pub fn cmd_admin(&self) -> AdminEntry {
109 AdminEntry::new(self.bin.clone(), self.global_opts.clone())
110 }
111
112 #[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 pub fn cmd_annotate(&self) -> Annotate {
120 Annotate::new(self.bin.clone(), self.global_opts.clone())
121 }
122
123 pub fn cmd_archive(&self) -> Archive {
125 Archive::new(self.bin.clone(), self.global_opts.clone())
126 }
127
128 pub fn cmd_attribute(&self) -> Attribute {
130 Attribute::new(self.bin.clone(), self.global_opts.clone())
131 }
132
133 #[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 pub fn cmd_branch(&self) -> Branch {
145 Branch::new(self.bin.clone(), self.global_opts.clone())
146 }
147
148 pub fn cmd_branches(&self) -> Branches {
150 Branches::new(self.bin.clone(), self.global_opts.clone())
151 }
152
153 pub fn cmd_cachepurge(&self) -> CachePurge {
161 CachePurge::new(self.bin.clone(), self.global_opts.clone())
162 }
163
164 pub fn cmd_clean(&self) -> Clean {
172 Clean::new(self.bin.clone(), self.global_opts.clone())
173 }
174
175 pub fn cmd_client(&self) -> Client {
183 Client::new(self.bin.clone(), self.global_opts.clone())
184 }
185
186 pub fn cmd_clients(&self) -> Clients {
194 Clients::new(self.bin.clone(), self.global_opts.clone())
195 }
196
197 pub fn cmd_workspaces(&self) -> Clients {
202 Clients::new(self.bin.clone(), self.global_opts.clone())
203 }
204
205 #[cfg(not(feature = "lt2015_1"))]
214 pub fn cmd_clone(&self) -> Clone {
215 Clone::new(self.bin.clone())
216 }
217
218 #[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 pub fn cmd_workspace(&self) -> Client {
242 Client::new(self.bin.clone(), self.global_opts.clone())
243 }
244
245 pub fn cmd_change(&self) -> Change {
247 Change::new(self.bin.clone(), self.global_opts.clone())
248 }
249
250 pub fn cmd_changelist(&self) -> Change {
255 Change::new(self.bin.clone(), self.global_opts.clone())
256 }
257
258 pub fn cmd_changes(&self) -> Changes {
260 Changes::new(self.bin.clone(), self.global_opts.clone())
261 }
262
263 pub fn cmd_changelists(&self) -> Changes {
268 Changes::new(self.bin.clone(), self.global_opts.clone())
269 }
270
271 pub fn cmd_describe(&self) -> Describe {
273 Describe::new(self.bin.clone(), self.global_opts.clone())
274 }
275
276 pub fn cmd_diff(&self) -> Diff {
278 Diff::new(self.bin.clone(), self.global_opts.clone())
279 }
280
281 pub fn cmd_diff2(&self) -> Diff2 {
283 Diff2::new(self.bin.clone(), self.global_opts.clone())
284 }
285
286 pub fn cmd_edit(&self) -> Edit {
288 Edit::new(self.bin.clone(), self.global_opts.clone())
289 }
290
291 pub fn cmd_filelog(&self) -> FileLog {
293 FileLog::new(self.bin.clone(), self.global_opts.clone())
294 }
295
296 pub fn cmd_print(&self) -> Print {
298 Print::new(self.bin.clone(), self.global_opts.clone())
299 }
300
301 pub fn cmd_sync(&self) -> Sync {
303 Sync::new(self.bin.clone(), self.global_opts.clone())
304 }
305
306 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}