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::Changes;
11use crate::cmd::Describe;
12use crate::cmd::Edit;
13use crate::cmd::FileLog;
14use crate::cmd::Print;
15use crate::cmd::Sync;
16use crate::cmd::Where;
17use crate::global::GlobalOpts;
18
19pub mod cmd;
20pub mod global;
21pub mod spawn;
22
23pub mod prelude {
24 pub use crate::cmd::*;
25 pub use crate::global::*;
26 pub use crate::spawn::*;
27}
28
29#[derive(Debug, Clone)]
30pub struct P4Cli {
31 bin: PathBuf,
32
33 global_opts: GlobalOpts,
34}
35
36impl Default for P4Cli {
37 fn default() -> Self {
38 Self {
39 bin: PathBuf::from("p4"),
40 global_opts: GlobalOpts::default(),
41 }
42 }
43}
44
45impl P4Cli {
46 pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
50 Self {
51 bin: bin.into(),
52 global_opts,
53 }
54 }
55
56 pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
58 self.set_bin(bin);
59 self
60 }
61
62 pub fn global_opts(mut self, global_opts: GlobalOpts) -> Self {
64 self.set_global_opts(global_opts);
65 self
66 }
67
68 pub fn get_bin(&self) -> &Path {
70 &self.bin
71 }
72
73 pub fn get_global_opts(&self) -> &GlobalOpts {
75 &self.global_opts
76 }
77
78 pub fn set_bin(&mut self, bin: impl Into<PathBuf>) {
80 self.bin = bin.into();
81 }
82
83 pub fn set_global_opts(&mut self, global_opts: GlobalOpts) {
85 self.global_opts = global_opts;
86 }
87
88 pub fn add(&self) -> Add {
90 Add::new(self.bin.clone(), self.global_opts.clone())
91 }
92
93 pub fn admin(&self) -> AdminEntry {
95 AdminEntry::new(self.bin.clone(), self.global_opts.clone())
96 }
97
98 #[cfg(not(feature = "lt2016_1"))]
100 pub fn aliases(&self) -> Aliases {
101 Aliases::new(self.bin.clone(), self.global_opts.clone())
102 }
103
104 pub fn annotate(&self) -> Annotate {
106 Annotate::new(self.bin.clone(), self.global_opts.clone())
107 }
108
109 pub fn archive(&self) -> Archive {
111 Archive::new(self.bin.clone(), self.global_opts.clone())
112 }
113
114 pub fn attribute(&self) -> Attribute {
116 Attribute::new(self.bin.clone(), self.global_opts.clone())
117 }
118
119 pub fn changes(&self) -> Changes {
121 Changes::new(self.bin.clone(), self.global_opts.clone())
122 }
123
124 pub fn changelists(&self) -> Changes {
129 Changes::new(self.bin.clone(), self.global_opts.clone())
130 }
131
132 pub fn describe(&self) -> Describe {
134 Describe::new(self.bin.clone(), self.global_opts.clone())
135 }
136
137 pub fn edit(&self) -> Edit {
139 Edit::new(self.bin.clone(), self.global_opts.clone())
140 }
141
142 pub fn filelog(&self) -> FileLog {
144 FileLog::new(self.bin.clone(), self.global_opts.clone())
145 }
146
147 pub fn print(&self) -> Print {
149 Print::new(self.bin.clone(), self.global_opts.clone())
150 }
151
152 pub fn sync(&self) -> Sync {
154 Sync::new(self.bin.clone(), self.global_opts.clone())
155 }
156
157 pub fn r#where(&self) -> Where {
159 Where::new(self.bin.clone(), self.global_opts.clone())
160 }
161}
162
163#[cfg(test)]
164mod tests {
165 use super::*;
166
167 #[test]
168 fn default_uses_p4_from_path() {
169 let p4 = P4Cli::default();
170
171 assert_eq!(p4.get_bin(), Path::new("p4"));
172 }
173
174 #[test]
175 fn accepts_custom_bin_path() {
176 let p4 = P4Cli::new("C:\\tools\\p4.exe", GlobalOpts::default());
177
178 assert_eq!(p4.get_bin(), Path::new("C:\\tools\\p4.exe"));
179 }
180}