use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use super::{ExclusiveOption, SubCommand, Unselected};
use crate::global::GlobalOpts;
use crate::spawn::ParameterizedSpawn;
#[derive(Debug, Clone, Copy, Default)]
pub struct Format {
format_non_text: bool,
limit: Option<u64>,
}
#[derive(Debug, Clone, Copy, Default)]
pub struct RawJournal;
#[derive(Debug, Clone, Default)]
pub struct CheckPoint<M = Unselected> {
token: String,
mode: M,
}
#[derive(Debug, Clone, Default)]
pub struct Journal<M = Unselected> {
token: String,
mode: M,
}
#[cfg_attr(
feature = "lt2017_1",
doc = "This command reports checkpoint and journal metadata from a Perforce server. With no options, the records are reported in tagged form."
)]
#[cfg_attr(
all(feature = "lt2017_2", not(feature = "lt2017_1")),
doc = "This command reports checkpoint and journal metadata from a Helix Core server. With no options, the records are reported in tagged form."
)]
#[cfg_attr(
all(feature = "lt2019_1", not(feature = "lt2017_2")),
doc = "This command reports checkpoint and journal metadata from a Helix Server server. With no options, the records are reported in tagged form."
)]
#[cfg_attr(
all(feature = "lt2021_1", not(feature = "lt2019_1")),
doc = "This command reports checkpoint and journal metadata from a Helix server server. With no options, the records are reported in tagged form."
)]
#[cfg_attr(
all(feature = "lt2024_1", not(feature = "lt2021_1")),
doc = "This command reports checkpoint and journal metadata from a Helix Server server. With no options, the records are reported in tagged form."
)]
#[cfg_attr(
all(feature = "lt2024_2", not(feature = "lt2024_1")),
doc = "This command reports checkpoint and journal metadata from a Helix Core Server server. With no options, the records are reported in tagged form."
)]
#[cfg_attr(
not(feature = "lt2024_2"),
doc = "This command reports checkpoint and journal metadata from a P4 Server server. With no options, the records are reported in tagged form."
)]
#[cfg_attr(
feature = "lt2014_2",
doc = "To filter database tables out of the exported data, use the `-T` flag with a list of tables whose data you wish to exclude. To exclude data from multiple tables, separate the table names by spaces or commas. The table names must begin with \"db.\", following the naming convention used for database files in the server root directory. If you separate the table exclusion list with spaces, you must enclose the list in quotes."
)]
#[cfg_attr(
not(feature = "lt2014_2"),
doc = "To filter database tables out of the exported data, use the `-T` option with a list of tables whose data you wish to exclude. To exclude data from multiple tables, separate the table names by spaces or commas. The table names must begin with \"db.\", following the naming convention used for database files in the server root directory. If you separate the table exclusion list with spaces, you must enclose the list in quotes."
)]
#[cfg_attr(feature = "lt2024_1", doc = "# Usage Notes")]
#[cfg_attr(not(feature = "lt2024_1"), doc = "# Usage notes")]
#[cfg_attr(not(feature = "lt2022_2"), doc = "available to an service user")]
#[derive(Debug, Clone, Default)]
pub struct Export<M = Unselected> {
bin: PathBuf,
global_opts: GlobalOpts,
prefix: Option<String>,
filter: Option<String>,
excluded_tables: Option<Vec<String>>,
filter_patterns: Option<Vec<String>>,
mode: M,
}
impl Export<Unselected> {
pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
Self {
bin: bin.into(),
global_opts,
prefix: None,
filter: None,
excluded_tables: None,
filter_patterns: None,
mode: Unselected,
}
}
pub fn checkpoint(self, token: impl Into<String>) -> Export<CheckPoint<Unselected>> {
self.map_mode(|mode| CheckPoint {
token: token.into(),
mode,
})
}
pub fn journal(self, token: impl Into<String>) -> Export<Journal<Unselected>> {
self.map_mode(|mode| Journal {
token: token.into(),
mode,
})
}
pub fn format_non_text(self) -> Export<Format> {
self.with_mode(Format {
format_non_text: true,
limit: None,
})
}
pub fn limit(self, lines: u64) -> Export<Format> {
self.with_mode(Format {
format_non_text: false,
limit: Some(lines),
})
}
pub fn raw_journal(self) -> Export<RawJournal> {
self.with_mode(RawJournal)
}
}
impl Export<Format> {
pub fn get_format_non_text(&self) -> bool {
self.mode.format_non_text
}
pub fn set_format_non_text(&mut self, v: bool) -> &mut Self {
self.mode.format_non_text = v;
self
}
pub fn format_non_text(mut self, v: bool) -> Self {
self.mode.format_non_text = v;
self
}
pub fn get_limit(&self) -> Option<u64> {
self.mode.limit
}
pub fn set_limit(&mut self, v: Option<u64>) -> &mut Self {
self.mode.limit = v;
self
}
pub fn limit(mut self, v: u64) -> Self {
self.mode.limit = Some(v);
self
}
pub fn checkpoint(self, token: impl Into<String>) -> Export<CheckPoint<Format>> {
self.map_mode(|mode| CheckPoint {
token: token.into(),
mode,
})
}
pub fn journal(self, token: impl Into<String>) -> Export<Journal<Format>> {
self.map_mode(|mode| Journal {
token: token.into(),
mode,
})
}
}
impl Export<RawJournal> {
pub fn journal(self, token: impl Into<String>) -> Export<Journal<RawJournal>> {
self.map_mode(|mode| Journal {
token: token.into(),
mode,
})
}
}
impl<M> Export<M> {
fn with_mode<N>(self, mode: N) -> Export<N> {
Export {
bin: self.bin,
global_opts: self.global_opts,
prefix: self.prefix,
filter: self.filter,
excluded_tables: self.excluded_tables,
filter_patterns: self.filter_patterns,
mode,
}
}
fn map_mode<N>(self, f: impl FnOnce(M) -> N) -> Export<N> {
Export {
bin: self.bin,
global_opts: self.global_opts,
prefix: self.prefix,
filter: self.filter,
excluded_tables: self.excluded_tables,
filter_patterns: self.filter_patterns,
mode: f(self.mode),
}
}
pub fn get_bin(&self) -> &Path {
&self.bin
}
pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
self.bin = bin.into();
self
}
pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
self.set_bin(bin);
self
}
pub fn get_prefix(&self) -> Option<&str> {
self.prefix.as_deref()
}
pub fn set_prefix(&mut self, v: impl Into<String>) -> &mut Self {
self.prefix = Some(v.into());
self
}
pub fn prefix(mut self, v: impl Into<String>) -> Self {
self.set_prefix(v);
self
}
pub fn get_filter(&self) -> Option<&str> {
self.filter.as_deref()
}
pub fn set_filter(&mut self, v: impl Into<String>) -> &mut Self {
self.filter = Some(v.into());
self
}
pub fn filter(mut self, v: impl Into<String>) -> Self {
self.set_filter(v);
self
}
pub fn get_excluded_tables(&self) -> Option<&[String]> {
self.excluded_tables.as_deref()
}
pub fn set_excluded_tables(&mut self, v: Vec<String>) -> &mut Self {
self.excluded_tables = Some(v);
self
}
pub fn excluded_tables(mut self, v: Vec<String>) -> Self {
self.set_excluded_tables(v);
self
}
#[cfg_attr(
feature = "lt2014_2",
doc = "Multiple filter patterns can be specified with multiple `-P` flags."
)]
#[cfg_attr(
not(feature = "lt2014_2"),
doc = "Multiple filter patterns can be specified with multiple `-P` options."
)]
#[cfg_attr(
feature = "lt2024_1",
doc = "The mechanism by which this filtering is implemented is the same as that which is used by the `ClientDataFilter:` and `RevisionDataFilter:` fields in the `p4 server` form."
)]
#[cfg_attr(
not(feature = "lt2024_1"),
doc = "The mechanism by which this filtering is implemented is the same as that which is used by the `ClientDataFilter:` and `RevisionDataFilter` fields in the `p4 server` form."
)]
pub fn get_filter_patterns(&self) -> Option<&[String]> {
self.filter_patterns.as_deref()
}
pub fn set_filter_pattern(&mut self, v: impl Into<String>) -> &mut Self {
self.filter_patterns
.get_or_insert_with(Vec::new)
.push(v.into());
self
}
pub fn filter_pattern(mut self, v: impl Into<String>) -> Self {
self.set_filter_pattern(v);
self
}
}
impl<M> Export<CheckPoint<M>> {
pub fn get_token(&self) -> &str {
&self.mode.token
}
pub fn set_token(&mut self, v: impl Into<String>) -> &mut Self {
self.mode.token = v.into();
self
}
pub fn token(mut self, v: impl Into<String>) -> Self {
self.set_token(v);
self
}
}
impl<M> Export<Journal<M>> {
pub fn get_token(&self) -> &str {
&self.mode.token
}
pub fn set_token(&mut self, v: impl Into<String>) -> &mut Self {
self.mode.token = v.into();
self
}
pub fn token(mut self, v: impl Into<String>) -> Self {
self.set_token(v);
self
}
}
impl ExclusiveOption for Format {
fn inject_args(&self, command: &mut Command) {
if self.format_non_text {
command.arg("-f");
}
if let Some(lines) = self.limit {
command.arg("-l").arg(lines.to_string());
}
}
}
impl ExclusiveOption for RawJournal {
fn inject_args(&self, command: &mut Command) {
command.arg("-r");
}
}
impl<M: ExclusiveOption> ExclusiveOption for CheckPoint<M> {
fn inject_args(&self, command: &mut Command) {
command.arg("-c").arg(&self.token);
self.mode.inject_args(command);
}
}
impl<M: ExclusiveOption> ExclusiveOption for Journal<M> {
fn inject_args(&self, command: &mut Command) {
command.arg("-j").arg(&self.token);
self.mode.inject_args(command);
}
}
impl<M: ExclusiveOption> SubCommand for Export<M> {
fn name(&self) -> &str {
"export"
}
fn inject_local_args(&self, command: &mut Command) {
self.mode.inject_args(command);
if let Some(prefix) = &self.prefix {
command.arg("-J").arg(prefix);
}
if let Some(filter) = &self.filter {
command.arg("-F").arg(filter);
}
if let Some(tables) = &self.excluded_tables {
command.arg("-T").arg(tables.join(","));
}
if let Some(patterns) = &self.filter_patterns {
for pattern in patterns {
command.arg("-P").arg(pattern);
}
}
}
fn global_opts(&self) -> Option<&GlobalOpts> {
Some(&self.global_opts)
}
}
impl<M: ExclusiveOption> ParameterizedSpawn<()> for Export<CheckPoint<M>> {
type Output = Child;
type Error = std::io::Error;
fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
self.setup_command(&self.bin)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
}
}
impl<M: ExclusiveOption> ParameterizedSpawn<()> for Export<Journal<M>> {
type Output = Child;
type Error = std::io::Error;
fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
self.setup_command(&self.bin)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
}
}
impl<M: ExclusiveOption> Export<M> {
#[cfg_attr(
feature = "lt2014_2",
doc = "See the [Global Options](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2015_1", not(feature = "lt2014_2")),
doc = "See the [“Global Options”](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2017_1", not(feature = "lt2015_1")),
doc = "See [“Global Options”](GlobalOpts)."
)]
#[cfg_attr(
all(feature = "lt2018_2", not(feature = "lt2017_1")),
doc = "See [Global Options](GlobalOpts)."
)]
#[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
pub fn get_global_opts(&self) -> &GlobalOpts {
&self.global_opts
}
#[cfg_attr(
feature = "lt2014_2",
doc = "See the [Global Options](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2015_1", not(feature = "lt2014_2")),
doc = "See the [“Global Options”](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2017_1", not(feature = "lt2015_1")),
doc = "See [“Global Options”](GlobalOpts)."
)]
#[cfg_attr(
all(feature = "lt2018_2", not(feature = "lt2017_1")),
doc = "See [Global Options](GlobalOpts)."
)]
#[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
self.global_opts = v;
self
}
#[cfg_attr(
feature = "lt2014_2",
doc = "See the [Global Options](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2015_1", not(feature = "lt2014_2")),
doc = "See the [“Global Options”](GlobalOpts) section."
)]
#[cfg_attr(
all(feature = "lt2017_1", not(feature = "lt2015_1")),
doc = "See [“Global Options”](GlobalOpts)."
)]
#[cfg_attr(
all(feature = "lt2018_2", not(feature = "lt2017_1")),
doc = "See [Global Options](GlobalOpts)."
)]
#[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
pub fn global_opts(mut self, v: GlobalOpts) -> Self {
self.set_global_opts(v);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::cmd::args_of;
fn args(cmd: &impl SubCommand) -> Vec<String> {
args_of(&cmd.setup_command("p4"))
}
#[test]
fn without_selection_has_no_options() {
let cmd = Export::new("p4", GlobalOpts::new());
assert_eq!(args(&cmd), ["export"]);
}
#[test]
fn format_state_injects_flags_before_selection() {
assert_eq!(
args(&Export::new("p4", GlobalOpts::new()).format_non_text()),
["export", "-f"]
);
assert_eq!(
args(&Export::new("p4", GlobalOpts::new()).limit(12)),
["export", "-l", "12"]
);
}
#[test]
fn raw_journal_state_injects_flag_before_selection() {
assert_eq!(
args(&Export::new("p4", GlobalOpts::new()).raw_journal()),
["export", "-r"]
);
}
#[test]
fn bare_checkpoint_and_journal() {
assert_eq!(
args(&Export::new("p4", GlobalOpts::new()).checkpoint("3")),
["export", "-c", "3"]
);
assert_eq!(
args(&Export::new("p4", GlobalOpts::new()).journal("7")),
["export", "-j", "7"]
);
}
#[test]
fn checkpoint_with_all_options() {
let cmd = Export::new("p4", GlobalOpts::new())
.prefix("backup")
.format_non_text()
.limit(10)
.filter("table = db.configure")
.excluded_tables(vec!["db.have".to_string(), "db.client".to_string()])
.filter_pattern("ic://client/depot/main")
.filter_pattern("xf://depot/secret")
.checkpoint("3");
assert_eq!(
args(&cmd),
[
"export",
"-c",
"3",
"-f",
"-l",
"10",
"-J",
"backup",
"-F",
"table = db.configure",
"-T",
"db.have,db.client",
"-P",
"ic://client/depot/main",
"-P",
"xf://depot/secret",
]
);
}
#[test]
fn raw_journal_with_options() {
let cmd = Export::new("p4", GlobalOpts::new())
.prefix("backup")
.excluded_tables(vec!["db.have".to_string()])
.filter_pattern("if://depot/main")
.raw_journal()
.journal("7");
assert_eq!(
args(&cmd),
[
"export",
"-j",
"7",
"-r",
"-J",
"backup",
"-T",
"db.have",
"-P",
"if://depot/main",
]
);
}
#[test]
fn global_opts_are_injected_once() {
let cmd = Export::new(
"p4",
GlobalOpts::new().port("localhost:1666").quiet_mode(true),
)
.journal("7");
assert_eq!(
args(&cmd),
["-p", "localhost:1666", "-q", "export", "-j", "7"]
);
}
#[test]
fn token_accessors_update_selection() {
let mut cmd = Export::new("p4", GlobalOpts::new()).checkpoint("3");
assert_eq!(cmd.get_token(), "3");
cmd.set_token("12#4");
assert_eq!(cmd.get_token(), "12#4");
assert_eq!(args(&cmd), ["export", "-c", "12#4"]);
let cmd = cmd.token("5");
assert_eq!(args(&cmd), ["export", "-c", "5"]);
}
#[test]
fn format_accessors_update_options() {
let mut cmd = Export::new("p4", GlobalOpts::new()).format_non_text();
assert!(cmd.get_format_non_text());
assert_eq!(cmd.get_limit(), None);
cmd.set_format_non_text(false);
cmd.set_limit(Some(5));
assert!(!cmd.get_format_non_text());
assert_eq!(cmd.get_limit(), Some(5));
assert_eq!(args(&cmd), ["export", "-l", "5"]);
let cmd = cmd.limit(9).format_non_text(true);
assert_eq!(args(&cmd), ["export", "-f", "-l", "9"]);
}
#[test]
fn bin_accessors_update_executable_path() {
let mut cmd = Export::new("p4", GlobalOpts::new()).journal("7");
assert_eq!(cmd.get_bin(), Path::new("p4"));
cmd.set_bin("/usr/local/bin/p4");
assert_eq!(cmd.get_bin(), Path::new("/usr/local/bin/p4"));
let cmd = cmd.bin("p4.exe");
assert_eq!(cmd.get_bin(), Path::new("p4.exe"));
}
}