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;
20use crate::cmd::Describe;
21use crate::cmd::Diff;
22use crate::cmd::Diff2;
23use crate::cmd::Edit;
24use crate::cmd::FileLog;
25use crate::cmd::Print;
26use crate::cmd::Sync;
27use crate::cmd::Where;
28use crate::global::GlobalOpts;
29
30pub mod cmd;
31pub mod global;
32pub mod spawn;
33
34pub mod prelude {
35 pub use crate::cmd::*;
36 pub use crate::global::*;
37 pub use crate::spawn::*;
38}
39
40#[derive(Debug, Clone)]
41pub struct P4Cli {
42 bin: PathBuf,
43
44 global_opts: GlobalOpts,
45}
46
47impl Default for P4Cli {
48 fn default() -> Self {
49 Self {
50 bin: PathBuf::from("p4"),
51 global_opts: GlobalOpts::default(),
52 }
53 }
54}
55
56impl P4Cli {
57 pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
61 Self {
62 bin: bin.into(),
63 global_opts,
64 }
65 }
66
67 pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
69 self.set_bin(bin);
70 self
71 }
72
73 pub fn global_opts(mut self, global_opts: GlobalOpts) -> Self {
75 self.set_global_opts(global_opts);
76 self
77 }
78
79 pub fn get_bin(&self) -> &Path {
81 &self.bin
82 }
83
84 pub fn get_global_opts(&self) -> &GlobalOpts {
86 &self.global_opts
87 }
88
89 pub fn set_bin(&mut self, bin: impl Into<PathBuf>) {
91 self.bin = bin.into();
92 }
93
94 pub fn set_global_opts(&mut self, global_opts: GlobalOpts) {
96 self.global_opts = global_opts;
97 }
98
99 pub fn add(&self) -> Add {
101 Add::new(self.bin.clone(), self.global_opts.clone())
102 }
103
104 pub fn admin(&self) -> AdminEntry {
106 AdminEntry::new(self.bin.clone(), self.global_opts.clone())
107 }
108
109 #[cfg(not(feature = "lt2016_1"))]
111 pub fn aliases(&self) -> Aliases {
112 Aliases::new(self.bin.clone(), self.global_opts.clone())
113 }
114
115 pub fn annotate(&self) -> Annotate {
117 Annotate::new(self.bin.clone(), self.global_opts.clone())
118 }
119
120 pub fn archive(&self) -> Archive {
122 Archive::new(self.bin.clone(), self.global_opts.clone())
123 }
124
125 pub fn attribute(&self) -> Attribute {
127 Attribute::new(self.bin.clone(), self.global_opts.clone())
128 }
129
130 #[cfg(not(feature = "lt2017_2"))]
136 pub fn bgtask(&self) -> BackGroundTask {
137 BackGroundTask::new(self.bin.clone(), self.global_opts.clone())
138 }
139
140 pub fn branch(&self) -> Branch {
142 Branch::new(self.bin.clone(), self.global_opts.clone())
143 }
144
145 pub fn branches(&self) -> Branches {
147 Branches::new(self.bin.clone(), self.global_opts.clone())
148 }
149
150 pub fn cachepurge(&self) -> CachePurge {
158 CachePurge::new(self.bin.clone(), self.global_opts.clone())
159 }
160
161 pub fn clean(&self) -> Clean {
169 Clean::new(self.bin.clone(), self.global_opts.clone())
170 }
171
172 pub fn client(&self) -> Client {
180 Client::new(self.bin.clone(), self.global_opts.clone())
181 }
182
183 pub fn clients(&self) -> Clients {
191 Clients::new(self.bin.clone(), self.global_opts.clone())
192 }
193
194 pub fn workspaces(&self) -> Clients {
199 Clients::new(self.bin.clone(), self.global_opts.clone())
200 }
201
202 pub fn workspace(&self) -> Client {
207 Client::new(self.bin.clone(), self.global_opts.clone())
208 }
209
210 pub fn change(&self) -> Change {
212 Change::new(self.bin.clone(), self.global_opts.clone())
213 }
214
215 pub fn changelist(&self) -> Change {
220 Change::new(self.bin.clone(), self.global_opts.clone())
221 }
222
223 pub fn changes(&self) -> Changes {
225 Changes::new(self.bin.clone(), self.global_opts.clone())
226 }
227
228 pub fn changelists(&self) -> Changes {
233 Changes::new(self.bin.clone(), self.global_opts.clone())
234 }
235
236 pub fn describe(&self) -> Describe {
238 Describe::new(self.bin.clone(), self.global_opts.clone())
239 }
240
241 pub fn diff(&self) -> Diff {
243 Diff::new(self.bin.clone(), self.global_opts.clone())
244 }
245
246 pub fn diff2(&self) -> Diff2 {
248 Diff2::new(self.bin.clone(), self.global_opts.clone())
249 }
250
251 pub fn edit(&self) -> Edit {
253 Edit::new(self.bin.clone(), self.global_opts.clone())
254 }
255
256 pub fn filelog(&self) -> FileLog {
258 FileLog::new(self.bin.clone(), self.global_opts.clone())
259 }
260
261 pub fn print(&self) -> Print {
263 Print::new(self.bin.clone(), self.global_opts.clone())
264 }
265
266 pub fn sync(&self) -> Sync {
268 Sync::new(self.bin.clone(), self.global_opts.clone())
269 }
270
271 pub fn r#where(&self) -> Where {
273 Where::new(self.bin.clone(), self.global_opts.clone())
274 }
275}
276
277#[cfg(test)]
278mod tests {
279 use super::*;
280
281 #[test]
282 fn default_uses_p4_from_path() {
283 let p4 = P4Cli::default();
284
285 assert_eq!(p4.get_bin(), Path::new("p4"));
286 }
287
288 #[test]
289 fn accepts_custom_bin_path() {
290 let p4 = P4Cli::new("C:\\tools\\p4.exe", GlobalOpts::default());
291
292 assert_eq!(p4.get_bin(), Path::new("C:\\tools\\p4.exe"));
293 }
294}