use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use super::{SubCommand, Unselected};
use crate::global::GlobalOpts;
use crate::spawn::ParameterizedSpawn;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Target {
AllServers,
Server(String),
Variable(String),
}
#[cfg_attr(
feature = "lt2019_2",
doc = "Set and manage server configuration variables."
)]
#[cfg_attr(
all(not(feature = "lt2019_2"), feature = "lt2024_1"),
doc = "Set, view, and manage server configuration variables."
)]
#[cfg_attr(
not(feature = "lt2024_1"),
doc = "Set, view, and manage server configuration variables, including comments."
)]
#[cfg_attr(not(feature = "lt2019_2"), doc = ", [`history`](Self::history)")]
#[cfg_attr(feature = "lt2023_2", doc = ".")]
#[cfg_attr(
not(feature = "lt2023_2"),
doc = ", [`patch`](Self::patch), or [`help`](Self::help)."
)]
#[derive(Debug, Clone, Default)]
pub struct Configure<T = Unselected> {
bin: PathBuf,
global_opts: GlobalOpts,
sub_command: T,
}
impl Configure<Unselected> {
pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
Self {
bin: bin.into(),
global_opts,
sub_command: Unselected,
}
}
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 set(
self,
variable: impl Into<String>,
value: impl Into<String>,
) -> Configure<SetVariable> {
Configure {
bin: self.bin,
global_opts: self.global_opts,
sub_command: SetVariable {
server_id: None,
variable: variable.into(),
value: value.into(),
..Default::default()
},
}
}
pub fn unset(self, variable: impl Into<String>) -> Configure<UnSetVariable> {
Configure {
bin: self.bin,
global_opts: self.global_opts,
sub_command: UnSetVariable {
server_id: None,
variable: variable.into(),
..Default::default()
},
}
}
pub fn show(self) -> Configure<Show> {
Configure {
bin: self.bin,
global_opts: self.global_opts,
sub_command: Show::default(),
}
}
#[cfg(not(feature = "lt2019_2"))]
pub fn history(self) -> Configure<History> {
Configure {
bin: self.bin,
global_opts: self.global_opts,
sub_command: History::default(),
}
}
#[cfg(not(feature = "lt2023_2"))]
pub fn patch(
self,
variable: impl Into<String>,
comment: impl Into<String>,
) -> Configure<Patch> {
Configure {
bin: self.bin,
global_opts: self.global_opts,
sub_command: Patch {
iteration: None,
comment: comment.into(),
variable: variable.into(),
},
}
}
#[cfg(not(feature = "lt2023_2"))]
pub fn help(self) -> Configure<Help> {
Configure {
bin: self.bin,
global_opts: self.global_opts,
sub_command: Help,
}
}
}
impl<T: SubCommand> Configure<T> {
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
}
fn spawn_piped(&mut self) -> Result<Child, std::io::Error> {
self.setup_command(&self.bin)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
}
}
impl<T: SubCommand> SubCommand for Configure<T> {
fn name(&self) -> &str {
"configure"
}
fn inject_local_args(&self, command: &mut Command) {
self.sub_command.inject_args(command);
}
fn global_opts(&self) -> Option<&GlobalOpts> {
Some(&self.global_opts)
}
}
#[cfg_attr(
feature = "lt2020_1",
doc = "p4 [g-opts] configure set [P4NAME#|serverid#]variable=value",
doc = "```"
)]
#[cfg_attr(
all(not(feature = "lt2020_1"), feature = "lt2023_2"),
doc = "p4 [g-opts] configure set [serverid#]variable=value",
doc = "```"
)]
#[cfg_attr(
not(feature = "lt2023_2"),
doc = "p4 [g-opts] configure set [--comment=comment] [serverid#]variable=value",
doc = "```"
)]
#[derive(Debug, Clone, Default)]
pub struct SetVariable {
server_id: Option<String>,
variable: String,
value: String,
#[cfg(not(feature = "lt2023_2"))]
comment: Option<String>,
}
impl Configure<SetVariable> {
pub fn get_server_id(&self) -> Option<&str> {
self.sub_command.server_id.as_deref()
}
pub fn set_server_id(&mut self, v: impl Into<String>) -> &mut Self {
self.sub_command.server_id = Some(v.into());
self
}
pub fn server_id(mut self, v: impl Into<String>) -> Self {
self.set_server_id(v);
self
}
pub fn get_variable(&self) -> &str {
&self.sub_command.variable
}
pub fn set_variable(&mut self, v: impl Into<String>) -> &mut Self {
self.sub_command.variable = v.into();
self
}
pub fn variable(mut self, v: impl Into<String>) -> Self {
self.set_variable(v);
self
}
pub fn get_value(&self) -> &str {
&self.sub_command.value
}
pub fn set_value(&mut self, v: impl Into<String>) -> &mut Self {
self.sub_command.value = v.into();
self
}
pub fn value(mut self, v: impl Into<String>) -> Self {
self.set_value(v);
self
}
#[cfg(not(feature = "lt2023_2"))]
pub fn get_comment(&self) -> Option<&str> {
self.sub_command.comment.as_deref()
}
#[cfg(not(feature = "lt2023_2"))]
pub fn set_comment(&mut self, v: impl Into<String>) -> &mut Self {
self.sub_command.comment = Some(v.into());
self
}
#[cfg(not(feature = "lt2023_2"))]
pub fn comment(mut self, v: impl Into<String>) -> Self {
self.set_comment(v);
self
}
}
impl SubCommand for SetVariable {
fn name(&self) -> &str {
"set"
}
fn inject_local_args(&self, command: &mut Command) {
#[cfg(not(feature = "lt2023_2"))]
{
if let Some(comment) = &self.comment {
command.arg(format!("--comment={}", comment));
}
}
let variable = match &self.server_id {
Some(server_id) => format!("{}#{}", server_id, self.variable),
None => self.variable.clone(),
};
command.arg(format!("{}={}", variable, self.value));
}
}
impl ParameterizedSpawn<()> for Configure<SetVariable> {
type Output = Child;
type Error = std::io::Error;
fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
self.spawn_piped()
}
}
#[cfg_attr(
feature = "lt2020_1",
doc = "p4 [g-opts] configure unset [P4NAME#|serverid#]variable",
doc = "```"
)]
#[cfg_attr(
all(not(feature = "lt2020_1"), feature = "lt2023_2"),
doc = "p4 [g-opts] configure unset [serverid#]variable",
doc = "```"
)]
#[cfg_attr(
not(feature = "lt2023_2"),
doc = "p4 [g-opts] configure unset [--comment=comment] [serverid#]variable",
doc = "```"
)]
#[derive(Debug, Clone, Default)]
pub struct UnSetVariable {
server_id: Option<String>,
variable: String,
#[cfg(not(feature = "lt2023_2"))]
comment: Option<String>,
}
impl Configure<UnSetVariable> {
pub fn get_server_id(&self) -> Option<&str> {
self.sub_command.server_id.as_deref()
}
pub fn set_server_id(&mut self, v: impl Into<String>) -> &mut Self {
self.sub_command.server_id = Some(v.into());
self
}
pub fn server_id(mut self, v: impl Into<String>) -> Self {
self.set_server_id(v);
self
}
pub fn get_variable(&self) -> &str {
&self.sub_command.variable
}
pub fn set_variable(&mut self, v: impl Into<String>) -> &mut Self {
self.sub_command.variable = v.into();
self
}
pub fn variable(mut self, v: impl Into<String>) -> Self {
self.set_variable(v);
self
}
#[cfg(not(feature = "lt2023_2"))]
pub fn get_comment(&self) -> Option<&str> {
self.sub_command.comment.as_deref()
}
#[cfg(not(feature = "lt2023_2"))]
pub fn set_comment(&mut self, v: impl Into<String>) -> &mut Self {
self.sub_command.comment = Some(v.into());
self
}
#[cfg(not(feature = "lt2023_2"))]
pub fn comment(mut self, v: impl Into<String>) -> Self {
self.set_comment(v);
self
}
}
impl SubCommand for UnSetVariable {
fn name(&self) -> &str {
"unset"
}
fn inject_local_args(&self, command: &mut Command) {
#[cfg(not(feature = "lt2023_2"))]
{
if let Some(comment) = &self.comment {
command.arg(format!("--comment={}", comment));
}
}
let variable = match &self.server_id {
Some(server_id) => format!("{}#{}", server_id, self.variable),
None => self.variable.clone(),
};
command.arg(variable);
}
}
impl ParameterizedSpawn<()> for Configure<UnSetVariable> {
type Output = Child;
type Error = std::io::Error;
fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
self.spawn_piped()
}
}
#[cfg_attr(
feature = "lt2020_1",
doc = "p4 [g-opts] configure show [allservers | P4NAME | variable]",
doc = "```"
)]
#[cfg_attr(
all(not(feature = "lt2020_1"), feature = "lt2023_2"),
doc = "p4 [g-opts] configure show [allservers | variable]",
doc = "```"
)]
#[cfg_attr(
not(feature = "lt2023_2"),
doc = "p4 [g-opts] configure show [allservers | serverid | variable]",
doc = "```"
)]
#[derive(Debug, Clone, Default)]
pub struct Show {
target: Option<Target>,
}
impl Configure<Show> {
pub fn get_target(&self) -> Option<&Target> {
self.sub_command.target.as_ref()
}
pub fn set_target(&mut self, target: Option<Target>) -> &mut Self {
self.sub_command.target = target;
self
}
pub fn target(mut self, target: Option<Target>) -> Self {
self.set_target(target);
self
}
pub fn allservers(mut self) -> Self {
self.sub_command.target = Some(Target::AllServers);
self
}
pub fn server(mut self, v: impl Into<String>) -> Self {
self.sub_command.target = Some(Target::Server(v.into()));
self
}
pub fn variable(mut self, v: impl Into<String>) -> Self {
self.sub_command.target = Some(Target::Variable(v.into()));
self
}
}
impl SubCommand for Show {
fn name(&self) -> &str {
"show"
}
fn inject_local_args(&self, command: &mut Command) {
match &self.target {
Some(Target::AllServers) => {
command.arg("allservers");
}
Some(Target::Server(name)) => {
command.arg(name);
}
Some(Target::Variable(name)) => {
command.arg(name);
}
None => {}
}
}
}
impl ParameterizedSpawn<()> for Configure<Show> {
type Output = Child;
type Error = std::io::Error;
fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
self.spawn_piped()
}
}
#[cfg_attr(
feature = "lt2020_1",
doc = "p4 [g-opts] configure history [allservers | P4NAME | variable]",
doc = "```"
)]
#[cfg_attr(
all(not(feature = "lt2020_1"), feature = "lt2023_2"),
doc = "p4 [g-opts] configure history [allservers | variable]",
doc = "```"
)]
#[cfg_attr(
not(feature = "lt2023_2"),
doc = "p4 [g-opts] configure history [allservers | serverid | variable]",
doc = "```"
)]
#[cfg(not(feature = "lt2019_2"))]
#[derive(Debug, Clone, Default)]
pub struct History {
target: Option<Target>,
}
#[cfg(not(feature = "lt2019_2"))]
impl Configure<History> {
pub fn get_target(&self) -> Option<&Target> {
self.sub_command.target.as_ref()
}
pub fn set_target(&mut self, target: Option<Target>) -> &mut Self {
self.sub_command.target = target;
self
}
pub fn target(mut self, target: Option<Target>) -> Self {
self.set_target(target);
self
}
pub fn allservers(mut self) -> Self {
self.sub_command.target = Some(Target::AllServers);
self
}
pub fn server(mut self, v: impl Into<String>) -> Self {
self.sub_command.target = Some(Target::Server(v.into()));
self
}
pub fn variable(mut self, v: impl Into<String>) -> Self {
self.sub_command.target = Some(Target::Variable(v.into()));
self
}
}
#[cfg(not(feature = "lt2019_2"))]
impl SubCommand for History {
fn name(&self) -> &str {
"history"
}
fn inject_local_args(&self, command: &mut Command) {
match &self.target {
Some(Target::AllServers) => {
command.arg("allservers");
}
Some(Target::Server(name)) => {
command.arg(name);
}
Some(Target::Variable(name)) => {
command.arg(name);
}
None => {}
}
}
}
#[cfg(not(feature = "lt2019_2"))]
impl ParameterizedSpawn<()> for Configure<History> {
type Output = Child;
type Error = std::io::Error;
fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
self.spawn_piped()
}
}
#[cfg(not(feature = "lt2023_2"))]
#[derive(Debug, Clone, Default)]
pub struct Patch {
iteration: Option<u64>,
comment: String,
variable: String,
}
#[cfg(not(feature = "lt2023_2"))]
impl Configure<Patch> {
pub fn get_iteration(&self) -> Option<u64> {
self.sub_command.iteration
}
pub fn set_iteration(&mut self, v: u64) -> &mut Self {
self.sub_command.iteration = Some(v);
self
}
pub fn iteration(mut self, v: u64) -> Self {
self.set_iteration(v);
self
}
pub fn get_comment(&self) -> &str {
&self.sub_command.comment
}
pub fn set_comment(&mut self, v: impl Into<String>) -> &mut Self {
self.sub_command.comment = v.into();
self
}
pub fn comment(mut self, v: impl Into<String>) -> Self {
self.set_comment(v);
self
}
pub fn get_variable(&self) -> &str {
&self.sub_command.variable
}
pub fn set_variable(&mut self, v: impl Into<String>) -> &mut Self {
self.sub_command.variable = v.into();
self
}
pub fn variable(mut self, v: impl Into<String>) -> Self {
self.set_variable(v);
self
}
}
#[cfg(not(feature = "lt2023_2"))]
impl SubCommand for Patch {
fn name(&self) -> &str {
"history"
}
fn inject_local_args(&self, command: &mut Command) {
if let Some(iteration) = self.iteration {
command.arg(format!("--iteration={}", iteration));
}
command
.arg(format!("--comment={}", self.comment))
.arg(&self.variable);
}
}
#[cfg(not(feature = "lt2023_2"))]
impl ParameterizedSpawn<()> for Configure<Patch> {
type Output = Child;
type Error = std::io::Error;
fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
self.spawn_piped()
}
}
#[cfg(not(feature = "lt2023_2"))]
#[derive(Debug, Clone, Default)]
pub struct Help;
#[cfg(not(feature = "lt2023_2"))]
impl SubCommand for Help {
fn name(&self) -> &str {
"help"
}
fn inject_local_args(&self, _command: &mut Command) {}
}
#[cfg(not(feature = "lt2023_2"))]
impl ParameterizedSpawn<()> for Configure<Help> {
type Output = Child;
type Error = std::io::Error;
fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
self.spawn_piped()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::cmd::args_of;
fn entry() -> Configure<Unselected> {
Configure::new("p4", GlobalOpts::default())
}
#[cfg(not(feature = "lt2023_2"))]
#[test]
fn set_injects_options_in_syntax_order() {
let command = entry()
.set("monitor", "2")
.server_id("edge1")
.comment("why");
assert_eq!(
args_of(&command.setup_command("p4")),
["configure", "set", "--comment=why", "edge1#monitor=2"]
);
}
#[test]
fn set_without_optional_parts() {
let command = entry().set("monitor", "2");
assert_eq!(
args_of(&command.setup_command("p4")),
["configure", "set", "monitor=2"]
);
}
#[test]
fn set_accessors_reflect_changes() {
let mut command = entry().set("monitor", "2");
command.set_variable("security").set_value("4");
assert_eq!(command.get_variable(), "security");
assert_eq!(command.get_value(), "4");
assert_eq!(command.get_server_id(), None);
command.set_server_id("edge1");
assert_eq!(command.get_server_id(), Some("edge1"));
}
#[cfg(not(feature = "lt2023_2"))]
#[test]
fn unset_injects_options_in_syntax_order() {
let command = entry()
.unset("net.parallel.max")
.server_id("edge1")
.comment("unused");
assert_eq!(
args_of(&command.setup_command("p4")),
[
"configure",
"unset",
"--comment=unused",
"edge1#net.parallel.max"
]
);
}
#[test]
fn show_target_variants() {
let bare = entry().show();
assert_eq!(args_of(&bare.setup_command("p4")), ["configure", "show"]);
assert_eq!(bare.get_target(), None);
let all = entry().show().allservers();
assert_eq!(
args_of(&all.setup_command("p4")),
["configure", "show", "allservers"]
);
let server = entry().show().server("paris-edge");
assert_eq!(
args_of(&server.setup_command("p4")),
["configure", "show", "paris-edge"]
);
let variable = entry().show().variable("monitor");
assert_eq!(
args_of(&variable.setup_command("p4")),
["configure", "show", "monitor"]
);
assert_eq!(
variable.get_target(),
Some(&Target::Variable("monitor".to_string()))
);
}
#[test]
fn show_set_target_roundtrip() {
let mut command = entry().show();
command.set_target(Some(Target::Server("headquarters".to_string())));
assert_eq!(
args_of(&command.setup_command("p4")),
["configure", "show", "headquarters"]
);
command.set_target(None);
assert_eq!(args_of(&command.setup_command("p4")), ["configure", "show"]);
}
#[cfg(not(feature = "lt2019_2"))]
#[test]
fn history_target_variants() {
let bare = entry().history();
assert_eq!(args_of(&bare.setup_command("p4")), ["configure", "history"]);
let all = entry().history().allservers();
assert_eq!(
args_of(&all.setup_command("p4")),
["configure", "history", "allservers"]
);
let variable = entry().history().variable("monitor");
assert_eq!(
args_of(&variable.setup_command("p4")),
["configure", "history", "monitor"]
);
}
#[cfg(not(feature = "lt2023_2"))]
#[test]
fn patch_injects_options_in_syntax_order() {
let command = entry().patch("server.allowfetch", "for DVCS").iteration(2);
assert_eq!(
args_of(&command.setup_command("p4")),
[
"configure",
"history",
"--iteration=2",
"--comment=for DVCS",
"server.allowfetch"
]
);
assert_eq!(command.get_iteration(), Some(2));
}
#[cfg(not(feature = "lt2023_2"))]
#[test]
fn patch_without_iteration() {
let command = entry().patch("server.allowfetch", "for DVCS");
assert_eq!(
args_of(&command.setup_command("p4")),
[
"configure",
"history",
"--comment=for DVCS",
"server.allowfetch"
]
);
}
#[cfg(not(feature = "lt2023_2"))]
#[test]
fn help_injects_no_local_arguments() {
let command = entry().help();
assert_eq!(args_of(&command.setup_command("p4")), ["configure", "help"]);
}
#[test]
fn global_opts_are_injected_before_the_subcommand() {
let mut global_opts = GlobalOpts::default();
global_opts.set_user("bruno");
let command = Configure::new("p4", global_opts).set("monitor", "2");
assert_eq!(
args_of(&command.setup_command("p4")),
["-u", "bruno", "configure", "set", "monitor=2"]
);
}
}