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