Replace StructOpt with Clap, and improve error printing
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
Aode (Lion) 2022-05-02 20:47:11 -05:00
parent 903134c7c7
commit 5f4f821474
3 changed files with 85 additions and 67 deletions

113
Cargo.lock generated
View file

@ -409,17 +409,41 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "clap" name = "clap"
version = "2.34.0" version = "3.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" checksum = "85a35a599b11c089a7f49105658d089b8f2cf0882993c17daf6de15285c2c35d"
dependencies = [ dependencies = [
"ansi_term",
"atty", "atty",
"bitflags", "bitflags",
"clap_derive",
"clap_lex",
"indexmap",
"lazy_static",
"strsim", "strsim",
"termcolor",
"textwrap", "textwrap",
"unicode-width", ]
"vec_map",
[[package]]
name = "clap_derive"
version = "3.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3aab4734e083b809aaf5794e14e756d1c798d2c69c7f7de7a09a2f5214993c1"
dependencies = [
"heck 0.4.0",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a37c35f1112dad5e6e0b1adaff798507497a18fceeb30cceb3bae7d1427b9213"
dependencies = [
"os_str_bytes",
] ]
[[package]] [[package]]
@ -762,6 +786,12 @@ dependencies = [
"unicode-segmentation", "unicode-segmentation",
] ]
[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.1.19" version = "0.1.19"
@ -1191,6 +1221,12 @@ dependencies = [
"tonic-build", "tonic-build",
] ]
[[package]]
name = "os_str_bytes"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64"
[[package]] [[package]]
name = "parking_lot" name = "parking_lot"
version = "0.12.0" version = "0.12.0"
@ -1244,6 +1280,7 @@ dependencies = [
"actix-web", "actix-web",
"anyhow", "anyhow",
"awc", "awc",
"clap",
"console-subscriber", "console-subscriber",
"dotenv", "dotenv",
"mime", "mime",
@ -1254,7 +1291,6 @@ dependencies = [
"ructe", "ructe",
"serde", "serde",
"serde_qs", "serde_qs",
"structopt",
"thiserror", "thiserror",
"tracing", "tracing",
"tracing-actix-web", "tracing-actix-web",
@ -1365,7 +1401,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62941722fb675d463659e49c4f3fe1fe792ff24fe5bbaa9c08cd3b98a1c354f5" checksum = "62941722fb675d463659e49c4f3fe1fe792ff24fe5bbaa9c08cd3b98a1c354f5"
dependencies = [ dependencies = [
"bytes", "bytes",
"heck", "heck 0.3.3",
"itertools", "itertools",
"lazy_static", "lazy_static",
"log", "log",
@ -1674,33 +1710,9 @@ dependencies = [
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.8.0" version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "structopt"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10"
dependencies = [
"clap",
"lazy_static",
"structopt-derive",
]
[[package]]
name = "structopt-derive"
version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "syn" name = "syn"
@ -1734,14 +1746,20 @@ dependencies = [
] ]
[[package]] [[package]]
name = "textwrap" name = "termcolor"
version = "0.11.0" version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
dependencies = [ dependencies = [
"unicode-width", "winapi-util",
] ]
[[package]]
name = "textwrap"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.31" version = "1.0.31"
@ -2165,12 +2183,6 @@ version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e8820f5d777f6224dc4be3632222971ac30164d4a258d595640799554ebfd99" checksum = "7e8820f5d777f6224dc4be3632222971ac30164d4a258d595640799554ebfd99"
[[package]]
name = "unicode-width"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
[[package]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.2.3" version = "0.2.3"
@ -2204,12 +2216,6 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.4" version = "0.9.4"
@ -2319,6 +2325,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "winapi-x86_64-pc-windows-gnu" name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"

View file

@ -20,6 +20,7 @@ actix-rt = "2.6.0"
actix-web = { version = "4.0.0", default-features = false } actix-web = { version = "4.0.0", default-features = false }
anyhow = "1.0" anyhow = "1.0"
awc = { version = "3.0.0", default-features = false } awc = { version = "3.0.0", default-features = false }
clap = { version = "3.1.15", features = ["derive", "env"] }
console-subscriber = "0.1" console-subscriber = "0.1"
dotenv = "0.15.0" dotenv = "0.15.0"
mime = "0.3" mime = "0.3"
@ -29,7 +30,6 @@ opentelemetry = { version = "0.17", features = ["rt-tokio"] }
opentelemetry-otlp = "0.10" opentelemetry-otlp = "0.10"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_qs = { version = "0.9", features = ["actix4"] } serde_qs = { version = "0.9", features = ["actix4"] }
structopt = "0.3.14"
thiserror = "1.0" thiserror = "1.0"
tracing = "0.1" tracing = "0.1"
tracing-error = "0.2" tracing-error = "0.2"

View file

@ -10,6 +10,7 @@ use actix_web::{
web, App, HttpRequest, HttpResponse, HttpResponseBuilder, HttpServer, ResponseError, web, App, HttpRequest, HttpResponse, HttpResponseBuilder, HttpServer, ResponseError,
}; };
use awc::Client; use awc::Client;
use clap::Parser;
use console_subscriber::ConsoleLayer; use console_subscriber::ConsoleLayer;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use opentelemetry::{ use opentelemetry::{
@ -22,7 +23,6 @@ use std::{
net::SocketAddr, net::SocketAddr,
time::{Duration, SystemTime}, time::{Duration, SystemTime},
}; };
use structopt::StructOpt;
use tracing_actix_web::TracingLogger; use tracing_actix_web::TracingLogger;
use tracing_awc::Tracing; use tracing_awc::Tracing;
use tracing_error::{ErrorLayer, SpanTrace}; use tracing_error::{ErrorLayer, SpanTrace};
@ -38,10 +38,9 @@ include!(concat!(env!("OUT_DIR"), "/templates.rs"));
const HOURS: u32 = 60 * 60; const HOURS: u32 = 60 * 60;
const DAYS: u32 = 24 * HOURS; const DAYS: u32 = 24 * HOURS;
// TODO: change structopt use to clap #[derive(Clone, Debug, Parser)]
#[derive(Clone, Debug, StructOpt)]
struct Config { struct Config {
#[structopt( #[clap(
short, short,
long, long,
env = "PICTRS_PROXY_ADDR", env = "PICTRS_PROXY_ADDR",
@ -50,7 +49,7 @@ struct Config {
)] )]
addr: SocketAddr, addr: SocketAddr,
#[structopt( #[clap(
short, short,
long, long,
env = "PICTRS_PROXY_UPSTREAM", env = "PICTRS_PROXY_UPSTREAM",
@ -59,7 +58,7 @@ struct Config {
)] )]
upstream: Url, upstream: Url,
#[structopt( #[clap(
short, short,
long, long,
env = "PICTRS_PROXY_DOMAIN", env = "PICTRS_PROXY_DOMAIN",
@ -68,14 +67,14 @@ struct Config {
)] )]
domain: Url, domain: Url,
#[structopt( #[clap(
long, long,
env = "PICTRS_PROXY_CONSOLE_BUFFER_SIZE", env = "PICTRS_PROXY_CONSOLE_BUFFER_SIZE",
help = "Number of events to buffer for the console subscriber. When unset, console will be disabled" help = "Number of events to buffer for the console subscriber. When unset, console will be disabled"
)] )]
console_event_buffer_size: Option<usize>, console_event_buffer_size: Option<usize>,
#[structopt( #[clap(
short, short,
long, long,
env = "PICTRS_PROXY_OPENTELEMETRY_URL", env = "PICTRS_PROXY_OPENTELEMETRY_URL",
@ -179,7 +178,7 @@ impl Config {
} }
} }
static CONFIG: Lazy<Config> = Lazy::new(Config::from_args); static CONFIG: Lazy<Config> = Lazy::new(Config::parse);
pub enum UploadResult<'a> { pub enum UploadResult<'a> {
Image(Image), Image(Image),
@ -340,7 +339,12 @@ where
impl std::fmt::Display for Error { impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{}", self.kind)?; let mut source: Option<&dyn std::error::Error> = Some(&self.kind);
while let Some(error) = source {
writeln!(f, "{}", error)?;
source = std::error::Error::source(error);
}
std::fmt::Display::fmt(&self.context, f) std::fmt::Display::fmt(&self.context, f)
} }
} }
@ -368,25 +372,24 @@ impl ResponseError for Error {
} }
} }
// TODO: make this error type better (do not forward display impl)
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
enum ErrorKind { enum ErrorKind {
#[error("{0}")] #[error("Upload failed")]
UploadFailed(String), UploadFailed(String),
#[error("{0}")] #[error("Error transfering data")]
Io(#[from] std::io::Error), Io(#[from] std::io::Error),
#[error("{0}")] #[error("Upstream request failed")]
SendRequest(#[from] awc::error::SendRequestError), SendRequest(#[from] awc::error::SendRequestError),
#[error("{0}")] #[error("Could not parse response")]
JsonPayload(#[from] awc::error::JsonPayloadError), JsonPayload(#[from] awc::error::JsonPayloadError),
#[error("{0}")] #[error("Invalid query string")]
Query(#[from] serde_qs::Error), Query(#[from] serde_qs::Error),
#[error("{0}")] #[error("Operation panicked")]
Canceled(#[from] actix_rt::task::JoinError), Canceled(#[from] actix_rt::task::JoinError),
} }