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;
10use crate::cmd::Change;
11use crate::cmd::Changes;
12use crate::cmd::Describe;
13use crate::cmd::Diff;
14use crate::cmd::Diff2;
15use crate::cmd::Edit;
16use crate::cmd::FileLog;
17use crate::cmd::Print;
18use crate::cmd::Sync;
19use crate::cmd::Where;
20use crate::global::GlobalOpts;
21
22pub mod cmd;
23pub mod global;
24pub mod spawn;
25
26pub mod prelude {
27 pub use crate::cmd::*;
28 pub use crate::global::*;
29 pub use crate::spawn::*;
30}
31
32#[derive(Debug, Clone)]
33pub struct P4Cli {
34 bin: PathBuf,
35
36 global_opts: GlobalOpts,
37}
38
39impl Default for P4Cli {
40 fn default() -> Self {
41 Self {
42 bin: PathBuf::from("p4"),
43 global_opts: GlobalOpts::default(),
44 }
45 }
46}
47
48impl P4Cli {
49 pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
53 Self {
54 bin: bin.into(),
55 global_opts,
56 }
57 }
58
59 pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
61 self.set_bin(bin);
62 self
63 }
64
65 pub fn global_opts(mut self, global_opts: GlobalOpts) -> Self {
67 self.set_global_opts(global_opts);
68 self
69 }
70
71 pub fn get_bin(&self) -> &Path {
73 &self.bin
74 }
75
76 pub fn get_global_opts(&self) -> &GlobalOpts {
78 &self.global_opts
79 }
80
81 pub fn set_bin(&mut self, bin: impl Into<PathBuf>) {
83 self.bin = bin.into();
84 }
85
86 pub fn set_global_opts(&mut self, global_opts: GlobalOpts) {
88 self.global_opts = global_opts;
89 }
90
91 pub fn add(&self) -> Add {
93 Add::new(self.bin.clone(), self.global_opts.clone())
94 }
95
96 pub fn admin(&self) -> AdminEntry {
98 AdminEntry::new(self.bin.clone(), self.global_opts.clone())
99 }
100
101 #[cfg(not(feature = "lt2016_1"))]
103 pub fn aliases(&self) -> Aliases {
104 Aliases::new(self.bin.clone(), self.global_opts.clone())
105 }
106
107 pub fn annotate(&self) -> Annotate {
109 Annotate::new(self.bin.clone(), self.global_opts.clone())
110 }
111
112 pub fn archive(&self) -> Archive {
114 Archive::new(self.bin.clone(), self.global_opts.clone())
115 }
116
117 pub fn attribute(&self) -> Attribute {
119 Attribute::new(self.bin.clone(), self.global_opts.clone())
120 }
121
122 pub fn change(&self) -> Change {
124 Change::new(self.bin.clone(), self.global_opts.clone())
125 }
126
127 pub fn changelist(&self) -> Change {
132 Change::new(self.bin.clone(), self.global_opts.clone())
133 }
134
135 pub fn changes(&self) -> Changes {
137 Changes::new(self.bin.clone(), self.global_opts.clone())
138 }
139
140 pub fn changelists(&self) -> Changes {
145 Changes::new(self.bin.clone(), self.global_opts.clone())
146 }
147
148 pub fn describe(&self) -> Describe {
150 Describe::new(self.bin.clone(), self.global_opts.clone())
151 }
152
153 pub fn diff(&self) -> Diff {
155 Diff::new(self.bin.clone(), self.global_opts.clone())
156 }
157
158 pub fn diff2(&self) -> Diff2 {
160 Diff2::new(self.bin.clone(), self.global_opts.clone())
161 }
162
163 pub fn edit(&self) -> Edit {
165 Edit::new(self.bin.clone(), self.global_opts.clone())
166 }
167
168 pub fn filelog(&self) -> FileLog {
170 FileLog::new(self.bin.clone(), self.global_opts.clone())
171 }
172
173 pub fn print(&self) -> Print {
175 Print::new(self.bin.clone(), self.global_opts.clone())
176 }
177
178 pub fn sync(&self) -> Sync {
180 Sync::new(self.bin.clone(), self.global_opts.clone())
181 }
182
183 pub fn r#where(&self) -> Where {
185 Where::new(self.bin.clone(), self.global_opts.clone())
186 }
187}
188
189#[cfg(test)]
190mod tests {
191 use super::*;
192
193 #[test]
194 fn default_uses_p4_from_path() {
195 let p4 = P4Cli::default();
196
197 assert_eq!(p4.get_bin(), Path::new("p4"));
198 }
199
200 #[test]
201 fn accepts_custom_bin_path() {
202 let p4 = P4Cli::new("C:\\tools\\p4.exe", GlobalOpts::default());
203
204 assert_eq!(p4.get_bin(), Path::new("C:\\tools\\p4.exe"));
205 }
206}