Skip to content

Commit

Permalink
refactor: Migrate clap to v4
Browse files Browse the repository at this point in the history
  • Loading branch information
jshearer committed Sep 16, 2024
1 parent 272ab50 commit 7adc312
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 51 deletions.
33 changes: 9 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ byteorder = "1.4"
caseless = "0.2"
chardetng = "0.1"
chrono = { version = "0.4", features = ["serde"] }
clap = { version = "3.2", features = ["derive", "env"] }
clap = { package = "clap", version = "4", features = ["derive", "env"] }
colored_json = "3"
comfy-table = "6.1"
# The `console_error_panic_hook` crate causes panics in a Rust WASM module to be logged
Expand Down
4 changes: 2 additions & 2 deletions crates/billing-integrations/src/stripe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const INVOICE_TYPE_KEY: &str = "estuary.dev/invoice_type";
const BILLING_PERIOD_START_KEY: &str = "estuary.dev/period_start";
const BILLING_PERIOD_END_KEY: &str = "estuary.dev/period_end";

#[derive(Debug, Clone, Copy, clap::ArgEnum)]
#[derive(Debug, Clone, Copy, clap::ValueEnum)]
#[clap(rename_all = "kebab_case")]
enum ChargeType {
AutoCharge,
Expand All @@ -37,7 +37,7 @@ pub struct PublishInvoice {
#[clap(long, conflicts_with("tenants"))]
all_tenants: bool,
/// The month to generate invoices for, in format "YYYY-MM-DD"
#[clap(long, parse(try_from_str = parse_date))]
#[clap(long, value_parser = parse_date)]
month: NaiveDate,
/// Whether to delete and recreate finalized invoices
#[clap(long)]
Expand Down
8 changes: 5 additions & 3 deletions crates/flow_cli_common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
//! Contains helpers and things that are used by all Flow rust executables.
mod logging;

use clap::AppSettings::NoAutoHelp;

pub use logging::{init_logging, LogArgs, LogFormat, LogLevel};

/// An arguments container that accepts all arguments verbatim. This is used by external
/// subcommands to allow all their argument parsing to be handled by the external binary.
// These settings prevent clap from automatically using its own help
// handling, so that --help and -h are simply forwarded to the external subcommand.
#[derive(Debug, clap::Args)]
#[clap(setting = NoAutoHelp, disable_help_flag = true, allow_hyphen_values = true)]
#[clap(
disable_help_subcommand = true,
disable_help_flag = true,
allow_hyphen_values = true
)]
pub struct ExternalArgs {
pub args: Vec<String>,
}
Expand Down
13 changes: 7 additions & 6 deletions crates/flow_cli_common/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
#[derive(Debug, clap::Args)]
pub struct LogArgs {
/// The log verbosity. Can be one of trace|debug|info|warn|error|off
#[clap(
#[arg(
long = "log.level",
default_value_t = LogLevel::Warn,
group = "logging",
ignore_case(true),
arg_enum,
ignore_case = true,
value_enum,
global = true
)]
pub level: LogLevel,
#[clap(long = "log.format", arg_enum, group = "logging", global = true)]

#[arg(long = "log.format", value_enum, global = true, group = "logging")]
pub format: Option<LogFormat>,
}

#[derive(Debug, clap::ArgEnum, Clone, Copy)]
#[derive(Debug, clap::ValueEnum, Clone, Copy)]
pub enum LogLevel {
Trace,
Debug,
Expand All @@ -41,7 +42,7 @@ impl ToString for LogLevel {
}

/// The format for logs.
#[derive(Debug, clap::ArgEnum, Clone, Copy)]
#[derive(Debug, clap::ValueEnum, Clone, Copy)]
pub enum LogFormat {
/// Logs are written to stderr in jsonl format. This format is very compatible with Flow's log
/// parsing, which means that they will get forwarded with the proper level and will retain the
Expand Down
4 changes: 2 additions & 2 deletions crates/flowctl/src/auth/roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub enum Command {
Revoke(Revoke),
}

#[derive(Debug, Copy, Clone, Serialize, Deserialize, clap::ArgEnum)]
#[derive(Debug, Copy, Clone, Serialize, Deserialize, clap::ValueEnum)]
#[serde(rename_all = "camelCase")]
enum Capability {
Read,
Expand All @@ -58,7 +58,7 @@ pub struct Grant {
#[clap(long)]
object_role: String,
/// Granted capability of the subject to the object role.
#[clap(long, arg_enum)]
#[clap(long, value_enum)]
capability: Capability,
/// Free-form details of the grant, such as a reason or audit log message.
#[clap(long)]
Expand Down
6 changes: 3 additions & 3 deletions crates/flowctl/src/collection/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ pub async fn read_collection(ctx: &mut crate::CliContext, args: &ReadArgs) -> an
.output
.filter(|ot| *ot != OutputType::Json)
{
let name = clap::ValueEnum::to_possible_value(&naughty_output_type)
.expect("possible value cannot be None")
.get_name();
let clap_enum = clap::ValueEnum::to_possible_value(&naughty_output_type)
.expect("possible value cannot be None");
let name = clap_enum.get_name();
anyhow::bail!(
"cannot use --output {name} when reading collection data (only json is supported)"
);
Expand Down
8 changes: 1 addition & 7 deletions crates/flowctl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::fmt::Debug;

use anyhow::Context;
use clap::AppSettings;
use clap::Parser;

mod auth;
Expand All @@ -26,12 +25,7 @@ use poll::poll_while_queued;

/// A command-line tool for working with Estuary Flow.
#[derive(Debug, Parser)]
#[clap(
author,
about,
version,
global_setting = AppSettings::DeriveDisplayOrder
)]
#[command(author, about, version, next_display_order = None)]
pub struct Cli {
/// Configuration profile to use.
///
Expand Down
6 changes: 3 additions & 3 deletions crates/flowctl/src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub struct Get {
#[clap(long)]
table: String,
/// Optional query parameters.
#[clap(long, parse(try_from_str = parse_key_val), number_of_values = 1)]
#[clap(long, value_parser = parse_key_val::<String, String>, number_of_values = 1)]
query: Vec<(String, String)>,
}

Expand All @@ -83,7 +83,7 @@ pub struct Update {
#[clap(long)]
table: String,
/// Optional query parameters.
#[clap(long, parse(try_from_str = parse_key_val), number_of_values = 1)]
#[clap(long, value_parser = parse_key_val::<String, String>, number_of_values = 1)]
query: Vec<(String, String)>,
/// Serialized JSON argument of the request.
#[clap(long)]
Expand All @@ -97,7 +97,7 @@ pub struct Rpc {
#[clap(long)]
function: String,
/// Optional query parameters.
#[clap(long, parse(try_from_str = parse_key_val), number_of_values = 1)]
#[clap(long, value_parser = parse_key_val::<String, String>, number_of_values = 1)]
query: Vec<(String, String)>,
/// Serialized JSON argument of the request.
#[clap(long)]
Expand Down

0 comments on commit 7adc312

Please sign in to comment.