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::Change;
14use crate::cmd::Changes;
15use crate::cmd::Describe;
16use crate::cmd::Diff;
17use crate::cmd::Diff2;
18use crate::cmd::Edit;
19use crate::cmd::FileLog;
20use crate::cmd::Print;
21use crate::cmd::Sync;
22use crate::cmd::Where;
23use crate::global::GlobalOpts;
24
25pub mod cmd;
26pub mod global;
27pub mod spawn;
28
29pub mod prelude {
30 pub use crate::cmd::*;
31 pub use crate::global::*;
32 pub use crate::spawn::*;
33}
34
35#[derive(Debug, Clone)]
36pub struct P4Cli {
37 bin: PathBuf,
38
39 global_opts: GlobalOpts,
40}
41
42impl Default for P4Cli {
43 fn default() -> Self {
44 Self {
45 bin: PathBuf::from("p4"),
46 global_opts: GlobalOpts::default(),
47 }
48 }
49}
50
51impl P4Cli {
52 pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
56 Self {
57 bin: bin.into(),
58 global_opts,
59 }
60 }
61
62 pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
64 self.set_bin(bin);
65 self
66 }
67
68 pub fn global_opts(mut self, global_opts: GlobalOpts) -> Self {
70 self.set_global_opts(global_opts);
71 self
72 }
73
74 pub fn get_bin(&self) -> &Path {
76 &self.bin
77 }
78
79 pub fn get_global_opts(&self) -> &GlobalOpts {
81 &self.global_opts
82 }
83
84 pub fn set_bin(&mut self, bin: impl Into<PathBuf>) {
86 self.bin = bin.into();
87 }
88
89 pub fn set_global_opts(&mut self, global_opts: GlobalOpts) {
91 self.global_opts = global_opts;
92 }
93
94 pub fn add(&self) -> Add {
96 Add::new(self.bin.clone(), self.global_opts.clone())
97 }
98
99 pub fn admin(&self) -> AdminEntry {
101 AdminEntry::new(self.bin.clone(), self.global_opts.clone())
102 }
103
104 #[cfg(not(feature = "lt2016_1"))]
106 pub fn aliases(&self) -> Aliases {
107 Aliases::new(self.bin.clone(), self.global_opts.clone())
108 }
109
110 pub fn annotate(&self) -> Annotate {
112 Annotate::new(self.bin.clone(), self.global_opts.clone())
113 }
114
115 pub fn archive(&self) -> Archive {
117 Archive::new(self.bin.clone(), self.global_opts.clone())
118 }
119
120 pub fn attribute(&self) -> Attribute {
122 Attribute::new(self.bin.clone(), self.global_opts.clone())
123 }
124
125 #[cfg(not(feature = "lt2017_2"))]
131 pub fn bgtask(&self) -> BackGroundTask {
132 BackGroundTask::new(self.bin.clone(), self.global_opts.clone())
133 }
134
135 pub fn branch(&self) -> Branch {
137 Branch::new(self.bin.clone(), self.global_opts.clone())
138 }
139
140 pub fn change(&self) -> Change {
142 Change::new(self.bin.clone(), self.global_opts.clone())
143 }
144
145 pub fn changelist(&self) -> Change {
150 Change::new(self.bin.clone(), self.global_opts.clone())
151 }
152
153 pub fn changes(&self) -> Changes {
155 Changes::new(self.bin.clone(), self.global_opts.clone())
156 }
157
158 pub fn changelists(&self) -> Changes {
163 Changes::new(self.bin.clone(), self.global_opts.clone())
164 }
165
166 pub fn describe(&self) -> Describe {
168 Describe::new(self.bin.clone(), self.global_opts.clone())
169 }
170
171 pub fn diff(&self) -> Diff {
173 Diff::new(self.bin.clone(), self.global_opts.clone())
174 }
175
176 pub fn diff2(&self) -> Diff2 {
178 Diff2::new(self.bin.clone(), self.global_opts.clone())
179 }
180
181 pub fn edit(&self) -> Edit {
183 Edit::new(self.bin.clone(), self.global_opts.clone())
184 }
185
186 pub fn filelog(&self) -> FileLog {
188 FileLog::new(self.bin.clone(), self.global_opts.clone())
189 }
190
191 pub fn print(&self) -> Print {
193 Print::new(self.bin.clone(), self.global_opts.clone())
194 }
195
196 pub fn sync(&self) -> Sync {
198 Sync::new(self.bin.clone(), self.global_opts.clone())
199 }
200
201 pub fn r#where(&self) -> Where {
203 Where::new(self.bin.clone(), self.global_opts.clone())
204 }
205}
206
207#[cfg(test)]
208mod tests {
209 use super::*;
210
211 #[test]
212 fn default_uses_p4_from_path() {
213 let p4 = P4Cli::default();
214
215 assert_eq!(p4.get_bin(), Path::new("p4"));
216 }
217
218 #[test]
219 fn accepts_custom_bin_path() {
220 let p4 = P4Cli::new("C:\\tools\\p4.exe", GlobalOpts::default());
221
222 assert_eq!(p4.get_bin(), Path::new("C:\\tools\\p4.exe"));
223 }
224}