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::Describe;
20use crate::cmd::Diff;
21use crate::cmd::Diff2;
22use crate::cmd::Edit;
23use crate::cmd::FileLog;
24use crate::cmd::Print;
25use crate::cmd::Sync;
26use crate::cmd::Where;
27use crate::global::GlobalOpts;
28
29pub mod cmd;
30pub mod global;
31pub mod spawn;
32
33pub mod prelude {
34 pub use crate::cmd::*;
35 pub use crate::global::*;
36 pub use crate::spawn::*;
37}
38
39#[derive(Debug, Clone)]
40pub struct P4Cli {
41 bin: PathBuf,
42
43 global_opts: GlobalOpts,
44}
45
46impl Default for P4Cli {
47 fn default() -> Self {
48 Self {
49 bin: PathBuf::from("p4"),
50 global_opts: GlobalOpts::default(),
51 }
52 }
53}
54
55impl P4Cli {
56 pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
60 Self {
61 bin: bin.into(),
62 global_opts,
63 }
64 }
65
66 pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
68 self.set_bin(bin);
69 self
70 }
71
72 pub fn global_opts(mut self, global_opts: GlobalOpts) -> Self {
74 self.set_global_opts(global_opts);
75 self
76 }
77
78 pub fn get_bin(&self) -> &Path {
80 &self.bin
81 }
82
83 pub fn get_global_opts(&self) -> &GlobalOpts {
85 &self.global_opts
86 }
87
88 pub fn set_bin(&mut self, bin: impl Into<PathBuf>) {
90 self.bin = bin.into();
91 }
92
93 pub fn set_global_opts(&mut self, global_opts: GlobalOpts) {
95 self.global_opts = global_opts;
96 }
97
98 pub fn add(&self) -> Add {
100 Add::new(self.bin.clone(), self.global_opts.clone())
101 }
102
103 pub fn admin(&self) -> AdminEntry {
105 AdminEntry::new(self.bin.clone(), self.global_opts.clone())
106 }
107
108 #[cfg(not(feature = "lt2016_1"))]
110 pub fn aliases(&self) -> Aliases {
111 Aliases::new(self.bin.clone(), self.global_opts.clone())
112 }
113
114 pub fn annotate(&self) -> Annotate {
116 Annotate::new(self.bin.clone(), self.global_opts.clone())
117 }
118
119 pub fn archive(&self) -> Archive {
121 Archive::new(self.bin.clone(), self.global_opts.clone())
122 }
123
124 pub fn attribute(&self) -> Attribute {
126 Attribute::new(self.bin.clone(), self.global_opts.clone())
127 }
128
129 #[cfg(not(feature = "lt2017_2"))]
135 pub fn bgtask(&self) -> BackGroundTask {
136 BackGroundTask::new(self.bin.clone(), self.global_opts.clone())
137 }
138
139 pub fn branch(&self) -> Branch {
141 Branch::new(self.bin.clone(), self.global_opts.clone())
142 }
143
144 pub fn branches(&self) -> Branches {
146 Branches::new(self.bin.clone(), self.global_opts.clone())
147 }
148
149 pub fn cachepurge(&self) -> CachePurge {
157 CachePurge::new(self.bin.clone(), self.global_opts.clone())
158 }
159
160 pub fn clean(&self) -> Clean {
168 Clean::new(self.bin.clone(), self.global_opts.clone())
169 }
170
171 pub fn client(&self) -> Client {
179 Client::new(self.bin.clone(), self.global_opts.clone())
180 }
181
182 pub fn workspace(&self) -> Client {
187 Client::new(self.bin.clone(), self.global_opts.clone())
188 }
189
190 pub fn change(&self) -> Change {
192 Change::new(self.bin.clone(), self.global_opts.clone())
193 }
194
195 pub fn changelist(&self) -> Change {
200 Change::new(self.bin.clone(), self.global_opts.clone())
201 }
202
203 pub fn changes(&self) -> Changes {
205 Changes::new(self.bin.clone(), self.global_opts.clone())
206 }
207
208 pub fn changelists(&self) -> Changes {
213 Changes::new(self.bin.clone(), self.global_opts.clone())
214 }
215
216 pub fn describe(&self) -> Describe {
218 Describe::new(self.bin.clone(), self.global_opts.clone())
219 }
220
221 pub fn diff(&self) -> Diff {
223 Diff::new(self.bin.clone(), self.global_opts.clone())
224 }
225
226 pub fn diff2(&self) -> Diff2 {
228 Diff2::new(self.bin.clone(), self.global_opts.clone())
229 }
230
231 pub fn edit(&self) -> Edit {
233 Edit::new(self.bin.clone(), self.global_opts.clone())
234 }
235
236 pub fn filelog(&self) -> FileLog {
238 FileLog::new(self.bin.clone(), self.global_opts.clone())
239 }
240
241 pub fn print(&self) -> Print {
243 Print::new(self.bin.clone(), self.global_opts.clone())
244 }
245
246 pub fn sync(&self) -> Sync {
248 Sync::new(self.bin.clone(), self.global_opts.clone())
249 }
250
251 pub fn r#where(&self) -> Where {
253 Where::new(self.bin.clone(), self.global_opts.clone())
254 }
255}
256
257#[cfg(test)]
258mod tests {
259 use super::*;
260
261 #[test]
262 fn default_uses_p4_from_path() {
263 let p4 = P4Cli::default();
264
265 assert_eq!(p4.get_bin(), Path::new("p4"));
266 }
267
268 #[test]
269 fn accepts_custom_bin_path() {
270 let p4 = P4Cli::new("C:\\tools\\p4.exe", GlobalOpts::default());
271
272 assert_eq!(p4.get_bin(), Path::new("C:\\tools\\p4.exe"));
273 }
274}