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::Change;
15use crate::cmd::Changes;
16use crate::cmd::Describe;
17use crate::cmd::Diff;
18use crate::cmd::Diff2;
19use crate::cmd::Edit;
20use crate::cmd::FileLog;
21use crate::cmd::Print;
22use crate::cmd::Sync;
23use crate::cmd::Where;
24use crate::global::GlobalOpts;
25
26pub mod cmd;
27pub mod global;
28pub mod spawn;
29
30pub mod prelude {
31 pub use crate::cmd::*;
32 pub use crate::global::*;
33 pub use crate::spawn::*;
34}
35
36#[derive(Debug, Clone)]
37pub struct P4Cli {
38 bin: PathBuf,
39
40 global_opts: GlobalOpts,
41}
42
43impl Default for P4Cli {
44 fn default() -> Self {
45 Self {
46 bin: PathBuf::from("p4"),
47 global_opts: GlobalOpts::default(),
48 }
49 }
50}
51
52impl P4Cli {
53 pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
57 Self {
58 bin: bin.into(),
59 global_opts,
60 }
61 }
62
63 pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
65 self.set_bin(bin);
66 self
67 }
68
69 pub fn global_opts(mut self, global_opts: GlobalOpts) -> Self {
71 self.set_global_opts(global_opts);
72 self
73 }
74
75 pub fn get_bin(&self) -> &Path {
77 &self.bin
78 }
79
80 pub fn get_global_opts(&self) -> &GlobalOpts {
82 &self.global_opts
83 }
84
85 pub fn set_bin(&mut self, bin: impl Into<PathBuf>) {
87 self.bin = bin.into();
88 }
89
90 pub fn set_global_opts(&mut self, global_opts: GlobalOpts) {
92 self.global_opts = global_opts;
93 }
94
95 pub fn add(&self) -> Add {
97 Add::new(self.bin.clone(), self.global_opts.clone())
98 }
99
100 pub fn admin(&self) -> AdminEntry {
102 AdminEntry::new(self.bin.clone(), self.global_opts.clone())
103 }
104
105 #[cfg(not(feature = "lt2016_1"))]
107 pub fn aliases(&self) -> Aliases {
108 Aliases::new(self.bin.clone(), self.global_opts.clone())
109 }
110
111 pub fn annotate(&self) -> Annotate {
113 Annotate::new(self.bin.clone(), self.global_opts.clone())
114 }
115
116 pub fn archive(&self) -> Archive {
118 Archive::new(self.bin.clone(), self.global_opts.clone())
119 }
120
121 pub fn attribute(&self) -> Attribute {
123 Attribute::new(self.bin.clone(), self.global_opts.clone())
124 }
125
126 #[cfg(not(feature = "lt2017_2"))]
132 pub fn bgtask(&self) -> BackGroundTask {
133 BackGroundTask::new(self.bin.clone(), self.global_opts.clone())
134 }
135
136 pub fn branch(&self) -> Branch {
138 Branch::new(self.bin.clone(), self.global_opts.clone())
139 }
140
141 pub fn branches(&self) -> Branches {
143 Branches::new(self.bin.clone(), self.global_opts.clone())
144 }
145
146 pub fn change(&self) -> Change {
148 Change::new(self.bin.clone(), self.global_opts.clone())
149 }
150
151 pub fn changelist(&self) -> Change {
156 Change::new(self.bin.clone(), self.global_opts.clone())
157 }
158
159 pub fn changes(&self) -> Changes {
161 Changes::new(self.bin.clone(), self.global_opts.clone())
162 }
163
164 pub fn changelists(&self) -> Changes {
169 Changes::new(self.bin.clone(), self.global_opts.clone())
170 }
171
172 pub fn describe(&self) -> Describe {
174 Describe::new(self.bin.clone(), self.global_opts.clone())
175 }
176
177 pub fn diff(&self) -> Diff {
179 Diff::new(self.bin.clone(), self.global_opts.clone())
180 }
181
182 pub fn diff2(&self) -> Diff2 {
184 Diff2::new(self.bin.clone(), self.global_opts.clone())
185 }
186
187 pub fn edit(&self) -> Edit {
189 Edit::new(self.bin.clone(), self.global_opts.clone())
190 }
191
192 pub fn filelog(&self) -> FileLog {
194 FileLog::new(self.bin.clone(), self.global_opts.clone())
195 }
196
197 pub fn print(&self) -> Print {
199 Print::new(self.bin.clone(), self.global_opts.clone())
200 }
201
202 pub fn sync(&self) -> Sync {
204 Sync::new(self.bin.clone(), self.global_opts.clone())
205 }
206
207 pub fn r#where(&self) -> Where {
209 Where::new(self.bin.clone(), self.global_opts.clone())
210 }
211}
212
213#[cfg(test)]
214mod tests {
215 use super::*;
216
217 #[test]
218 fn default_uses_p4_from_path() {
219 let p4 = P4Cli::default();
220
221 assert_eq!(p4.get_bin(), Path::new("p4"));
222 }
223
224 #[test]
225 fn accepts_custom_bin_path() {
226 let p4 = P4Cli::new("C:\\tools\\p4.exe", GlobalOpts::default());
227
228 assert_eq!(p4.get_bin(), Path::new("C:\\tools\\p4.exe"));
229 }
230}