Skip to main content

perforce_cli/
lib.rs

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    /// Create a new Perforce command-line client.
50    ///
51    /// `bin` is the path to the Perforce command-line executable.
52    pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
53        Self {
54            bin: bin.into(),
55            global_opts,
56        }
57    }
58
59    /// Set the Perforce command path.
60    pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
61        self.set_bin(bin);
62        self
63    }
64
65    /// Set the global options.
66    pub fn global_opts(mut self, global_opts: GlobalOpts) -> Self {
67        self.set_global_opts(global_opts);
68        self
69    }
70
71    /// Get the Perforce command path.
72    pub fn get_bin(&self) -> &Path {
73        &self.bin
74    }
75
76    /// Get the global options.
77    pub fn get_global_opts(&self) -> &GlobalOpts {
78        &self.global_opts
79    }
80
81    /// Set the Perforce command path.
82    pub fn set_bin(&mut self, bin: impl Into<PathBuf>) {
83        self.bin = bin.into();
84    }
85
86    /// Set the global options.
87    pub fn set_global_opts(&mut self, global_opts: GlobalOpts) {
88        self.global_opts = global_opts;
89    }
90
91    /// Open files in a client workspace for addition to the depot.
92    pub fn add(&self) -> Add {
93        Add::new(self.bin.clone(), self.global_opts.clone())
94    }
95
96    /// Perform administrative operations on the server.
97    pub fn admin(&self) -> AdminEntry {
98        AdminEntry::new(self.bin.clone(), self.global_opts.clone())
99    }
100
101    /// Display command aliases that are currently defined in a .p4aliases file.
102    #[cfg(not(feature = "lt2016_1"))]
103    pub fn aliases(&self) -> Aliases {
104        Aliases::new(self.bin.clone(), self.global_opts.clone())
105    }
106
107    /// Print file lines along with their revisions.
108    pub fn annotate(&self) -> Annotate {
109        Annotate::new(self.bin.clone(), self.global_opts.clone())
110    }
111
112    /// Archive obsolete revisions to an archive depot.
113    pub fn archive(&self) -> Archive {
114        Archive::new(self.bin.clone(), self.global_opts.clone())
115    }
116
117    /// Set per-revision attributes on file revisions.
118    pub fn attribute(&self) -> Attribute {
119        Attribute::new(self.bin.clone(), self.global_opts.clone())
120    }
121
122    /// Create or edit a changelist specification.
123    pub fn change(&self) -> Change {
124        Change::new(self.bin.clone(), self.global_opts.clone())
125    }
126
127    /// Create or edit a changelist specification.
128    ///
129    /// This is an alias for [`Self::change`], corresponding to the `p4
130    /// changelist` command.
131    pub fn changelist(&self) -> Change {
132        Change::new(self.bin.clone(), self.global_opts.clone())
133    }
134
135    /// List submitted and pending changelists.
136    pub fn changes(&self) -> Changes {
137        Changes::new(self.bin.clone(), self.global_opts.clone())
138    }
139
140    /// List submitted and pending changelists.
141    ///
142    /// This is an alias for [`Self::changes`], corresponding to the `p4
143    /// changelists` command.
144    pub fn changelists(&self) -> Changes {
145        Changes::new(self.bin.clone(), self.global_opts.clone())
146    }
147
148    /// Display the details of one or more changelists.
149    pub fn describe(&self) -> Describe {
150        Describe::new(self.bin.clone(), self.global_opts.clone())
151    }
152
153    /// Diff workspace content against depot content, or diff stream specs.
154    pub fn diff(&self) -> Diff {
155        Diff::new(self.bin.clone(), self.global_opts.clone())
156    }
157
158    /// Compare the content at two depot paths, or compare two stream specs.
159    pub fn diff2(&self) -> Diff2 {
160        Diff2::new(self.bin.clone(), self.global_opts.clone())
161    }
162
163    /// Open files in a client workspace for edit.
164    pub fn edit(&self) -> Edit {
165        Edit::new(self.bin.clone(), self.global_opts.clone())
166    }
167
168    /// Print detailed information about the revisions of files.
169    pub fn filelog(&self) -> FileLog {
170        FileLog::new(self.bin.clone(), self.global_opts.clone())
171    }
172
173    /// Print the contents of depot file revisions.
174    pub fn print(&self) -> Print {
175        Print::new(self.bin.clone(), self.global_opts.clone())
176    }
177
178    /// Update the client workspace to reflect the contents of the depot.
179    pub fn sync(&self) -> Sync {
180        Sync::new(self.bin.clone(), self.global_opts.clone())
181    }
182
183    /// Show where a particular file is located, as determined by the client view.
184    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}