Merge pull request #215 from storytold/brandon-fix-clap-upgrade

Fix clap in various CLI tools post 2.x -> 4.x upgrade
This commit is contained in:
Brandon Thomas authored and GitHub committed 2023-12-14 18:23:46 -05:00
commit 92b5a4c90b
12 files changed
+63 -116

No files matched your search

Generated
-2
View File
@@ -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",
+1 -1
View File
@@ -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.
+7 -18
View File
@@ -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<CliArgs> {
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())
}
+1 -1
View File
@@ -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
@@ -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<CliArgs> {
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),
})
}
+1 -1
View File
@@ -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
+24 -39
View File
@@ -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<CliArgs> {
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<String>,
#[arg(name="elasticsearch", long="elasticsearch", help="production or development")]
elasticsearch: Option<String>,
}
pub fn parse_cli_args() -> AnyhowResult<ParsedArgs> {
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<Environment> {
let value = matches.value_of(key)
.map(|s| s.to_lowercase());
Ok(match value.as_deref() {
fn to_environment(environment: Option<&str>) -> AnyhowResult<Environment> {
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<Action> {
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<Action> {
let action = Action::from_str(value)
.map_err(|err| {
+1 -1
View File
@@ -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"] }
+14 -20
View File
@@ -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?;
@@ -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"
@@ -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"
@@ -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 =