Skip to main content

perforce_cli/cmd/
extension.rs

1#[cfg(not(feature = "lt2020_1"))]
2use std::ffi::OsStr;
3use std::path::{Path, PathBuf};
4use std::process::{Child, Command, Stdio};
5
6use super::{ExclusiveOption, SubCommand, Unselected};
7use crate::global::GlobalOpts;
8use crate::spawn::ParameterizedSpawn;
9
10// ---------------------------------------------------------------------------
11// ExtensionEntry
12// ---------------------------------------------------------------------------
13
14/// Entry point of `p4 extension`, holding the binary and global options.
15///
16/// # Description
17///
18/// The following operations are available: [`sample`](Self::sample),
19/// [`package`](Self::package), [`install`](Self::install),
20/// [`delete`](Self::delete), [`configure`](Self::configure),
21#[cfg_attr(not(feature = "lt2020_1"), doc = " [`run`](Self::run),")]
22/// or [`list`](Self::list).
23///
24#[cfg_attr(
25    feature = "lt2020_1",
26    doc = "`p4 extension` manages the installation, versioning, and configuration of Helix Core Server Extensions. This command requires super access granted by p4 protect.",
27    doc = "",
28    doc = "For server extensions, see p4 help serverextensionintro and p4 help serverextensiondev. See also Helix Core Extensions Developer Guide."
29)]
30#[cfg_attr(
31    all(feature = "lt2020_2", not(feature = "lt2020_1")),
32    doc = "`p4 extension` manages the installation, versioning, and configuration of Helix Core Server Extensions.",
33    doc = "",
34    doc = "Extensions provide a way to customize the behavior of the Helix Core Server with user-supplied logic. Extensions are self-contained packages of third-party code and assets run within the Helix Core Server. Extensions can be used for many use cases, such as change submission validation, form validation, external authentication, external job fix integration, external archive integration, and command policies. For a list of events that Extensions can register for, see the list of trigger types in the output of p4 help triggers.",
35    doc = "",
36    doc = "Extensions coexist with triggers (see p4 triggers), but offer more functionality. The Helix Core Server runs Extensions natively, without relying on external processes, so Extensions provide a portable, versioned runtime with automatic replication and a programmatic API.",
37    doc = "",
38    doc = "Extensions are versioned within the Helix Core Server and are stored in a special depot, named .p4-extensions by default. The extension depot is only accessible to the super user and by read-only commands. The extension depot is created automatically.",
39    doc = "",
40    doc = "Installing or upgrading an Extension creates a changelist with information about pre- and post-install versions.",
41    doc = "",
42    doc = "For more information about Extensions, see p4 help serverextensionintro and Helix Core Extensions Developer Guide."
43)]
44#[cfg_attr(
45    all(feature = "lt2025_1", not(feature = "lt2020_2")),
46    doc = "`p4 extension` manages the installation, versioning, and configuration of Helix Core Server Extensions.",
47    doc = "",
48    doc = "Extensions are a means for customizing parts of the Helix Core with user-supplied logic. Extensions are self-contained packages of third-party code and assets run within the Helix Core Server, used for change submission validation, form validation, external authentication, external job fix integration, external archive integration, and command policies. For a list of events that Extensions can register for, see the list of trigger types in the output of p4 help triggers.",
49    doc = "",
50    doc = "The Helix Core Server runs Extensions natively, without relying on external processes, so it can provide a portable, versioned runtime with automatic replication and a programmatic API.",
51    doc = "",
52    doc = "Extensions coexist with triggers (see p4 triggers), but Extensions offer more functionality.",
53    doc = "",
54    doc = "Extensions are versioned within the Helix Core Server and are stored in a special depot, named .p4-extensions by default. The extension depot is only accessible to the super user and by read-only commands. The extension depot is created automatically.",
55    doc = "",
56    doc = "Installing or upgrading an Extension creates a changelist with information about pre- and post-install versions.",
57    doc = "",
58    doc = "The p4 extension command requires super access granted by p4 protect. Users who are members of groups specified in the ExtAllowedGroups field of an Extension's global configuration and who are either a depot or repo owner can create or configure instances of file-based Extensions to operate on the sections of the server they manage.",
59    doc = ""
60)]
61#[cfg_attr(
62    all(feature = "lt2024_2", not(feature = "lt2020_2")),
63    doc = "For more information about Extensions, see p4 help serverextensionintro and Helix Core Extensions Developer Guide."
64)]
65#[cfg_attr(
66    all(feature = "lt2025_1", not(feature = "lt2024_2")),
67    doc = "For more information about Extensions, see p4 help serverextensionintro and P4 Extensions Documentation."
68)]
69#[cfg_attr(
70    not(feature = "lt2025_1"),
71    doc = "Create and configure P4 Extensions.",
72    doc = "",
73    doc = "Extensions are a means for customizing parts of the P4 with user-supplied logic. Extensions are self-contained packages of third-party code and assets that run within the P4 Server. Typical use cases are change submission validation, form validation, external authentication, external job fix integration, external archive integration, and command policies.",
74    doc = "",
75    doc = "An Extension can implement custom commands that a user can run. For example, `p4 extension --run myInstanceConfig validateFileSize pathToFile` where validateFileSize represents a custom command and pathToFile represents an argument that is relevant to the custom command.",
76    doc = "",
77    doc = "Extensions coexist with p4 triggers, but offer more functionality. To learn more, see Advantages over triggers in the Extension overview at P4 Server Administration Documentation.",
78    doc = "",
79    doc = "The P4 Server runs extensions natively, without relying on external processes. Extensions run in a portable, versioned runtime with automatic replication and a programmatic API, which is documented at P4 Server Administration Documentation.",
80    doc = "",
81    doc = "The p4 extension command requires super access granted by p4 protect. Users who are members of groups specified in the ExtAllowedGroups field of an extension's global configuration and who are either a depot or repo owner can create or configure instances of file-based extensions to operate on the sections of the server they manage.",
82    doc = "",
83    doc = "Extensions are versioned within the P4 Server and are stored in the built-in .p4-extensions depot. The extension depot is only accessible to the super user and by read-only commands.",
84    doc = "",
85    doc = "Installing or upgrading an extension creates a changelist with information about pre/post install versions. Multiple revisions of an extension can be active at any given time and can be referenced by their revision number."
86)]
87pub struct ExtensionEntry {
88    bin: PathBuf,
89
90    global_opts: GlobalOpts,
91}
92
93impl ExtensionEntry {
94    /// Creates an `ExtensionEntry` with the given p4 binary path and global
95    /// options.
96    pub(crate) fn new(bin: PathBuf, global_opts: GlobalOpts) -> Self {
97        Self { bin, global_opts }
98    }
99
100    /// # Description
101    ///
102    /// --sample name
103    ///
104    #[cfg_attr(
105        feature = "lt2020_1",
106        doc = "Create a skeleton of a new Extension in a directory.  Pass it the name of the Extension to be created."
107    )]
108    #[cfg_attr(
109        all(feature = "lt2020_2", not(feature = "lt2020_1")),
110        doc = "Creates a skeleton of a new Extension.  The argument is the name of the Extension to create as well as the name of the local directory where the files are placed.  The sample Extension has placeholder values that should be replaced when developing the code. (See Naming rules.)"
111    )]
112    #[cfg_attr(
113        all(feature = "lt2022_2", not(feature = "lt2020_2")),
114        doc = "Create a skeleton of a new Extension in a directory.  Pass it the name of the Extension to be created. (See Naming rules.) `p4 extension --sample myExt` This allows you to then use the `--package` option."
115    )]
116    #[cfg_attr(
117        not(feature = "lt2022_2"),
118        doc = "Create a skeleton of a new Extension in a directory.  Pass it the name of the Extension to be created. (See Naming rules.) `p4 extension --sample ExtName` This allows you to then use the `--package` option."
119    )]
120    pub fn sample(self, name: impl Into<String>) -> Extension<Sample> {
121        Extension {
122            bin: self.bin,
123            global_opts: self.global_opts,
124            op: Sample { name: name.into() },
125        }
126    }
127
128    /// # Description
129    ///
130    /// --package dir
131    ///
132    #[cfg_attr(
133        feature = "lt2020_1",
134        doc = "The `--package` flag creates a packaged Extension file from the named directory of files.  The resulting file has the same name as the directory, but with the .p4-extension suffix."
135    )]
136    #[cfg_attr(
137        all(feature = "lt2020_2", not(feature = "lt2020_1")),
138        doc = "Creates a packaged Extension file from the named directory of files, preparing it for `--install` on the server.  The resulting file has the same name as the directory, but with the .p4-extension suffix. Packaging is done client-side and requires a version 2020.1 or later command-line client."
139    )]
140    #[cfg_attr(
141        all(feature = "lt2022_2", not(feature = "lt2020_2")),
142        doc = "The `--package` option creates a packaged Extension file from the named directory of files.  For example, `p4 extension --package myExt` The resulting file has the same name as the directory, but with the .p4-extension suffix. An Extension must be packaged before it can be installed by using the `--install` option."
143    )]
144    #[cfg_attr(
145        not(feature = "lt2022_2"),
146        doc = "The `--package` option creates a packaged Extension file from the named directory of files.  For example, `p4 extension --package ExtName` The resulting file has the same name as the directory, but with the .p4-extension suffix. An Extension must be packaged before it can be installed by using the `--install` option."
147    )]
148    pub fn package(self, dir: impl Into<String>) -> Extension<Package> {
149        Extension {
150            bin: self.bin,
151            global_opts: self.global_opts,
152            op: Package {
153                dir: dir.into(),
154                sign: None,
155            },
156        }
157    }
158
159    /// # Description
160    ///
161    /// --install file
162    ///
163    #[cfg_attr(
164        feature = "lt2020_1",
165        doc = "The full name of an Extension file on the client. The command installs it server-side. Packaged Extensions have the p4-extension file suffix."
166    )]
167    #[cfg_attr(
168        all(feature = "lt2020_2", not(feature = "lt2020_1")),
169        doc = "The `--install` flag is passed the full name of a packaged Extension file residing on the client and installs it server-side, where it can then be instantiated with `--configure`."
170    )]
171    #[cfg_attr(
172        all(feature = "lt2022_2", not(feature = "lt2020_2")),
173        doc = "The full name of an Extension file on the client. The command installs it server-side. Packaged Extensions have the p4-extension file suffix. For example, `p4 extension --install myExt.p4-extension --yes`",
174        doc = "",
175        doc = "# Note",
176        doc = "",
177        doc = "To actually perform the install or delete, the `--yes` option is required. Without `--yes`, the p4 extension command merely reports what it would do without actually performing the install or delete."
178    )]
179    #[cfg_attr(
180        not(feature = "lt2022_2"),
181        doc = "The full name of an Extension file on the client. The command installs it server-side. Packaged Extensions have the p4-extension file suffix. For example, `p4 extension --install ExtName.p4-extension --yes`",
182        doc = "",
183        doc = "# Note",
184        doc = "",
185        doc = "To actually perform the install or delete, the `--yes` option is required. Without `--yes`, the p4 extension command merely reports what it would do without actually performing the install or delete."
186    )]
187    pub fn install(self, file: impl Into<String>) -> Extension<Install<Unselected>> {
188        Extension {
189            bin: self.bin,
190            global_opts: self.global_opts,
191            op: Install {
192                file: file.into(),
193                mode: Unselected,
194            },
195        }
196    }
197
198    /// # Description
199    ///
200    /// --delete extFQN
201    ///
202    #[cfg_attr(
203        feature = "lt2020_1",
204        doc = "Deletes an extension, its configurations, or both. To remove one specific revision and its instances, include the `--revision` option. To remove only the named instance config, include the `--name` option. To delete file-based instances by the part of the depot they affect, include the `--path` option. To remove all revisions of an extension and its configurations, omit the `--name` option."
205    )]
206    #[cfg_attr(
207        all(feature = "lt2020_2", not(feature = "lt2020_1")),
208        doc = "Deletes an extension, its configurations, or both. To remove the named instance config, include the `--name` option. For example: `p4 extension --delete myCompany::fileSizeCheck --name=fileSizeCheck-instance --yes` To remove all revisions of an extension and its configurations, omit the `--name` option. For example: `p4 extension --delete myCompany::fileSizeCheck` To remove one specific revision and its instances, include the `--revision` option. For example: `p4 extension --delete myCompany::fileSizeCheck --name=fileSizeCheck-instance --revision=2 --yes` To delete file-based instances registered under the specified path, use the `--path` option. For example: `p4 extension --delete ExampleInc::extD1 --path=//depot/... --yes`"
209    )]
210    #[cfg_attr(
211        all(feature = "lt2022_2", not(feature = "lt2020_2")),
212        doc = "Deletes an extension, its configurations, or both. To remove the named instance config, include the `--name` option. For example: `p4 extension --delete myCompany::fileSizeCheck --name=fileSizeCheck-instance --yes` To remove all revisions of an extension and its configurations, omit the `--name` option. For example: `p4 extension --delete myCompany::fileSizeCheck` To remove one specific revision and its instances, include the `--revision` option. For example: `p4 extension --delete myCompany::fileSizeCheck --name=fileSizeCheck-instance --revision=2 --yes` To delete file-based instances registered under the specified path, use the `--path` option. For example: `p4 extension --delete ExampleInc::extD1 --path=//depot/... --yes`",
213        doc = "",
214        doc = "# Note",
215        doc = "",
216        doc = "To actually perform the install or delete, the `--yes` option is required. Without `--yes`, the p4 extension command merely reports what it would do without actually performing the install or delete."
217    )]
218    #[cfg_attr(
219        all(feature = "lt2025_1", not(feature = "lt2022_2")),
220        doc = "Deletes an extension, its configurations, or both. To remove the named instance config, include the `--name` option: `p4 extension --delete myCompany::fileSizeCheck --name fileSizeCheck-instance --yes` To remove all revisions of an extension and its configurations, omit the `--name` option: `p4 extension --delete myCompany::fileSizeCheck` To remove one specific revision and its instances, include the `--revision` option: `p4 extension --delete myCompany::fileSizeCheck --revision=2 --yes` To delete file-based instances registered under the specified path: `p4 extension --delete ExampleInc::extD1 --path //depot/... --yes`",
221        doc = "",
222        doc = "# Note",
223        doc = "",
224        doc = "To actually perform the install or delete, the `--yes` option is required. Without `--yes`, the p4 extension command merely reports what it would do without actually performing the install or delete.",
225        doc = "",
226        doc = "extFQN means fully-qualified name: the combination of the Extension's namespace, name, and optional depot file revision."
227    )]
228    #[cfg_attr(
229        not(feature = "lt2025_1"),
230        doc = "Deletes an extension, its configurations, or both. To remove the named instance config, include the `--name` option: `p4 extension --delete myCompany::fileSizeCheck --name fileSizeCheck-instance --yes` To remove all revisions of an extension and its configurations, omit the `--name` option: `p4 extension --delete myCompany::fileSizeCheck` To remove one specific revision and its instances, include the `--revision` option: `p4 extension --delete myCompany::fileSizeCheck --revision=2 --yes` To delete file-based instances registered under the specified path: `p4 extension --delete ExampleInc::extD1 --path //depot/... --yes`",
231        doc = "",
232        doc = "# Note",
233        doc = "",
234        doc = "To perform the install or delete, the `--yes` option is required. Without `--yes`, the p4 extension command merely reports what it would do without actually performing the install or delete.",
235        doc = "",
236        doc = "extFQN means fully-qualified name: the combination of the Extension's namespace, name, and optional depot file revision."
237    )]
238    pub fn delete(self, fqn: impl Into<String>) -> Extension<Delete<Unselected>> {
239        Extension {
240            bin: self.bin,
241            global_opts: self.global_opts,
242            op: Delete {
243                fqn: fqn.into(),
244                mode: Unselected,
245            },
246        }
247    }
248
249    /// # Description
250    ///
251    /// --configure extFQN
252    ///
253    #[cfg_attr(
254        feature = "lt2020_1",
255        doc = "Supplies details about an Extension's configuration. To create a global configuration, use `--configure` with the fully-qualified name of the Extension; to create an instance configuration, combine it with the `--name` option."
256    )]
257    #[cfg_attr(
258        all(feature = "lt2020_2", not(feature = "lt2020_1")),
259        doc = "The `--configure` flag is used to supply details about an Extension's configuration.  There are two parts to configuring an Extension: the global configuration the instance configuration. To create a global configuration, use `--configure` with the FQN of the Extension. To create an instance configuration, use the `--configure` option with the `--name` option. The `--name` option takes the name of the configuration to create or modify.  More than one named instance is allowed for most event types that an Extension can be coded to work with, such as the change-submit event. However, the auth-check event allows only one named instance."
260    )]
261    #[cfg_attr(
262        all(feature = "lt2022_2", not(feature = "lt2020_2")),
263        doc = "An Extension is not active until it has been configured. The default global configuration is: `p4 extension --configure ExampleInc::myExt` and your global configuration might be: `p4 extension --configure myCompany::fileSizeCheck` and the corresponding instance configuration might be: `p4 extension --configure myCompany::extName --name Release1.0-fileSizeCheckInst` An Extension can have multiple configurations, depending on the events it registers for.  Each configuration of an Extension can point to a different version of the Extension."
264    )]
265    #[cfg_attr(
266        all(feature = "lt2025_2", not(feature = "lt2022_2")),
267        doc = "An Extension is not active until it has been configured. In this syntax: `p4 extension --configure extFQN [ --revision=n ] [ -o | -i ]` extFQN means fully-qualified name: the combination of the Extension's namespace, name, and optional depot file revision. The default global configuration is: `p4 extension --configure ExampleInc::myExt` and your global configuration might be: `p4 extension --configure myCompany::fileSizeCheck` and the corresponding instance configuration might be: `p4 extension --configure myCompany::extName --name Release1.0-fileSizeCheckInst` An Extension can have multiple configurations, depending on the events it registers for.  Each configuration of an Extension can point to a different version of the Extension."
268    )]
269    #[cfg_attr(
270        not(feature = "lt2025_2"),
271        doc = "An Extension is not active until it has been configured. In this syntax: `p4 extension --configure extFQN [ --revision=n ] [ -o | -i ]` extFQN means fully-qualified name: the combination of the Extension's namespace, name, and optional depot file revision. The default global configuration is: `p4 extension --configure ExampleInc::myExt` and your global configuration might be: `p4 extension --configure myCompany::fileSizeCheck` and the corresponding instance configuration might be: `p4 extension --configure myCompany::extName --name Release1.0-fileSizeCheckInst` An Extension can have multiple configurations, depending on the events it registers for.  Each configuration of an Extension can point to a different version of the Extension.",
272        doc = "",
273        doc = "Since 2025.2, you can prevent users from creating multiple configurations of the same extension. To learn more,, see the limit_instance field under Server Extension JSON manifest fields in the P4 Server Administration Documentation."
274    )]
275    pub fn configure(self, fqn: impl Into<String>) -> Extension<Configure<Unselected>> {
276        Extension {
277            bin: self.bin,
278            global_opts: self.global_opts,
279            op: Configure {
280                fqn: fqn.into(),
281                revision: None,
282                name: None,
283                mode: Unselected,
284            },
285        }
286    }
287
288    /// # Description
289    ///
290    /// --run instName
291    ///
292    #[cfg_attr(
293        all(feature = "lt2020_2", not(feature = "lt2020_1")),
294        doc = "Executes the Extension-supplied command. An Extension can implement its own end-user commands that behave similarly to native Helix Core commands in that they can have tagged output, send RPC-level messages, and open forms client-side. However, Extension-supplied commands are restricted to reporting output and cannot access client-side content. The `--run` option takes an instance configuration as its argument, and any other arguments are left to the Extension to interpret."
295    )]
296    #[cfg_attr(
297        not(feature = "lt2020_2"),
298        doc = "Executes the specified Extension. An Extension can implement custom end-user commands, executed by passing the `--run` flag the name of an instance config and supplying any optional arguments the Extension might need."
299    )]
300    #[cfg(not(feature = "lt2020_1"))]
301    pub fn run(self, inst: impl Into<String>) -> Extension<Run> {
302        Extension {
303            bin: self.bin,
304            global_opts: self.global_opts,
305            op: Run { inst: inst.into() },
306        }
307    }
308
309    /// # Description
310    ///
311    /// --list
312    ///
313    #[cfg_attr(
314        feature = "lt2020_1",
315        doc = "Displays the installed extensions and their instances.  If this option is specified with the `--type=extensions`, `--list` option, the output shows information about which extensions are installed, and `--type=configs` shows information about the different configurations."
316    )]
317    #[cfg_attr(
318        all(feature = "lt2020_2", not(feature = "lt2020_1")),
319        doc = "Displays information about installed Extensions. The value of the `--type` flag determines which data is reported.  Values for `--type` are: extensions configs, further narrowed by global and instance Adding the `--path` option filters instance configuration output to instances whose events are path-based and match the specified filespec."
320    )]
321    #[cfg_attr(
322        all(feature = "lt2022_2", not(feature = "lt2020_2")),
323        doc = "Displays the installed extensions and their instances. To list all Extensions: `p4 extension --list --type=extensions` To list all instance configurations: `p4 extension --list --type=configs` You can replace `--type=configs` with `--type=global` or `--type=instance` to filter results accordingly. Adding `--path=<path>` filters output to instance configurations that are path-based and their path argument matches the given `<path>`. The output of `p4 extension --list --type=extensions` indicates the status of extension configuration: No Global Config, No Instance Config, Incomplete Global Config, Incomplete Instance Config, or OK.",
324        doc = "",
325        doc = "# Note",
326        doc = "",
327        doc = "For an extension to be active, an extension must have a global configuration and at least one instance configuration."
328    )]
329    #[cfg_attr(
330        not(feature = "lt2022_2"),
331        doc = "Displays the installed extensions and their instances. To list all Extensions: `p4 extension --list --type extensions` To list all instance configurations: `p4 extension --list --type configs` You can replace `--type configs` with `--type global` or `--type instance` to filter results accordingly. Adding `--path path` filters output to instance configurations that are path-based and their path argument matches the given path. The output of `p4 extension --list --type extensions` indicates the status of extension configuration: No Global Config, No Instance Config, Incomplete Global Config, Incomplete Instance Config, or OK.",
332        doc = "",
333        doc = "# Note",
334        doc = "",
335        doc = "For an extension to be active, an extension must have a global configuration and at least one instance configuration."
336    )]
337    pub fn list(self) -> Extension<List> {
338        Extension {
339            bin: self.bin,
340            global_opts: self.global_opts,
341            op: List {
342                ty: None,
343
344                #[cfg(not(feature = "lt2022_2"))]
345                path: None,
346            },
347        }
348    }
349}
350
351// ---------------------------------------------------------------------------
352// Extension<T> — command carrier
353// ---------------------------------------------------------------------------
354
355/// A `p4 extension` command with a selected operation.
356///
357/// # Syntax
358///
359/// ```text
360/// p4 [g-opts] extension --sample extName
361/// p4 [g-opts] extension --package dir [--sign keyDir]
362#[cfg_attr(
363    feature = "lt2020_1",
364    doc = "p4 [g-opts] extension --install extName.p4-extension"
365)]
366#[cfg_attr(
367    not(feature = "lt2020_1"),
368    doc = "p4 [g-opts] extension --install extName.p4-extension [--yes]"
369)]
370/// p4 [g-opts] extension --install certificateFile --cert [--comment]
371/// p4 [g-opts] extension --delete extFQN [--name instName] [--revision=n] [--path FileSpec] [--yes]
372/// p4 [g-opts] extension --delete fingerprint --cert
373/// p4 [g-opts] extension --configure extFQN [--revision=n] [-o | -i]
374/// p4 [g-opts] extension --configure extFQN [--revision=n] [-o | -i] --name instName
375/// p4 [g-opts] extension --run instName [extArguments]
376/// p4 [g-opts] extension --list --type type [--path FileSpec]
377/// ```
378///
379/// Manages the installation, versioning, and configuration of extensions.
380/// The type parameter selects the operation, such as [`Sample`], [`Package`],
381/// [`Install`], [`Delete`], [`Configure`],
382#[cfg_attr(feature = "lt2020_1", doc = "or [`List`].")]
383#[cfg_attr(not(feature = "lt2020_1"), doc = "[`Run`], or [`List`].")]
384pub struct Extension<T: ExclusiveOption> {
385    bin: PathBuf,
386
387    global_opts: GlobalOpts,
388
389    op: T,
390}
391
392impl<T: ExclusiveOption> Extension<T> {
393    /// Returns the path of the p4 binary.
394    pub fn get_bin(&self) -> &Path {
395        &self.bin
396    }
397
398    /// Sets the path of the p4 binary.
399    pub fn set_bin(&mut self, v: impl Into<PathBuf>) -> &mut Self {
400        self.bin = v.into();
401        self
402    }
403
404    /// Sets the path of the p4 binary.
405    pub fn bin(mut self, v: impl Into<PathBuf>) -> Self {
406        self.bin = v.into();
407        self
408    }
409
410    /// # Description
411    ///
412    /// g-opts
413    ///
414    #[cfg_attr(
415        all(feature = "lt2018_2", not(feature = "lt2017_1")),
416        doc = "See [Global Options](GlobalOpts)."
417    )]
418    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
419    pub fn get_global_opts(&self) -> &GlobalOpts {
420        &self.global_opts
421    }
422
423    /// # Description
424    ///
425    /// g-opts
426    ///
427    #[cfg_attr(
428        all(feature = "lt2018_2", not(feature = "lt2017_1")),
429        doc = "See [Global Options](GlobalOpts)."
430    )]
431    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
432    pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
433        self.global_opts = v;
434        self
435    }
436
437    /// # Description
438    ///
439    /// g-opts
440    ///
441    #[cfg_attr(
442        all(feature = "lt2018_2", not(feature = "lt2017_1")),
443        doc = "See [Global Options](GlobalOpts)."
444    )]
445    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
446    pub fn global_opts(mut self, v: GlobalOpts) -> Self {
447        self.global_opts = v;
448        self
449    }
450}
451
452impl<T: ExclusiveOption> SubCommand for Extension<T> {
453    fn name(&self) -> &str {
454        "extension"
455    }
456
457    fn inject_local_args(&self, command: &mut Command) {
458        self.op.inject_args(command);
459    }
460
461    fn global_opts(&self) -> Option<&GlobalOpts> {
462        Some(&self.global_opts)
463    }
464}
465
466impl<T: ExclusiveOption> ParameterizedSpawn<()> for Extension<T> {
467    type Output = Child;
468    type Error = std::io::Error;
469
470    /// Spawns the selected `p4 extension` operation as a child process with
471    /// piped standard output and error streams; use the returned [`Child`]
472    /// handle to wait for it or interact with it.
473    fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
474        self.setup_command(&self.bin)
475            .stdout(Stdio::piped())
476            .stderr(Stdio::piped())
477            .spawn()
478    }
479}
480
481// ---------------------------------------------------------------------------
482// sample
483// ---------------------------------------------------------------------------
484
485/// The `--sample` operation of `p4 extension`.
486///
487/// Creates a skeleton of a new extension in a directory named after it.
488/// Selected with [`ExtensionEntry::sample`].
489#[derive(Debug, Clone, Default)]
490pub struct Sample {
491    name: String,
492}
493
494impl ExclusiveOption for Sample {
495    fn inject_args(&self, command: &mut Command) {
496        command.arg("--sample").arg(&self.name);
497    }
498}
499
500impl Extension<Sample> {
501    /// Returns the name of the extension skeleton to create.
502    pub fn get_name(&self) -> &str {
503        &self.op.name
504    }
505
506    /// Sets the name of the extension skeleton to create.
507    pub fn set_name(&mut self, v: impl Into<String>) -> &mut Self {
508        self.op.name = v.into();
509        self
510    }
511
512    /// Sets the name of the extension skeleton to create.
513    pub fn name(mut self, v: impl Into<String>) -> Self {
514        self.op.name = v.into();
515        self
516    }
517}
518
519// ---------------------------------------------------------------------------
520// package
521// ---------------------------------------------------------------------------
522
523/// The `--package` operation of `p4 extension`.
524///
525/// Packages a directory of extension files into a `.p4-extension` file.
526/// Selected with [`ExtensionEntry::package`].
527#[derive(Debug, Clone, Default)]
528pub struct Package {
529    dir: String,
530
531    sign: Option<String>,
532}
533
534impl ExclusiveOption for Package {
535    fn inject_args(&self, command: &mut Command) {
536        command.arg("--package").arg(&self.dir);
537
538        if let Some(key_dir) = &self.sign {
539            command.arg("--sign").arg(key_dir);
540        }
541    }
542}
543
544impl Extension<Package> {
545    /// Returns the directory of extension files to package.
546    pub fn get_dir(&self) -> &str {
547        &self.op.dir
548    }
549
550    /// Sets the directory of extension files to package.
551    pub fn set_dir(&mut self, v: impl Into<String>) -> &mut Self {
552        self.op.dir = v.into();
553        self
554    }
555
556    /// Sets the directory of extension files to package.
557    pub fn dir(mut self, v: impl Into<String>) -> Self {
558        self.op.dir = v.into();
559        self
560    }
561
562    /// # Description
563    ///
564    /// --sign keyDir
565    ///
566    /// Can be supplied in `--package`.
567    #[cfg(not(feature = "lt2022_2"))]
568    pub fn get_sign(&self) -> Option<&String> {
569        self.op.sign.as_ref()
570    }
571
572    /// # Description
573    ///
574    /// --sign keyDir
575    ///
576    /// Can be supplied in `--package`.
577    #[cfg(not(feature = "lt2022_2"))]
578    pub fn set_sign(&mut self, v: impl Into<String>) -> &mut Self {
579        self.op.sign = Some(v.into());
580        self
581    }
582
583    /// # Description
584    ///
585    /// --sign keyDir
586    ///
587    /// Can be supplied in `--package`.
588    #[cfg(not(feature = "lt2022_2"))]
589    pub fn sign(mut self, v: impl Into<String>) -> Self {
590        self.op.sign = Some(v.into());
591        self
592    }
593}
594
595// ---------------------------------------------------------------------------
596// install
597// ---------------------------------------------------------------------------
598
599/// The extension-file install state of `--install`.
600///
601/// Adds [`--yes`](Extension::yes) to perform the install for real; without
602/// it the command only reports what it would do.
603#[cfg(not(feature = "lt2020_1"))]
604#[derive(Debug, Clone, Copy, Default)]
605pub struct InstallExt {
606    yes: bool,
607
608    #[cfg(not(feature = "lt2022_2"))]
609    allow_unsigned: bool,
610}
611
612#[cfg(not(feature = "lt2020_1"))]
613impl ExclusiveOption for InstallExt {
614    fn inject_args(&self, command: &mut Command) {
615        if self.yes {
616            command.arg("--yes");
617        }
618
619        #[cfg(not(feature = "lt2022_2"))]
620        if self.allow_unsigned {
621            command.arg("--allow-unsigned");
622        }
623    }
624}
625
626/// The certificate install state of `--install`.
627///
628/// Installs a certificate file with `--cert`, optionally recording a
629/// [`--comment`](Extension::comment).
630#[derive(Debug, Clone, Default)]
631#[cfg(not(feature = "lt2022_2"))]
632pub struct InstallCert {
633    comment: Option<String>,
634}
635
636#[cfg(not(feature = "lt2022_2"))]
637impl ExclusiveOption for InstallCert {
638    fn inject_args(&self, command: &mut Command) {
639        command.arg("--cert");
640
641        if let Some(comment) = &self.comment {
642            command.arg("--comment").arg(comment);
643        }
644    }
645}
646
647/// The `--install` operation of `p4 extension`.
648///
649#[cfg_attr(
650    feature = "lt2020_1",
651    doc = "The `M` type parameter only ever takes [`Unselected`]; `--yes` cannot be supplied to `--install`."
652)]
653#[cfg_attr(
654    all(feature = "lt2022_2", not(feature = "lt2020_1")),
655    doc = "The type parameter selects what is installed: [`Unselected`] for a plain install, or [`InstallExt`] for an extension package."
656)]
657#[cfg_attr(
658    not(feature = "lt2022_2"),
659    doc = "The type parameter selects what is installed: [`Unselected`] for a plain install, [`InstallExt`] for an extension package, or [`InstallCert`] for a certificate."
660)]
661/// Selected with [`ExtensionEntry::install`].
662#[derive(Debug, Clone, Default)]
663pub struct Install<M = Unselected> {
664    file: String,
665
666    mode: M,
667}
668
669impl<M: ExclusiveOption> ExclusiveOption for Install<M> {
670    fn inject_args(&self, command: &mut Command) {
671        command.arg("--install").arg(&self.file);
672        self.mode.inject_args(command);
673    }
674}
675
676impl Extension<Install<Unselected>> {
677    /// Returns the packaged extension or certificate file to install.
678    pub fn get_file(&self) -> &str {
679        &self.op.file
680    }
681
682    /// Sets the packaged extension or certificate file to install.
683    pub fn set_file(&mut self, v: impl Into<String>) -> &mut Self {
684        self.op.file = v.into();
685        self
686    }
687
688    /// Sets the packaged extension or certificate file to install.
689    pub fn file(mut self, v: impl Into<String>) -> Self {
690        self.op.file = v.into();
691        self
692    }
693
694    /// # Description
695    ///
696    /// --yes
697    ///
698    /// To actually perform the install or delete, the `--yes` option is
699    /// required. Without `--yes`, the p4 extension command merely reports
700    /// what it would do without actually performing the install or delete.
701    /// `-y` is equivalent to `--yes`.
702    #[cfg(not(feature = "lt2020_1"))]
703    pub fn yes(self) -> Extension<Install<InstallExt>> {
704        Extension {
705            bin: self.bin,
706            global_opts: self.global_opts,
707            op: Install {
708                file: self.op.file,
709                mode: InstallExt {
710                    yes: true,
711
712                    #[cfg(not(feature = "lt2022_2"))]
713                    allow_unsigned: false,
714                },
715            },
716        }
717    }
718
719    /// # Description
720    ///
721    /// --cert
722    ///
723    /// Can be supplied in `--install` or `--delete`. For example,
724    /// `p4 extension --install path/certificate.txt --cert --comment "We trust ACTools"`.
725    #[cfg(not(feature = "lt2022_2"))]
726    pub fn cert(self) -> Extension<Install<InstallCert>> {
727        Extension {
728            bin: self.bin,
729            global_opts: self.global_opts,
730            op: Install {
731                file: self.op.file,
732                mode: InstallCert { comment: None },
733            },
734        }
735    }
736
737    /// # Description
738    ///
739    /// --comment
740    ///
741    /// Can be supplied in `--install` or `--delete`. For example,
742    /// `p4 extension --install path/certificate.txt --cert --comment "We trust ACTools"`.
743    #[cfg(not(feature = "lt2022_2"))]
744    pub fn comment(self, v: impl Into<String>) -> Extension<Install<InstallCert>> {
745        Extension {
746            bin: self.bin,
747            global_opts: self.global_opts,
748            op: Install {
749                file: self.op.file,
750                mode: InstallCert {
751                    comment: Some(v.into()),
752                },
753            },
754        }
755    }
756}
757
758#[cfg(not(feature = "lt2020_1"))]
759impl Extension<Install<InstallExt>> {
760    /// # Description
761    ///
762    /// --yes
763    ///
764    /// To actually perform the install or delete, the `--yes` option is
765    /// required. Without `--yes`, the p4 extension command merely reports
766    /// what it would do without actually performing the install or delete.
767    /// `-y` is equivalent to `--yes`.
768    pub fn get_yes(&self) -> bool {
769        self.op.mode.yes
770    }
771
772    /// # Description
773    ///
774    /// --yes
775    ///
776    /// To actually perform the install or delete, the `--yes` option is
777    /// required. Without `--yes`, the p4 extension command merely reports
778    /// what it would do without actually performing the install or delete.
779    /// `-y` is equivalent to `--yes`.
780    pub fn set_yes(&mut self, v: bool) -> &mut Self {
781        self.op.mode.yes = v;
782        self
783    }
784
785    /// # Description
786    ///
787    /// --yes
788    ///
789    /// To actually perform the install or delete, the `--yes` option is
790    /// required. Without `--yes`, the p4 extension command merely reports
791    /// what it would do without actually performing the install or delete.
792    /// `-y` is equivalent to `--yes`.
793    pub fn yes(mut self, v: bool) -> Self {
794        self.op.mode.yes = v;
795        self
796    }
797
798    /// # Description
799    ///
800    /// --allow-unsigned
801    ///
802    #[cfg_attr(
803        all(
804            feature = "lt2025_1",
805            not(feature = "lt2022_2"),
806            feature = "lt2024_2",
807            not(feature = "lt2024_1")
808        ),
809        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the Configurables reference configurable were set to 1 so that the extension does not need to be signed."
810    )]
811    #[cfg_attr(
812        not(all(
813            feature = "lt2025_1",
814            not(feature = "lt2022_2"),
815            feature = "lt2024_2",
816            not(feature = "lt2024_1")
817        )),
818        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the server.extensions.allow.unsigned configurable were set to 1 so that the extension does not need to be signed."
819    )]
820    #[cfg(not(feature = "lt2022_2"))]
821    pub fn get_allow_unsigned(&self) -> bool {
822        self.op.mode.allow_unsigned
823    }
824
825    /// # Description
826    ///
827    /// --allow-unsigned
828    ///
829    #[cfg_attr(
830        all(
831            feature = "lt2025_1",
832            not(feature = "lt2022_2"),
833            feature = "lt2024_2",
834            not(feature = "lt2024_1")
835        ),
836        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the Configurables reference configurable were set to 1 so that the extension does not need to be signed."
837    )]
838    #[cfg_attr(
839        not(all(
840            feature = "lt2025_1",
841            not(feature = "lt2022_2"),
842            feature = "lt2024_2",
843            not(feature = "lt2024_1")
844        )),
845        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the server.extensions.allow.unsigned configurable were set to 1 so that the extension does not need to be signed."
846    )]
847    #[cfg(not(feature = "lt2022_2"))]
848    pub fn set_allow_unsigned(&mut self, v: bool) -> &mut Self {
849        self.op.mode.allow_unsigned = v;
850        self
851    }
852
853    /// # Description
854    ///
855    /// --allow-unsigned
856    ///
857    #[cfg_attr(
858        all(
859            feature = "lt2025_1",
860            not(feature = "lt2022_2"),
861            feature = "lt2024_2",
862            not(feature = "lt2024_1")
863        ),
864        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the Configurables reference configurable were set to 1 so that the extension does not need to be signed."
865    )]
866    #[cfg_attr(
867        not(all(
868            feature = "lt2025_1",
869            not(feature = "lt2022_2"),
870            feature = "lt2024_2",
871            not(feature = "lt2024_1")
872        )),
873        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the server.extensions.allow.unsigned configurable were set to 1 so that the extension does not need to be signed."
874    )]
875    #[cfg(not(feature = "lt2022_2"))]
876    pub fn allow_unsigned(mut self, v: bool) -> Self {
877        self.op.mode.allow_unsigned = v;
878        self
879    }
880}
881
882#[cfg(not(feature = "lt2022_2"))]
883impl Extension<Install<InstallCert>> {
884    /// # Description
885    ///
886    /// --comment
887    ///
888    /// Can be supplied in `--install` or `--delete`. For example,
889    /// `p4 extension --install path/certificate.txt --cert --comment "We trust ACTools"`.
890    pub fn get_comment(&self) -> Option<&String> {
891        self.op.mode.comment.as_ref()
892    }
893
894    /// # Description
895    ///
896    /// --comment
897    ///
898    /// Can be supplied in `--install` or `--delete`. For example,
899    /// `p4 extension --install path/certificate.txt --cert --comment "We trust ACTools"`.
900    pub fn set_comment(&mut self, v: impl Into<String>) -> &mut Self {
901        self.op.mode.comment = Some(v.into());
902        self
903    }
904
905    /// # Description
906    ///
907    /// --comment
908    ///
909    /// Can be supplied in `--install` or `--delete`. For example,
910    /// `p4 extension --install path/certificate.txt --cert --comment "We trust ACTools"`.
911    pub fn comment(mut self, v: impl Into<String>) -> Self {
912        self.op.mode.comment = Some(v.into());
913        self
914    }
915}
916
917// ---------------------------------------------------------------------------
918// delete
919// ---------------------------------------------------------------------------
920
921/// The extension delete state of `--delete`.
922///
923/// Narrows the deletion with [`--name`](Extension::name),
924/// [`--revision`](Extension::revision), or [`--path`](Extension::path), and
925/// performs it for real with [`--yes`](Extension::yes).
926#[derive(Debug, Clone, Default)]
927pub struct DeleteExt {
928    yes: bool,
929
930    #[cfg(not(feature = "lt2020_1"))]
931    name: Option<String>,
932
933    revision: Option<String>,
934
935    path: Option<String>,
936
937    #[cfg(not(feature = "lt2022_2"))]
938    allow_unsigned: bool,
939}
940
941impl ExclusiveOption for DeleteExt {
942    fn inject_args(&self, command: &mut Command) {
943        #[cfg(not(feature = "lt2020_1"))]
944        if let Some(name) = &self.name {
945            command.arg("--name").arg(name);
946        }
947
948        if let Some(revision) = &self.revision {
949            command.arg(format!("--revision={revision}"));
950        }
951
952        if let Some(path) = &self.path {
953            command.arg("--path").arg(path);
954        }
955
956        if self.yes {
957            command.arg("--yes");
958        }
959
960        #[cfg(not(feature = "lt2022_2"))]
961        if self.allow_unsigned {
962            command.arg("--allow-unsigned");
963        }
964    }
965}
966
967/// The certificate delete state of `--delete`.
968///
969/// Deletes a certificate by fingerprint with `--cert`. Selected with
970/// [`Extension::cert`].
971#[derive(Debug, Clone, Copy, Default)]
972#[cfg(not(feature = "lt2022_2"))]
973pub struct DeleteCert;
974
975#[cfg(not(feature = "lt2022_2"))]
976impl ExclusiveOption for DeleteCert {
977    fn inject_args(&self, command: &mut Command) {
978        command.arg("--cert");
979    }
980}
981
982/// The `--delete` operation of `p4 extension`.
983///
984/// The type parameter selects what is deleted:
985#[cfg_attr(
986    feature = "lt2022_2",
987    doc = "[`Unselected`] or [`DeleteExt`] for an extension."
988)]
989#[cfg_attr(
990    not(feature = "lt2022_2"),
991    doc = "[`Unselected`] or [`DeleteExt`] for an extension, [`DeleteCert`] for a certificate."
992)]
993/// Selected with [`ExtensionEntry::delete`].
994#[derive(Debug, Clone, Default)]
995pub struct Delete<M = Unselected> {
996    fqn: String,
997
998    mode: M,
999}
1000
1001impl<M: ExclusiveOption> ExclusiveOption for Delete<M> {
1002    fn inject_args(&self, command: &mut Command) {
1003        command.arg("--delete").arg(&self.fqn);
1004        self.mode.inject_args(command);
1005    }
1006}
1007
1008impl Extension<Delete<Unselected>> {
1009    /// Returns the fully-qualified name (or certificate fingerprint) to
1010    /// delete.
1011    pub fn get_fqn(&self) -> &str {
1012        &self.op.fqn
1013    }
1014
1015    /// Sets the fully-qualified name (or certificate fingerprint) to delete.
1016    pub fn set_fqn(&mut self, v: impl Into<String>) -> &mut Self {
1017        self.op.fqn = v.into();
1018        self
1019    }
1020
1021    /// Sets the fully-qualified name (or certificate fingerprint) to delete.
1022    pub fn fqn(mut self, v: impl Into<String>) -> Self {
1023        self.op.fqn = v.into();
1024        self
1025    }
1026
1027    /// # Description
1028    ///
1029    /// --yes
1030    ///
1031    /// To actually perform the install or delete, the `--yes` option is
1032    /// required. Without `--yes`, the p4 extension command merely reports
1033    /// what it would do without actually performing the install or delete.
1034    /// `-y` is equivalent to `--yes`.
1035    pub fn yes(self) -> Extension<Delete<DeleteExt>> {
1036        self.delete_ext(|mode| mode.yes = true)
1037    }
1038
1039    /// # Description
1040    ///
1041    /// --name instName
1042    ///
1043    /// To remove the named instance config, include the `--name` option.
1044    #[cfg(not(feature = "lt2020_1"))]
1045    pub fn name(self, v: impl Into<String>) -> Extension<Delete<DeleteExt>> {
1046        let name = v.into();
1047        self.delete_ext(|mode| mode.name = Some(name))
1048    }
1049
1050    /// # Description
1051    ///
1052    /// --revision=n
1053    ///
1054    /// To remove one specific revision and its instances, include the
1055    /// `--revision` option.
1056    pub fn revision(self, v: impl Into<String>) -> Extension<Delete<DeleteExt>> {
1057        let revision = v.into();
1058        self.delete_ext(|mode| mode.revision = Some(revision))
1059    }
1060
1061    /// # Description
1062    ///
1063    /// --path FileSpec
1064    ///
1065    /// To delete file-based instances registered under the specified path,
1066    /// use the `--path` option.
1067    pub fn path(self, v: impl Into<String>) -> Extension<Delete<DeleteExt>> {
1068        let path = v.into();
1069        self.delete_ext(|mode| mode.path = Some(path))
1070    }
1071
1072    /// # Description
1073    ///
1074    /// --cert
1075    ///
1076    /// Can be supplied in `--install` or `--delete`. For example,
1077    /// `p4 extension --install path/certificate.txt --cert --comment "We trust ACTools"`.
1078    #[cfg(not(feature = "lt2022_2"))]
1079    pub fn cert(self) -> Extension<Delete<DeleteCert>> {
1080        Extension {
1081            bin: self.bin,
1082            global_opts: self.global_opts,
1083            op: Delete {
1084                fqn: self.op.fqn,
1085                mode: DeleteCert,
1086            },
1087        }
1088    }
1089
1090    fn delete_ext(mut self, f: impl FnOnce(&mut DeleteExt)) -> Extension<Delete<DeleteExt>> {
1091        let mut mode = DeleteExt::default();
1092        f(&mut mode);
1093        Extension {
1094            bin: self.bin,
1095            global_opts: self.global_opts,
1096            op: Delete {
1097                fqn: std::mem::take(&mut self.op.fqn),
1098                mode,
1099            },
1100        }
1101    }
1102}
1103
1104impl Extension<Delete<DeleteExt>> {
1105    /// # Description
1106    ///
1107    /// --yes
1108    ///
1109    /// To actually perform the install or delete, the `--yes` option is
1110    /// required. Without `--yes`, the p4 extension command merely reports
1111    /// what it would do without actually performing the install or delete.
1112    /// `-y` is equivalent to `--yes`.
1113    pub fn get_yes(&self) -> bool {
1114        self.op.mode.yes
1115    }
1116
1117    /// # Description
1118    ///
1119    /// --yes
1120    ///
1121    /// To actually perform the install or delete, the `--yes` option is
1122    /// required. Without `--yes`, the p4 extension command merely reports
1123    /// what it would do without actually performing the install or delete.
1124    /// `-y` is equivalent to `--yes`.
1125    pub fn set_yes(&mut self, v: bool) -> &mut Self {
1126        self.op.mode.yes = v;
1127        self
1128    }
1129
1130    /// # Description
1131    ///
1132    /// --yes
1133    ///
1134    /// To actually perform the install or delete, the `--yes` option is
1135    /// required. Without `--yes`, the p4 extension command merely reports
1136    /// what it would do without actually performing the install or delete.
1137    /// `-y` is equivalent to `--yes`.
1138    pub fn yes(mut self, v: bool) -> Self {
1139        self.op.mode.yes = v;
1140        self
1141    }
1142
1143    /// # Description
1144    ///
1145    /// --name instName
1146    ///
1147    /// To remove the named instance config, include the `--name` option.
1148    #[cfg(not(feature = "lt2020_1"))]
1149    pub fn get_name(&self) -> Option<&String> {
1150        self.op.mode.name.as_ref()
1151    }
1152
1153    /// # Description
1154    ///
1155    /// --name instName
1156    ///
1157    /// To remove the named instance config, include the `--name` option.
1158    #[cfg(not(feature = "lt2020_1"))]
1159    pub fn set_name(&mut self, v: impl Into<String>) -> &mut Self {
1160        self.op.mode.name = Some(v.into());
1161        self
1162    }
1163
1164    /// # Description
1165    ///
1166    /// --name instName
1167    ///
1168    /// To remove the named instance config, include the `--name` option.
1169    #[cfg(not(feature = "lt2020_1"))]
1170    pub fn name(mut self, v: impl Into<String>) -> Self {
1171        self.op.mode.name = Some(v.into());
1172        self
1173    }
1174
1175    /// # Description
1176    ///
1177    /// --revision=n
1178    ///
1179    /// To remove one specific revision and its instances, include the
1180    /// `--revision` option.
1181    pub fn get_revision(&self) -> Option<&String> {
1182        self.op.mode.revision.as_ref()
1183    }
1184
1185    /// # Description
1186    ///
1187    /// --revision=n
1188    ///
1189    /// To remove one specific revision and its instances, include the
1190    /// `--revision` option.
1191    pub fn set_revision(&mut self, v: impl Into<String>) -> &mut Self {
1192        self.op.mode.revision = Some(v.into());
1193        self
1194    }
1195
1196    /// # Description
1197    ///
1198    /// --revision=n
1199    ///
1200    /// To remove one specific revision and its instances, include the
1201    /// `--revision` option.
1202    pub fn revision(mut self, v: impl Into<String>) -> Self {
1203        self.op.mode.revision = Some(v.into());
1204        self
1205    }
1206
1207    /// # Description
1208    ///
1209    /// --path FileSpec
1210    ///
1211    /// To delete file-based instances registered under the specified path,
1212    /// use the `--path` option.
1213    pub fn get_path(&self) -> Option<&String> {
1214        self.op.mode.path.as_ref()
1215    }
1216
1217    /// # Description
1218    ///
1219    /// --path FileSpec
1220    ///
1221    /// To delete file-based instances registered under the specified path,
1222    /// use the `--path` option.
1223    pub fn set_path(&mut self, v: impl Into<String>) -> &mut Self {
1224        self.op.mode.path = Some(v.into());
1225        self
1226    }
1227
1228    /// # Description
1229    ///
1230    /// --path FileSpec
1231    ///
1232    /// To delete file-based instances registered under the specified path,
1233    /// use the `--path` option.
1234    pub fn path(mut self, v: impl Into<String>) -> Self {
1235        self.op.mode.path = Some(v.into());
1236        self
1237    }
1238
1239    /// # Description
1240    ///
1241    /// --allow-unsigned
1242    ///
1243    #[cfg_attr(
1244        all(
1245            feature = "lt2025_1",
1246            not(feature = "lt2022_2"),
1247            feature = "lt2024_2",
1248            not(feature = "lt2024_1")
1249        ),
1250        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the Configurables reference configurable were set to 1 so that the extension does not need to be signed."
1251    )]
1252    #[cfg_attr(
1253        not(all(
1254            feature = "lt2025_1",
1255            not(feature = "lt2022_2"),
1256            feature = "lt2024_2",
1257            not(feature = "lt2024_1")
1258        )),
1259        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the server.extensions.allow.unsigned configurable were set to 1 so that the extension does not need to be signed."
1260    )]
1261    #[cfg(not(feature = "lt2022_2"))]
1262    pub fn get_allow_unsigned(&self) -> bool {
1263        self.op.mode.allow_unsigned
1264    }
1265
1266    /// # Description
1267    ///
1268    /// --allow-unsigned
1269    ///
1270    #[cfg_attr(
1271        all(
1272            feature = "lt2025_1",
1273            not(feature = "lt2022_2"),
1274            feature = "lt2024_2",
1275            not(feature = "lt2024_1")
1276        ),
1277        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the Configurables reference configurable were set to 1 so that the extension does not need to be signed."
1278    )]
1279    #[cfg_attr(
1280        not(all(
1281            feature = "lt2025_1",
1282            not(feature = "lt2022_2"),
1283            feature = "lt2024_2",
1284            not(feature = "lt2024_1")
1285        )),
1286        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the server.extensions.allow.unsigned configurable were set to 1 so that the extension does not need to be signed."
1287    )]
1288    #[cfg(not(feature = "lt2022_2"))]
1289    pub fn set_allow_unsigned(&mut self, v: bool) -> &mut Self {
1290        self.op.mode.allow_unsigned = v;
1291        self
1292    }
1293
1294    /// # Description
1295    ///
1296    /// --allow-unsigned
1297    ///
1298    #[cfg_attr(
1299        all(
1300            feature = "lt2025_1",
1301            not(feature = "lt2022_2"),
1302            feature = "lt2024_2",
1303            not(feature = "lt2024_1")
1304        ),
1305        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the Configurables reference configurable were set to 1 so that the extension does not need to be signed."
1306    )]
1307    #[cfg_attr(
1308        not(all(
1309            feature = "lt2025_1",
1310            not(feature = "lt2022_2"),
1311            feature = "lt2024_2",
1312            not(feature = "lt2024_1")
1313        )),
1314        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the server.extensions.allow.unsigned configurable were set to 1 so that the extension does not need to be signed."
1315    )]
1316    #[cfg(not(feature = "lt2022_2"))]
1317    pub fn allow_unsigned(mut self, v: bool) -> Self {
1318        self.op.mode.allow_unsigned = v;
1319        self
1320    }
1321}
1322
1323// ---------------------------------------------------------------------------
1324// configure
1325// ---------------------------------------------------------------------------
1326
1327/// The `-i` state of `--configure`.
1328///
1329/// Reads the extension definition from standard input. Selected with
1330/// [`Extension::stdin`].
1331#[derive(Debug, Clone, Copy, Default)]
1332pub struct Stdin;
1333
1334impl ExclusiveOption for Stdin {
1335    fn inject_args(&self, command: &mut Command) {
1336        command.arg("-i");
1337    }
1338}
1339
1340/// The `-o` state of `--configure`.
1341///
1342/// Writes the extension definition to standard output. Selected with
1343/// [`Extension::stdout`].
1344#[derive(Debug, Clone, Copy, Default)]
1345pub struct Stdout;
1346
1347impl ExclusiveOption for Stdout {
1348    fn inject_args(&self, command: &mut Command) {
1349        command.arg("-o");
1350    }
1351}
1352
1353/// The `--configure` operation of `p4 extension`.
1354///
1355/// The type parameter selects where the extension definition goes:
1356/// [`Unselected`] to edit it in a form, [`Stdin`] (`-i`) to read it from
1357/// standard input, or [`Stdout`] (`-o`) to write it to standard output.
1358/// Selected with [`ExtensionEntry::configure`].
1359#[derive(Debug, Clone, Default)]
1360pub struct Configure<M = Unselected> {
1361    fqn: String,
1362
1363    revision: Option<String>,
1364
1365    name: Option<String>,
1366
1367    mode: M,
1368}
1369
1370impl<M: ExclusiveOption> ExclusiveOption for Configure<M> {
1371    fn inject_args(&self, command: &mut Command) {
1372        command.arg("--configure").arg(&self.fqn);
1373
1374        if let Some(revision) = &self.revision {
1375            command.arg(format!("--revision={revision}"));
1376        }
1377
1378        self.mode.inject_args(command);
1379
1380        if let Some(name) = &self.name {
1381            command.arg("--name").arg(name);
1382        }
1383    }
1384}
1385
1386impl Extension<Configure<Unselected>> {
1387    /// # Description
1388    ///
1389    /// -i
1390    ///
1391    #[cfg_attr(
1392        not(feature = "lt2022_1"),
1393        doc = "Read the extension definition from standard input."
1394    )]
1395    pub fn stdin(self) -> Extension<Configure<Stdin>> {
1396        Extension {
1397            bin: self.bin,
1398            global_opts: self.global_opts,
1399            op: Configure {
1400                fqn: self.op.fqn,
1401                revision: self.op.revision,
1402                name: self.op.name,
1403                mode: Stdin,
1404            },
1405        }
1406    }
1407
1408    /// # Description
1409    ///
1410    /// -o
1411    ///
1412    #[cfg_attr(
1413        not(feature = "lt2022_1"),
1414        doc = "Write the extension definition from to standard output."
1415    )]
1416    pub fn stdout(self) -> Extension<Configure<Stdout>> {
1417        Extension {
1418            bin: self.bin,
1419            global_opts: self.global_opts,
1420            op: Configure {
1421                fqn: self.op.fqn,
1422                revision: self.op.revision,
1423                name: self.op.name,
1424                mode: Stdout,
1425            },
1426        }
1427    }
1428}
1429
1430impl<M: ExclusiveOption> Extension<Configure<M>> {
1431    /// Returns the fully-qualified name of the extension to configure.
1432    pub fn get_fqn(&self) -> &str {
1433        &self.op.fqn
1434    }
1435
1436    /// Sets the fully-qualified name of the extension to configure.
1437    pub fn set_fqn(&mut self, v: impl Into<String>) -> &mut Self {
1438        self.op.fqn = v.into();
1439        self
1440    }
1441
1442    /// Sets the fully-qualified name of the extension to configure.
1443    pub fn fqn(mut self, v: impl Into<String>) -> Self {
1444        self.op.fqn = v.into();
1445        self
1446    }
1447
1448    /// # Description
1449    ///
1450    /// --revision=n
1451    ///
1452    #[cfg_attr(
1453        feature = "lt2020_1",
1454        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option."
1455    )]
1456    #[cfg_attr(
1457        all(feature = "lt2022_1", not(feature = "lt2020_1")),
1458        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option. This flag applies to the `--configure` option when used with or without the `--name` option."
1459    )]
1460    #[cfg_attr(
1461        all(feature = "lt2022_2", not(feature = "lt2022_1")),
1462        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option. This flag applies to the `--configure` option when used with or without the `--name` option. It is optional to include a # followed by a revision number, such as ExampleInc::extName#6."
1463    )]
1464    #[cfg_attr(
1465        not(feature = "lt2022_2"),
1466        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option. This flag applies to the `--configure` option when used with or without the `--name` option."
1467    )]
1468    pub fn get_revision(&self) -> Option<&String> {
1469        self.op.revision.as_ref()
1470    }
1471
1472    /// # Description
1473    ///
1474    /// --revision=n
1475    ///
1476    #[cfg_attr(
1477        feature = "lt2020_1",
1478        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option."
1479    )]
1480    #[cfg_attr(
1481        all(feature = "lt2022_1", not(feature = "lt2020_1")),
1482        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option. This flag applies to the `--configure` option when used with or without the `--name` option."
1483    )]
1484    #[cfg_attr(
1485        all(feature = "lt2022_2", not(feature = "lt2022_1")),
1486        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option. This flag applies to the `--configure` option when used with or without the `--name` option. It is optional to include a # followed by a revision number, such as ExampleInc::extName#6."
1487    )]
1488    #[cfg_attr(
1489        not(feature = "lt2022_2"),
1490        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option. This flag applies to the `--configure` option when used with or without the `--name` option."
1491    )]
1492    pub fn set_revision(&mut self, v: impl Into<String>) -> &mut Self {
1493        self.op.revision = Some(v.into());
1494        self
1495    }
1496
1497    /// # Description
1498    ///
1499    /// --revision=n
1500    ///
1501    #[cfg_attr(
1502        feature = "lt2020_1",
1503        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option."
1504    )]
1505    #[cfg_attr(
1506        all(feature = "lt2022_1", not(feature = "lt2020_1")),
1507        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option. This flag applies to the `--configure` option when used with or without the `--name` option."
1508    )]
1509    #[cfg_attr(
1510        all(feature = "lt2022_2", not(feature = "lt2022_1")),
1511        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option. This flag applies to the `--configure` option when used with or without the `--name` option. It is optional to include a # followed by a revision number, such as ExampleInc::extName#6."
1512    )]
1513    #[cfg_attr(
1514        not(feature = "lt2022_2"),
1515        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option. This flag applies to the `--configure` option when used with or without the `--name` option."
1516    )]
1517    pub fn revision(mut self, v: impl Into<String>) -> Self {
1518        self.op.revision = Some(v.into());
1519        self
1520    }
1521
1522    /// # Description
1523    ///
1524    /// --name instName
1525    ///
1526    #[cfg_attr(
1527        feature = "lt2020_1",
1528        doc = "The name of the configuration to create or modify."
1529    )]
1530    #[cfg_attr(
1531        all(feature = "lt2022_2", not(feature = "lt2020_1")),
1532        doc = "The name of the instance configuration to create or modify."
1533    )]
1534    #[cfg_attr(
1535        not(feature = "lt2022_2"),
1536        doc = "The name of the instance configuration to create, modify, or delete."
1537    )]
1538    pub fn get_name(&self) -> Option<&String> {
1539        self.op.name.as_ref()
1540    }
1541
1542    /// # Description
1543    ///
1544    /// --name instName
1545    ///
1546    #[cfg_attr(
1547        feature = "lt2020_1",
1548        doc = "The name of the configuration to create or modify."
1549    )]
1550    #[cfg_attr(
1551        all(feature = "lt2022_2", not(feature = "lt2020_1")),
1552        doc = "The name of the instance configuration to create or modify."
1553    )]
1554    #[cfg_attr(
1555        not(feature = "lt2022_2"),
1556        doc = "The name of the instance configuration to create, modify, or delete."
1557    )]
1558    pub fn set_name(&mut self, v: impl Into<String>) -> &mut Self {
1559        self.op.name = Some(v.into());
1560        self
1561    }
1562
1563    /// # Description
1564    ///
1565    /// --name instName
1566    ///
1567    #[cfg_attr(
1568        feature = "lt2020_1",
1569        doc = "The name of the configuration to create or modify."
1570    )]
1571    #[cfg_attr(
1572        all(feature = "lt2022_2", not(feature = "lt2020_1")),
1573        doc = "The name of the instance configuration to create or modify."
1574    )]
1575    #[cfg_attr(
1576        not(feature = "lt2022_2"),
1577        doc = "The name of the instance configuration to create, modify, or delete."
1578    )]
1579    pub fn name(mut self, v: impl Into<String>) -> Self {
1580        self.op.name = Some(v.into());
1581        self
1582    }
1583}
1584
1585// ---------------------------------------------------------------------------
1586// run
1587// ---------------------------------------------------------------------------
1588
1589/// The `--run` operation of `p4 extension`.
1590///
1591/// Executes an extension-supplied command on an instance configuration.
1592/// Selected with [`ExtensionEntry::run`].
1593///
1594/// Extra arguments the extension might need are passed at spawn time:
1595/// `ParameterizedSpawn<()>` spawns the command with no extra arguments, and
1596/// `ParameterizedSpawn<(S,)>` spawns it with an arbitrary number of them.
1597#[cfg(not(feature = "lt2020_1"))]
1598#[derive(Debug, Clone, Default)]
1599pub struct Run {
1600    inst: String,
1601}
1602
1603#[cfg(not(feature = "lt2020_1"))]
1604impl ExclusiveOption for Run {
1605    fn inject_args(&self, command: &mut Command) {
1606        command.arg("--run").arg(&self.inst);
1607    }
1608}
1609
1610#[cfg(not(feature = "lt2020_1"))]
1611impl<S, I> ParameterizedSpawn<(S,)> for Extension<Run>
1612where
1613    S: IntoIterator<Item = I>,
1614    I: AsRef<OsStr>,
1615{
1616    type Output = Child;
1617    type Error = std::io::Error;
1618
1619    /// Spawns `p4 extension --run instName` with the given extra arguments
1620    /// passed through to the extension, with piped standard output and error
1621    /// streams; use the returned [`Child`] handle to wait for it or interact
1622    /// with it.
1623    fn spawn_with(&mut self, (arguments,): (S,)) -> Result<Self::Output, Self::Error> {
1624        self.setup_command(&self.bin)
1625            .args(arguments)
1626            .stdout(Stdio::piped())
1627            .stderr(Stdio::piped())
1628            .spawn()
1629    }
1630}
1631
1632#[cfg(not(feature = "lt2020_1"))]
1633impl Extension<Run> {
1634    /// Returns the instance configuration whose extension is executed.
1635    pub fn get_inst(&self) -> &str {
1636        &self.op.inst
1637    }
1638
1639    /// Sets the instance configuration whose extension is executed.
1640    pub fn set_inst(&mut self, v: impl Into<String>) -> &mut Self {
1641        self.op.inst = v.into();
1642        self
1643    }
1644
1645    /// Sets the instance configuration whose extension is executed.
1646    pub fn inst(mut self, v: impl Into<String>) -> Self {
1647        self.op.inst = v.into();
1648        self
1649    }
1650}
1651
1652// ---------------------------------------------------------------------------
1653// list
1654// ---------------------------------------------------------------------------
1655
1656/// The `--type` value of `--list`.
1657///
1658/// Determines which data is reported: installed extensions, their global
1659/// configurations, or their instance configurations.
1660#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1661pub enum Type {
1662    /// Reports the installed extensions.
1663    Extensions,
1664
1665    /// Reports both global and instance configurations.
1666    Configs,
1667
1668    /// Reports global configurations.
1669    #[cfg(not(feature = "lt2020_1"))]
1670    Global,
1671
1672    /// Reports instance configurations.
1673    #[cfg(not(feature = "lt2020_1"))]
1674    Instance,
1675
1676    /// Reports the installed certificates.
1677    #[cfg(not(feature = "lt2022_2"))]
1678    Certs,
1679}
1680
1681impl Type {
1682    fn to_str(&self) -> &'static str {
1683        match self {
1684            Type::Extensions => "extensions",
1685            Type::Configs => "configs",
1686
1687            #[cfg(not(feature = "lt2020_1"))]
1688            Type::Global => "global",
1689
1690            #[cfg(not(feature = "lt2020_1"))]
1691            Type::Instance => "instance",
1692
1693            #[cfg(not(feature = "lt2022_2"))]
1694            Type::Certs => "certs",
1695        }
1696    }
1697}
1698
1699/// The `--list` operation of `p4 extension`.
1700///
1701/// Displays the installed extensions and their configurations, as selected
1702/// by [`--type`](Extension::ty). Selected with [`ExtensionEntry::list`].
1703#[derive(Debug, Clone, Default)]
1704pub struct List {
1705    ty: Option<Type>,
1706
1707    #[cfg(not(feature = "lt2022_2"))]
1708    path: Option<String>,
1709}
1710
1711impl ExclusiveOption for List {
1712    fn inject_args(&self, command: &mut Command) {
1713        command.arg("--list");
1714
1715        if let Some(ty) = &self.ty {
1716            command.arg("--type").arg(ty.to_str());
1717        }
1718
1719        #[cfg(not(feature = "lt2022_2"))]
1720        if let Some(path) = &self.path {
1721            command.arg("--path").arg(path);
1722        }
1723    }
1724}
1725
1726impl Extension<List> {
1727    /// # Description
1728    ///
1729    /// --type type
1730    ///
1731    #[cfg_attr(
1732        feature = "lt2022_2",
1733        doc = "Determines which data is reported: the installed extensions, their global configurations, or their instance configurations."
1734    )]
1735    #[cfg_attr(
1736        not(feature = "lt2022_2"),
1737        doc = "Must be supplied with `--list`. Possible type values: extensions, global, instance, certs, configs (both global and instance). For example, `p4 extension --list --type certs`."
1738    )]
1739    pub fn get_ty(&self) -> Option<Type> {
1740        self.op.ty
1741    }
1742
1743    /// # Description
1744    ///
1745    /// --type type
1746    ///
1747    #[cfg_attr(
1748        feature = "lt2022_2",
1749        doc = "Determines which data is reported: the installed extensions, their global configurations, or their instance configurations."
1750    )]
1751    #[cfg_attr(
1752        not(feature = "lt2022_2"),
1753        doc = "Must be supplied with `--list`. Possible type values: extensions, global, instance, certs, configs (both global and instance). For example, `p4 extension --list --type certs`."
1754    )]
1755    pub fn set_ty(&mut self, v: Type) -> &mut Self {
1756        self.op.ty = Some(v);
1757        self
1758    }
1759
1760    /// # Description
1761    ///
1762    /// --type type
1763    ///
1764    #[cfg_attr(
1765        feature = "lt2022_2",
1766        doc = "Determines which data is reported: the installed extensions, their global configurations, or their instance configurations."
1767    )]
1768    #[cfg_attr(
1769        not(feature = "lt2022_2"),
1770        doc = "Must be supplied with `--list`. Possible type values: extensions, global, instance, certs, configs (both global and instance). For example, `p4 extension --list --type certs`."
1771    )]
1772    pub fn ty(mut self, v: Type) -> Self {
1773        self.op.ty = Some(v);
1774        self
1775    }
1776
1777    /// # Description
1778    ///
1779    /// --path FileSpec
1780    ///
1781    /// Filters output to instance configurations that are path-based and
1782    /// whose path argument matches the given filespec.
1783    #[cfg(not(feature = "lt2022_2"))]
1784    pub fn get_path(&self) -> Option<&String> {
1785        self.op.path.as_ref()
1786    }
1787
1788    /// # Description
1789    ///
1790    /// --path FileSpec
1791    ///
1792    /// Filters output to instance configurations that are path-based and
1793    /// whose path argument matches the given filespec.
1794    #[cfg(not(feature = "lt2022_2"))]
1795    pub fn set_path(&mut self, v: impl Into<String>) -> &mut Self {
1796        self.op.path = Some(v.into());
1797        self
1798    }
1799
1800    /// # Description
1801    ///
1802    /// --path FileSpec
1803    ///
1804    /// Filters output to instance configurations that are path-based and
1805    /// whose path argument matches the given filespec.
1806    #[cfg(not(feature = "lt2022_2"))]
1807    pub fn path(mut self, v: impl Into<String>) -> Self {
1808        self.op.path = Some(v.into());
1809        self
1810    }
1811}
1812
1813#[cfg(test)]
1814mod tests {
1815    use super::*;
1816    use crate::cmd::args_of;
1817
1818    fn args(cmd: &impl SubCommand) -> Vec<String> {
1819        args_of(&cmd.setup_command("p4"))
1820    }
1821
1822    fn entry() -> ExtensionEntry {
1823        ExtensionEntry::new("p4".into(), GlobalOpts::new())
1824    }
1825
1826    /// Dry-run checks of the assembled `p4 extension` command line; no
1827    /// process is spawned.
1828    #[test]
1829    fn sample_injects_name() {
1830        assert_eq!(
1831            args(&entry().sample("ExtName")),
1832            ["extension", "--sample", "ExtName"]
1833        );
1834    }
1835
1836    #[test]
1837    fn package_without_sign() {
1838        assert_eq!(
1839            args(&entry().package("ExtName")),
1840            ["extension", "--package", "ExtName"]
1841        );
1842    }
1843
1844    #[cfg(not(feature = "lt2022_2"))]
1845    #[test]
1846    fn package_with_sign() {
1847        assert_eq!(
1848            args(&entry().package("ExtName").sign("keys")),
1849            ["extension", "--package", "ExtName", "--sign", "keys"]
1850        );
1851    }
1852
1853    #[test]
1854    fn install_dry_run_has_no_yes() {
1855        assert_eq!(
1856            args(&entry().install("ExtName.p4-extension")),
1857            ["extension", "--install", "ExtName.p4-extension"]
1858        );
1859    }
1860
1861    #[cfg(not(feature = "lt2020_1"))]
1862    #[test]
1863    fn install_ext_yes() {
1864        assert_eq!(
1865            args(&entry().install("ExtName.p4-extension").yes()),
1866            ["extension", "--install", "ExtName.p4-extension", "--yes"]
1867        );
1868    }
1869
1870    #[cfg(all(not(feature = "lt2020_1"), not(feature = "lt2022_2")))]
1871    #[test]
1872    fn install_ext_yes_and_allow_unsigned() {
1873        assert_eq!(
1874            args(
1875                &entry()
1876                    .install("ExtName.p4-extension")
1877                    .yes()
1878                    .allow_unsigned(true)
1879            ),
1880            [
1881                "extension",
1882                "--install",
1883                "ExtName.p4-extension",
1884                "--yes",
1885                "--allow-unsigned"
1886            ]
1887        );
1888    }
1889
1890    #[cfg(not(feature = "lt2022_2"))]
1891    #[test]
1892    fn install_cert_with_comment() {
1893        assert_eq!(
1894            args(&entry().install("cert.txt").comment("We trust ACTools")),
1895            [
1896                "extension",
1897                "--install",
1898                "cert.txt",
1899                "--cert",
1900                "--comment",
1901                "We trust ACTools"
1902            ]
1903        );
1904    }
1905
1906    #[cfg(not(feature = "lt2022_2"))]
1907    #[test]
1908    fn install_cert_without_comment() {
1909        assert_eq!(
1910            args(&entry().install("cert.txt").cert()),
1911            ["extension", "--install", "cert.txt", "--cert"]
1912        );
1913    }
1914
1915    #[test]
1916    fn delete_dry_run_has_no_options() {
1917        assert_eq!(
1918            args(&entry().delete("myCompany::ext")),
1919            ["extension", "--delete", "myCompany::ext"]
1920        );
1921    }
1922
1923    #[test]
1924    fn delete_ext_with_revision_path_yes() {
1925        assert_eq!(
1926            args(
1927                &entry()
1928                    .delete("myCompany::ext")
1929                    .revision("2")
1930                    .path("//depot/...")
1931                    .yes(true)
1932            ),
1933            [
1934                "extension",
1935                "--delete",
1936                "myCompany::ext",
1937                "--revision=2",
1938                "--path",
1939                "//depot/...",
1940                "--yes"
1941            ]
1942        );
1943    }
1944
1945    #[cfg(not(feature = "lt2020_1"))]
1946    #[test]
1947    fn delete_ext_with_name() {
1948        assert_eq!(
1949            args(&entry().delete("myCompany::ext").name("inst").yes(true)),
1950            [
1951                "extension",
1952                "--delete",
1953                "myCompany::ext",
1954                "--name",
1955                "inst",
1956                "--yes"
1957            ]
1958        );
1959    }
1960
1961    #[cfg(not(feature = "lt2022_2"))]
1962    #[test]
1963    fn delete_cert() {
1964        assert_eq!(
1965            args(&entry().delete("AA:BB:CC").cert()),
1966            ["extension", "--delete", "AA:BB:CC", "--cert"]
1967        );
1968    }
1969
1970    #[test]
1971    fn configure_global_only() {
1972        assert_eq!(
1973            args(&entry().configure("myCompany::ext")),
1974            ["extension", "--configure", "myCompany::ext"]
1975        );
1976    }
1977
1978    #[test]
1979    fn configure_instance_with_revision_and_stdin() {
1980        assert_eq!(
1981            args(
1982                &entry()
1983                    .configure("myCompany::ext")
1984                    .revision("2")
1985                    .name("inst")
1986                    .stdin()
1987            ),
1988            [
1989                "extension",
1990                "--configure",
1991                "myCompany::ext",
1992                "--revision=2",
1993                "-i",
1994                "--name",
1995                "inst"
1996            ]
1997        );
1998    }
1999
2000    #[test]
2001    fn configure_stdout() {
2002        assert_eq!(
2003            args(&entry().configure("myCompany::ext").stdout()),
2004            ["extension", "--configure", "myCompany::ext", "-o"]
2005        );
2006    }
2007
2008    #[cfg(not(feature = "lt2020_1"))]
2009    #[test]
2010    fn run_without_arguments() {
2011        assert_eq!(args(&entry().run("inst")), ["extension", "--run", "inst"]);
2012    }
2013
2014    /// The extra-argument form is a separate `ParameterizedSpawn` impl that
2015    /// appends the argument list at spawn time.
2016    #[cfg(not(feature = "lt2020_1"))]
2017    #[test]
2018    fn run_accepts_arbitrary_arguments_at_spawn() {
2019        fn assert_arguments_spawn<S, I>()
2020        where
2021            S: IntoIterator<Item = I>,
2022            I: AsRef<OsStr>,
2023            Extension<Run>: ParameterizedSpawn<(S,)>,
2024        {
2025        }
2026
2027        assert_arguments_spawn::<Vec<String>, String>();
2028        assert_arguments_spawn::<[&str; 2], &str>();
2029    }
2030
2031    #[test]
2032    fn list_with_type() {
2033        assert_eq!(
2034            args(&entry().list().ty(Type::Configs)),
2035            ["extension", "--list", "--type", "configs"]
2036        );
2037    }
2038
2039    #[cfg(not(feature = "lt2022_2"))]
2040    #[test]
2041    fn list_with_type_and_path() {
2042        assert_eq!(
2043            args(&entry().list().ty(Type::Instance).path("//depot/...")),
2044            [
2045                "extension",
2046                "--list",
2047                "--type",
2048                "instance",
2049                "--path",
2050                "//depot/..."
2051            ]
2052        );
2053    }
2054
2055    #[test]
2056    fn global_opts_are_injected_once() {
2057        let cmd = ExtensionEntry::new(
2058            "p4".into(),
2059            GlobalOpts::new().port("localhost:1666").quiet_mode(true),
2060        )
2061        .configure("myCompany::ext");
2062
2063        assert_eq!(
2064            args(&cmd),
2065            [
2066                "-p",
2067                "localhost:1666",
2068                "-q",
2069                "extension",
2070                "--configure",
2071                "myCompany::ext"
2072            ]
2073        );
2074    }
2075
2076    #[test]
2077    fn bin_accessors_update_executable_path() {
2078        let mut cmd = entry().sample("ExtName");
2079        assert_eq!(cmd.get_bin(), Path::new("p4"));
2080
2081        cmd.set_bin("/opt/perforce/bin/p4");
2082        assert_eq!(cmd.get_bin(), Path::new("/opt/perforce/bin/p4"));
2083
2084        let cmd = cmd.bin("p4custom");
2085        assert_eq!(cmd.get_bin(), Path::new("p4custom"));
2086    }
2087
2088    #[test]
2089    fn delete_ext_accessors_update_mode() {
2090        let mut cmd = entry().delete("myCompany::ext").yes();
2091        assert!(cmd.get_yes());
2092
2093        cmd.set_yes(false);
2094        assert!(!cmd.get_yes());
2095
2096        cmd.set_revision("3");
2097        assert_eq!(cmd.get_revision(), Some(&"3".to_string()));
2098    }
2099}