use std::path::{Path, PathBuf};
use crate::cmd::Add;
use crate::cmd::AdminEntry;
#[cfg(not(feature = "lt2016_1"))]
use crate::cmd::Aliases;
use crate::cmd::Annotate;
use crate::cmd::Archive;
use crate::cmd::Attribute;
use crate::cmd::Changes;
use crate::cmd::Describe;
use crate::cmd::Edit;
use crate::cmd::FileLog;
use crate::cmd::Print;
use crate::cmd::Sync;
use crate::cmd::Where;
use crate::global::GlobalOpts;
pub mod cmd;
pub mod global;
pub mod spawn;
pub mod prelude {
pub use crate::cmd::*;
pub use crate::global::*;
pub use crate::spawn::*;
}
#[derive(Debug, Clone)]
pub struct P4Cli {
bin: PathBuf,
global_opts: GlobalOpts,
}
impl Default for P4Cli {
fn default() -> Self {
Self {
bin: PathBuf::from("p4"),
global_opts: GlobalOpts::default(),
}
}
}
impl P4Cli {
pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
Self {
bin: bin.into(),
global_opts,
}
}
pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
self.set_bin(bin);
self
}
pub fn global_opts(mut self, global_opts: GlobalOpts) -> Self {
self.set_global_opts(global_opts);
self
}
pub fn get_bin(&self) -> &Path {
&self.bin
}
pub fn get_global_opts(&self) -> &GlobalOpts {
&self.global_opts
}
pub fn set_bin(&mut self, bin: impl Into<PathBuf>) {
self.bin = bin.into();
}
pub fn set_global_opts(&mut self, global_opts: GlobalOpts) {
self.global_opts = global_opts;
}
pub fn add(&self) -> Add {
Add::new(self.bin.clone(), self.global_opts.clone())
}
pub fn admin(&self) -> AdminEntry {
AdminEntry::new(self.bin.clone(), self.global_opts.clone())
}
#[cfg(not(feature = "lt2016_1"))]
pub fn aliases(&self) -> Aliases {
Aliases::new(self.bin.clone(), self.global_opts.clone())
}
pub fn annotate(&self) -> Annotate {
Annotate::new(self.bin.clone(), self.global_opts.clone())
}
pub fn archive(&self) -> Archive {
Archive::new(self.bin.clone(), self.global_opts.clone())
}
pub fn attribute(&self) -> Attribute {
Attribute::new(self.bin.clone(), self.global_opts.clone())
}
pub fn changes(&self) -> Changes {
Changes::new(self.bin.clone(), self.global_opts.clone())
}
pub fn changelists(&self) -> Changes {
Changes::new(self.bin.clone(), self.global_opts.clone())
}
pub fn describe(&self) -> Describe {
Describe::new(self.bin.clone(), self.global_opts.clone())
}
pub fn edit(&self) -> Edit {
Edit::new(self.bin.clone(), self.global_opts.clone())
}
pub fn filelog(&self) -> FileLog {
FileLog::new(self.bin.clone(), self.global_opts.clone())
}
pub fn print(&self) -> Print {
Print::new(self.bin.clone(), self.global_opts.clone())
}
pub fn sync(&self) -> Sync {
Sync::new(self.bin.clone(), self.global_opts.clone())
}
pub fn r#where(&self) -> Where {
Where::new(self.bin.clone(), self.global_opts.clone())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_uses_p4_from_path() {
let p4 = P4Cli::default();
assert_eq!(p4.get_bin(), Path::new("p4"));
}
#[test]
fn accepts_custom_bin_path() {
let p4 = P4Cli::new("C:\\tools\\p4.exe", GlobalOpts::default());
assert_eq!(p4.get_bin(), Path::new("C:\\tools\\p4.exe"));
}
}