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::Describe;
18use crate::cmd::Diff;
19use crate::cmd::Diff2;
20use crate::cmd::Edit;
21use crate::cmd::FileLog;
22use crate::cmd::Print;
23use crate::cmd::Sync;
24use crate::cmd::Where;
25use crate::global::GlobalOpts;
26
27pub mod cmd;
28pub mod global;
29pub mod spawn;
30
31pub mod prelude {
32 pub use crate::cmd::*;
33 pub use crate::global::*;
34 pub use crate::spawn::*;
35}
36
37#[derive(Debug, Clone)]
38pub struct P4Cli {
39 bin: PathBuf,
40
41 global_opts: GlobalOpts,
42}
43
44impl Default for P4Cli {
45 fn default() -> Self {
46 Self {
47 bin: PathBuf::from("p4"),
48 global_opts: GlobalOpts::default(),
49 }
50 }
51}
52
53impl P4Cli {
54 pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
58 Self {
59 bin: bin.into(),
60 global_opts,
61 }
62 }
63
64 pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
66 self.set_bin(bin);
67 self
68 }
69
70 pub fn global_opts(mut self, global_opts: GlobalOpts) -> Self {
72 self.set_global_opts(global_opts);
73 self
74 }
75
76 pub fn get_bin(&self) -> &Path {
78 &self.bin
79 }
80
81 pub fn get_global_opts(&self) -> &GlobalOpts {
83 &self.global_opts
84 }
85
86 pub fn set_bin(&mut self, bin: impl Into<PathBuf>) {
88 self.bin = bin.into();
89 }
90
91 pub fn set_global_opts(&mut self, global_opts: GlobalOpts) {
93 self.global_opts = global_opts;
94 }
95
96 pub fn add(&self) -> Add {
98 Add::new(self.bin.clone(), self.global_opts.clone())
99 }
100
101 pub fn admin(&self) -> AdminEntry {
103 AdminEntry::new(self.bin.clone(), self.global_opts.clone())
104 }
105
106 #[cfg(not(feature = "lt2016_1"))]
108 pub fn aliases(&self) -> Aliases {
109 Aliases::new(self.bin.clone(), self.global_opts.clone())
110 }
111
112 pub fn annotate(&self) -> Annotate {
114 Annotate::new(self.bin.clone(), self.global_opts.clone())
115 }
116
117 pub fn archive(&self) -> Archive {
119 Archive::new(self.bin.clone(), self.global_opts.clone())
120 }
121
122 pub fn attribute(&self) -> Attribute {
124 Attribute::new(self.bin.clone(), self.global_opts.clone())
125 }
126
127 #[cfg(not(feature = "lt2017_2"))]
133 pub fn bgtask(&self) -> BackGroundTask {
134 BackGroundTask::new(self.bin.clone(), self.global_opts.clone())
135 }
136
137 pub fn branch(&self) -> Branch {
139 Branch::new(self.bin.clone(), self.global_opts.clone())
140 }
141
142 pub fn branches(&self) -> Branches {
144 Branches::new(self.bin.clone(), self.global_opts.clone())
145 }
146
147 pub fn cachepurge(&self) -> CachePurge {
155 CachePurge::new(self.bin.clone(), self.global_opts.clone())
156 }
157
158 pub fn change(&self) -> Change {
160 Change::new(self.bin.clone(), self.global_opts.clone())
161 }
162
163 pub fn changelist(&self) -> Change {
168 Change::new(self.bin.clone(), self.global_opts.clone())
169 }
170
171 pub fn changes(&self) -> Changes {
173 Changes::new(self.bin.clone(), self.global_opts.clone())
174 }
175
176 pub fn changelists(&self) -> Changes {
181 Changes::new(self.bin.clone(), self.global_opts.clone())
182 }
183
184 pub fn describe(&self) -> Describe {
186 Describe::new(self.bin.clone(), self.global_opts.clone())
187 }
188
189 pub fn diff(&self) -> Diff {
191 Diff::new(self.bin.clone(), self.global_opts.clone())
192 }
193
194 pub fn diff2(&self) -> Diff2 {
196 Diff2::new(self.bin.clone(), self.global_opts.clone())
197 }
198
199 pub fn edit(&self) -> Edit {
201 Edit::new(self.bin.clone(), self.global_opts.clone())
202 }
203
204 pub fn filelog(&self) -> FileLog {
206 FileLog::new(self.bin.clone(), self.global_opts.clone())
207 }
208
209 pub fn print(&self) -> Print {
211 Print::new(self.bin.clone(), self.global_opts.clone())
212 }
213
214 pub fn sync(&self) -> Sync {
216 Sync::new(self.bin.clone(), self.global_opts.clone())
217 }
218
219 pub fn r#where(&self) -> Where {
221 Where::new(self.bin.clone(), self.global_opts.clone())
222 }
223}
224
225#[cfg(test)]
226mod tests {
227 use super::*;
228
229 #[test]
230 fn default_uses_p4_from_path() {
231 let p4 = P4Cli::default();
232
233 assert_eq!(p4.get_bin(), Path::new("p4"));
234 }
235
236 #[test]
237 fn accepts_custom_bin_path() {
238 let p4 = P4Cli::new("C:\\tools\\p4.exe", GlobalOpts::default());
239
240 assert_eq!(p4.get_bin(), Path::new("C:\\tools\\p4.exe"));
241 }
242}