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

View file

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

View file

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