pact_broker_cli/
cli.rs

1use clap::builder::ArgPredicate;
2use clap::{Arg, Command};
3
4pub mod pact_broker;
5pub mod pact_broker_client;
6pub mod pactflow;
7pub mod pactflow_client;
8pub mod utils;
9pub fn build_cli() -> Command {
10    let app = pact_broker_client::add_pact_broker_client_command()
11        .arg_required_else_help(true)
12        .version(env!("CARGO_PKG_VERSION"))
13        .about("A pact cli tool")
14        .args(add_logging_arguments())
15        .subcommand(
16            pactflow_client::add_pactflow_client_command().version(env!("CARGO_PKG_VERSION")),
17        )
18        .subcommand(add_completions_subcommand());
19    app
20}
21
22pub fn add_logging_arguments() -> Vec<Arg> {
23    vec![
24        Arg::new("log-level")
25            .long("log-level")
26            .global(true)
27            .value_name("LEVEL")
28            .help("Set the log level (none, off, error, warn, info, debug, trace)")
29            .value_parser(clap::builder::PossibleValuesParser::new([
30                "off", "none", "error", "warn", "info", "debug", "trace",
31            ]))
32            .default_value("off")
33            .default_value_if("verbose", "true", Some("info")),
34        Arg::new("verbose")
35            .long("verbose")
36            .global(true)
37            .action(clap::ArgAction::SetTrue)
38            .help("DEPRECATED: Compatibility layer for pact_broker-client. Sets log level to info.")
39            .hide(true),
40    ]
41}
42pub fn add_output_arguments(
43    value_parser_args: Vec<&'static str>,
44    default_value: &'static str,
45) -> Vec<Arg> {
46    vec![
47        Arg::new("output")
48            .short('o')
49            .long("output")
50            .value_name("OUTPUT")
51            .value_parser(clap::builder::PossibleValuesParser::new(&value_parser_args))
52            .default_value(default_value)
53            .value_name("OUTPUT")
54            .help(format!("Value must be one of {:?}", value_parser_args)),
55    ]
56}
57
58pub fn add_ssl_arguments() -> Vec<Arg> {
59    vec![
60        Arg::new("ssl-certificate")
61            .short('c')
62            .long("ssl-certificate")
63            .num_args(1)
64            .help("The path to a valid SSL certificate file")
65            .required(false)
66            .value_name("SSL_CERT_FILE")
67            .env("SSL_CERT_FILE"),
68        Arg::new("skip-ssl-verification")
69            .long("skip-ssl-verification")
70            .num_args(0)
71            .help("Skip SSL certificate verification")
72            .required(false)
73            .value_name("SSL_SKIP_VERIFICATION")
74            .env("SSL_SKIP_VERIFICATION"),
75        Arg::new("ssl-trust-store")
76            .long("ssl-trust-store")
77            .num_args(1)
78            .default_value("true")
79            .value_parser(clap::builder::BoolValueParser::new())
80            .help("Use the system's root trust store for SSL verification")
81            .required(false)
82            .value_name("SSL_TRUST_STORE")
83            .env("SSL_TRUST_STORE"),
84    ]
85}
86
87fn add_completions_subcommand() -> Command {
88    Command::new("completions") 
89    .about("Generates completion scripts for your shell")
90    .arg(Arg::new("shell")
91        .value_name("SHELL")
92        .required(true)
93        .value_parser(clap::builder::PossibleValuesParser::new(&["bash", "fish", "zsh", "powershell", "elvish"]))
94        .help("The shell to generate the script for"))
95    .arg(Arg::new("dir")
96        .short('d')
97        .long("dir")
98        .value_name("DIRECTORY")
99        .required(false)
100        .default_value(".")
101        .num_args(1)
102        .value_parser(clap::builder::NonEmptyStringValueParser::new())
103        .help("The directory to write the shell completions to, default is the current directory"))
104}