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