From e9cd505e0bbe5a2c6a547e0fc75d84f3ec026f5c Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Thu, 14 Dec 2023 18:11:34 -0500 Subject: [PATCH] Fix clap in various CLI tools post 2.x -> 4.x upgrade --- Cargo.lock | 2 - crates/cli/dev_database_seed/Cargo.toml | 2 +- crates/cli/dev_database_seed/src/cli_args.rs | 25 +++----- crates/cli/dev_upload_media_file/Cargo.toml | 2 +- .../cli/dev_upload_media_file/src/cli_args.rs | 29 ++++----- crates/cli/elasticsearch-cli/Cargo.toml | 2 +- crates/cli/elasticsearch-cli/src/cli_args.rs | 63 +++++++------------ crates/cli/scrape_test/Cargo.toml | 2 +- crates/cli/scrape_test/src/main.rs | 34 +++++----- .../service/job/email_sender_job/Cargo.toml | 1 - .../service/job/tts_inference_job/Cargo.toml | 1 - .../service/job/tts_inference_job/src/main.rs | 16 +---- 12 files changed, 63 insertions(+), 116 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cc0a9d9644..7af174d2be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2068,7 +2068,6 @@ dependencies = [ "bootstrap", "buckets", "chrono", - "clap", "cloud_storage", "concurrency", "config", @@ -6707,7 +6706,6 @@ version = "0.0.1" dependencies = [ "anyhow", "chrono", - "clap", "cloud_storage", "collections", "config", diff --git a/crates/cli/dev_database_seed/Cargo.toml b/crates/cli/dev_database_seed/Cargo.toml index b065225a7f..26c93882f5 100644 --- a/crates/cli/dev_database_seed/Cargo.toml +++ b/crates/cli/dev_database_seed/Cargo.toml @@ -33,7 +33,7 @@ my-workspace-hack = { version = "0.1", path = "../../my-workspace-hack" } # External log = "0.4.14" -clap = "4.4.8" # TODO: This is really out of date, but has conflicts when updated +clap = "4.4.8" rand = "0.8.5" # Runtime # NB: We switched to Tokio from `async-std` for broader ecosystem support. diff --git a/crates/cli/dev_database_seed/src/cli_args.rs b/crates/cli/dev_database_seed/src/cli_args.rs index 6fb2e244c4..6642fe625e 100644 --- a/crates/cli/dev_database_seed/src/cli_args.rs +++ b/crates/cli/dev_database_seed/src/cli_args.rs @@ -1,28 +1,17 @@ -use clap::{App, Arg}; +use clap::Parser; use errors::AnyhowResult; +#[derive(Parser, Debug)] +#[command(name="dev-database-seed")] pub struct CliArgs { + #[arg(name="seed_cloud_bucket", long="bucket", help="Seed the cloud bucket with files", required=false, default_value_t=false)] pub seed_cloud_bucket: bool, + + #[arg(name="seed_elasticsearch", long="elasticsearch", help="Seed the local elasticsearch", required=false, default_value_t=false)] pub seed_elasticsearch: bool, } pub fn parse_cli_args() -> AnyhowResult { - let matches = App::new("dev-database-seed") - .arg(Arg::with_name("seed_cloud_bucket") - .long("bucket") - .help("Seed the cloud bucket with files") - .takes_value(false) - .required(false)) - .arg(Arg::with_name("seed_elasticsearch") - .long("elasticsearch") - .help("Seed the local elasticsearch") - .takes_value(false) - .required(false)) - .get_matches(); - - Ok(CliArgs { - seed_cloud_bucket: matches.is_present("seed_cloud_bucket"), - seed_elasticsearch: matches.is_present("seed_elasticsearch"), - }) + Ok(CliArgs::parse()) } diff --git a/crates/cli/dev_upload_media_file/Cargo.toml b/crates/cli/dev_upload_media_file/Cargo.toml index 9d928ea9e1..ad35f9d3b9 100644 --- a/crates/cli/dev_upload_media_file/Cargo.toml +++ b/crates/cli/dev_upload_media_file/Cargo.toml @@ -29,7 +29,7 @@ tokens = { path = "../../_types/tokens" } my-workspace-hack = { version = "0.1", path = "../../my-workspace-hack" } # External -clap = "4.4.8" # TODO: This is really out of date, but has conflicts when updated +clap = "4.4.8" log = "0.4.14" # Runtime diff --git a/crates/cli/dev_upload_media_file/src/cli_args.rs b/crates/cli/dev_upload_media_file/src/cli_args.rs index 1cf2ca590f..32ad7a5f4c 100644 --- a/crates/cli/dev_upload_media_file/src/cli_args.rs +++ b/crates/cli/dev_upload_media_file/src/cli_args.rs @@ -1,27 +1,24 @@ use std::path::PathBuf; -use clap::{App, Arg}; -use errors::{anyhow, AnyhowResult}; + +use clap::Parser; + +use errors::AnyhowResult; + +#[derive(Parser, Debug)] +#[command(name="dev-upload-media-file")] +pub struct RawArgs { + #[arg(name="file", short='f', long="file", help="File to upload", required=true)] + pub file_path: String, +} pub struct CliArgs { pub file_path: PathBuf, } pub fn parse_cli_args() -> AnyhowResult { - let matches = App::new("dev_upload_media") - .arg(Arg::with_name("file") - .short("f") - .long("file") - .value_name("FILE") - .help("File to upload") - .takes_value(true) - .required(true)) - .get_matches(); - - let file_path = matches.value_of("file") - .map(|val| val.trim().to_string()) - .ok_or(anyhow!("no file path supplied"))?; + let args = RawArgs::parse(); Ok(CliArgs { - file_path: PathBuf::from(file_path), + file_path: PathBuf::from(args.file_path), }) } diff --git a/crates/cli/elasticsearch-cli/Cargo.toml b/crates/cli/elasticsearch-cli/Cargo.toml index 9b0af1cde7..6921e54cda 100644 --- a/crates/cli/elasticsearch-cli/Cargo.toml +++ b/crates/cli/elasticsearch-cli/Cargo.toml @@ -29,7 +29,7 @@ my-workspace-hack = { version = "0.1", path = "../../my-workspace-hack" } # External log = "0.4.14" -clap = "4.4.8" # TODO: This is really out of date, but has conflicts when updated +clap = "4.4.8" strum = { version = "0.25", features = ["derive"] } # Runtime diff --git a/crates/cli/elasticsearch-cli/src/cli_args.rs b/crates/cli/elasticsearch-cli/src/cli_args.rs index 5c2c7e1a41..9b7a25e815 100644 --- a/crates/cli/elasticsearch-cli/src/cli_args.rs +++ b/crates/cli/elasticsearch-cli/src/cli_args.rs @@ -1,12 +1,12 @@ use std::str::FromStr; -use clap::{App, Arg, ArgMatches}; +use clap::Parser; use strum::{EnumCount, EnumString, IntoEnumIterator}; use strum::EnumIter; use errors::{anyhow, AnyhowResult, bail}; -pub struct CliArgs { +pub struct ParsedArgs { pub mysql_environment: Environment, pub elasticsearch_environment: Environment, pub action: Action, @@ -25,53 +25,38 @@ pub enum Action { SearchTts, } -pub fn parse_cli_args() -> AnyhowResult { - let matches = App::new("dev-database-seed") - .arg(Arg::with_name("action") - .long("action") - .help("action to take") - .takes_value(true) - .required(true)) - .arg(Arg::with_name("mysql") - .long("mysql") - .help("Production or development") - .takes_value(true) - .required(false)) - .arg(Arg::with_name("elasticsearch") - .long("elasticsearch") - .help("Production or development") - .takes_value(true) - .required(false)) - .get_matches(); +#[derive(Parser, Debug)] +#[command(name="elasticsearch-cli")] +pub struct Args { + #[arg(name="action", long="action", help="action to take", required=true)] + action: String, - Ok(CliArgs { - mysql_environment: to_environment(&matches, "mysql")?, - elasticsearch_environment: to_environment(&matches, "elasticsearch")?, - action: get_action(&matches)?, + #[arg(name="mysql", long="mysql", help="production or development")] + mysql: Option, + + #[arg(name="elasticsearch", long="elasticsearch", help="production or development")] + elasticsearch: Option, +} + +pub fn parse_cli_args() -> AnyhowResult { + let args = Args::parse(); + + Ok(ParsedArgs { + mysql_environment: to_environment(args.mysql.as_deref())?, + elasticsearch_environment: to_environment(args.elasticsearch.as_deref())?, + action: action_from_str(&args.action)?, }) } -fn to_environment(matches: &ArgMatches, key: &str) -> AnyhowResult { - let value = matches.value_of(key) - .map(|s| s.to_lowercase()); - - Ok(match value.as_deref() { +fn to_environment(environment: Option<&str>) -> AnyhowResult { + Ok(match environment { None => Environment::Development, Some("dev") | Some("development") => Environment::Development, Some("prod") | Some("production") => Environment::Production, - _ => bail!("invalid environment: {:?}", value), + _ => bail!("invalid environment: {:?}", environment), }) } -fn get_action(matches: &ArgMatches) -> AnyhowResult { - let action = matches.value_of("action") - .map(|s| action_from_str(s)) - .transpose()? - .ok_or(anyhow!("no action provided"))?; - - Ok(action) -} - fn action_from_str(value: &str) -> AnyhowResult { let action = Action::from_str(value) .map_err(|err| { diff --git a/crates/cli/scrape_test/Cargo.toml b/crates/cli/scrape_test/Cargo.toml index f68f20dc30..64dfd5adb6 100644 --- a/crates/cli/scrape_test/Cargo.toml +++ b/crates/cli/scrape_test/Cargo.toml @@ -22,5 +22,5 @@ web_scrapers = { path = "../../lib/web_scrapers" } my-workspace-hack = { version = "0.1", path = "../../my-workspace-hack" } # External -clap = "4.4.8" # TODO: This is really out of date, but has conflicts when updated +clap = "4.4.8" tokio = { version = "1.34.0", features = ["macros"] } diff --git a/crates/cli/scrape_test/src/main.rs b/crates/cli/scrape_test/src/main.rs index 71abbd5d11..42d3c8a51a 100644 --- a/crates/cli/scrape_test/src/main.rs +++ b/crates/cli/scrape_test/src/main.rs @@ -1,30 +1,24 @@ -use clap::{App, Arg}; +use clap::Parser; use errors::{anyhow, AnyhowResult}; use web_scrapers::scrape_supported_webpage::scrape_supported_webpage; +#[derive(Parser, Debug)] +#[command(name = "scrape_test")] +pub struct Args { + #[arg(name="url", short='u', long = "url", help = "URL of the page to scrape", required = true)] + url: String, + + #[arg(name="print_html", long = "html", help = "Print the HTML scraped", required = true)] + print_html: bool, +} + #[tokio::main] pub async fn main() -> AnyhowResult<()> { - let matches = App::new("scrape_test") - .arg(Arg::with_name("url") - .short("u") - .long("url") - .value_name("URL") - .help("URL of the page to scrape") - .takes_value(true) - .required(true)) - .arg(Arg::with_name("print_html") - .long("html") - .help("Print the HTML scraped") - .takes_value(false) - .required(false)) - .get_matches(); + let args = Args::parse(); - let url = matches.value_of("url") - .map(|val| val.trim().to_string()) - .ok_or(anyhow!("no url supplied"))?; - - let print_html = matches.is_present("print_html"); + let url = args.url.trim().to_string(); + let print_html = args.print_html; let scrape_result = scrape_supported_webpage(&url).await?; diff --git a/crates/service/job/email_sender_job/Cargo.toml b/crates/service/job/email_sender_job/Cargo.toml index 07db875a2e..7fd93708f7 100644 --- a/crates/service/job/email_sender_job/Cargo.toml +++ b/crates/service/job/email_sender_job/Cargo.toml @@ -47,7 +47,6 @@ my-workspace-hack = { version = "0.1", path = "../../../my-workspace-hack" } # External anyhow = "1.0.75" chrono = { version = "0.4.22", features = ["serde"] } -clap = "4.4.8" # TODO: This is really out of date, but has conflicts when updated dotenv = "0.15.0" hostname = "0.3.1" lfu = "0.2.5" diff --git a/crates/service/job/tts_inference_job/Cargo.toml b/crates/service/job/tts_inference_job/Cargo.toml index 02b6a01ed5..3695ed9a29 100644 --- a/crates/service/job/tts_inference_job/Cargo.toml +++ b/crates/service/job/tts_inference_job/Cargo.toml @@ -33,7 +33,6 @@ my-workspace-hack = { version = "0.1", path = "../../../my-workspace-hack" } # External anyhow = "1.0.75" chrono = { version = "0.4.22", features = ["serde"] } -clap = "4.4.8" # TODO: This is really out of date, but has conflicts when updated dotenv = "0.15.0" hostname = "0.3.1" lfu = "0.2.5" diff --git a/crates/service/job/tts_inference_job/src/main.rs b/crates/service/job/tts_inference_job/src/main.rs index db471482ac..18b2dc41cf 100644 --- a/crates/service/job/tts_inference_job/src/main.rs +++ b/crates/service/job/tts_inference_job/src/main.rs @@ -18,7 +18,6 @@ use std::path::PathBuf; use std::time::Duration; -use clap::{App, Arg}; use log::{error, info, warn}; use r2d2_redis::r2d2; use r2d2_redis::RedisConnectionManager; @@ -93,15 +92,6 @@ async fn main() -> AnyhowResult<()> { // This file should only contain *development* secrets, never production. let _ = dotenv::from_filename(".env-secrets").ok(); - let matches = App::new("tts-inference-job") - .arg(Arg::with_name("sidecar_hostname") - .long("sidecar_hostname") - .value_name("HOSTNAME") - .help("Hostname for the TTS inference sidecar") - .takes_value(true) - .required(false)) - .get_matches(); - info!("Obtaining worker hostname..."); let server_hostname = hostname::get() @@ -150,13 +140,9 @@ async fn main() -> AnyhowResult<()> { Some(bucket_timeout), )?; - let mut sidecar_hostname = + let sidecar_hostname = easyenv::get_env_string_required(ENV_TTS_INFERENCE_SIDECAR_HOSTNAME)?; - if let Some(hostname) = matches.value_of("sidecar_hostname") { - sidecar_hostname = hostname.to_string(); - } - info!("Sidecar hostname: {:?}", sidecar_hostname); let tts_inference_sidecar_client =