Skip to content

Update clap dependency from v3 to v4.1 #1053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
538 changes: 538 additions & 0 deletions .vscode/launch.json

Large diffs are not rendered by default.

70 changes: 64 additions & 6 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ async-trait = "0.1"
bindle = { workspace = true }
bytes = "1.1"
chrono = "0.4"
clap = { version = "3.1.15", features = ["derive", "env"] }
clap = { version = "4.1", features = ["derive", "env"] }
clap_complete = { version = "4.1", optional = true }
clap_complete_fig = { version = "4.1", optional = true }
cloud = { path = "crates/cloud" }
cloud-openapi = { git = "https://github.com/fermyon/cloud-openapi" }
comfy-table = "5.0"
Expand Down Expand Up @@ -80,12 +82,13 @@ vergen = { version = "7", default-features = false, features = [
] }

[features]
default = []
default = ["generate-completions"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added completions here just to make sure the less conventional features like build's --up option aren't breaking it.

I'll make sure to remove once this is ready for review again.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, if this provides us with a better and more consistent way to #1032 then I'd love to have your feedback on that PR (or your own PR). Thanks! (I agree on not trying to shoehorn it into this one, of course.)

e2e-tests = []
outbound-redis-tests = []
config-provider-tests = []
outbound-pg-tests = []
outbound-mysql-tests = []
generate-completions = ["dep:clap_complete", "dep:clap_complete_fig"]

[workspace]
members = [
Expand Down
2 changes: 1 addition & 1 deletion crates/abi-conformance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = { workspace = true }
[dependencies]
anyhow = "1.0.44"
cap-std = "0.26.1"
clap = { version = "3.1.15", features = ["derive", "env"] }
clap = { version = "4.1", features = ["derive", "env"] }
rand = "0.8.5"
rand_chacha = "0.3.1"
rand_core = "0.6.3"
Expand Down
8 changes: 4 additions & 4 deletions crates/abi-conformance/src/bin/spin-abi-conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ use std::{
use wasmtime::{Config, Engine, Module};

#[derive(Parser)]
#[clap(author, version, about)]
#[command(author, version, about)]
pub struct Options {
/// Name of Wasm file to test (or stdin if not specified)
#[clap(short, long)]
#[arg(short, long)]
pub input: Option<PathBuf>,

/// Name of JSON file to write report to (or stdout if not specified)
#[clap(short, long)]
#[arg(short, long)]
pub output: Option<PathBuf>,

/// Name of TOML configuration file to use
#[clap(short, long)]
#[arg(short, long)]
pub config: Option<PathBuf>,
}

Expand Down
2 changes: 1 addition & 1 deletion crates/http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ doctest = false
[dependencies]
anyhow = "1.0"
async-trait = "0.1"
clap = "3"
clap = "4.1"
futures = "0.3"
futures-util = "0.3.8"
http = "0.2"
Expand Down
97 changes: 68 additions & 29 deletions crates/http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ mod wagi;
use std::{
collections::HashMap,
future::ready,
net::{Ipv4Addr, SocketAddr, ToSocketAddrs},
net::{Ipv4Addr, SocketAddr, SocketAddrV4, ToSocketAddrs},
path::PathBuf,
sync::Arc,
sync::Arc, str::FromStr, fmt::Display,
};

use anyhow::{Context, Error, Result};
Expand Down Expand Up @@ -56,18 +56,71 @@ pub struct HttpTrigger {
component_trigger_configs: HashMap<String, HttpTriggerConfig>,
}

#[derive(Args)]
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub struct SocketAddress(SocketAddr);

impl SocketAddress {
pub fn new(v4: Ipv4Addr, port: u16) -> Self {
Self(SocketAddr::V4(SocketAddrV4::new(v4, port)))
}
}

impl Into<SocketAddr> for SocketAddress {
fn into(self) -> SocketAddr {
return self.0
}
}

impl ToSocketAddrs for SocketAddress {
type Iter = <SocketAddr as ToSocketAddrs>::Iter;

fn to_socket_addrs(&self) -> std::io::Result<Self::Iter> {
self.0.to_socket_addrs()
}
}



impl Display for SocketAddress {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}

impl Default for SocketAddress {
fn default() -> Self {
Self::new(Ipv4Addr::LOCALHOST, 3000)
}
}

impl FromStr for SocketAddress {
type Err = anyhow::Error;
fn from_str(addr: &str) -> Result<SocketAddress> {
let addrs: Vec<SocketAddr> = addr.to_socket_addrs()?.collect();
// Prefer 127.0.0.1 over e.g. [::1] because CHANGE IS HARD
if let Some(addr) = addrs
.iter()
.find(|addr| addr.is_ipv4() && addr.ip() == Ipv4Addr::LOCALHOST)
{
return Ok(Self(*addr));
}
// Otherwise, take the first addr (OS preference)
addrs.into_iter().next().context("couldn't resolve address").map(Self)
}
}

#[derive(Args, Clone)]
pub struct CliArgs {
/// IP address and port to listen on
#[clap(long = "listen", default_value = "127.0.0.1:3000", value_parser = parse_listen_addr)]
pub address: SocketAddr,
#[arg(long = "listen", default_value_t = SocketAddress::default())]
pub address: SocketAddress,

/// The path to the certificate to use for https, if this is not set, normal http will be used. The cert should be in PEM format
#[clap(long, env = "SPIN_TLS_CERT", requires = "tls-key")]
#[arg(long, env = "SPIN_TLS_CERT", requires = "tls_key")]
pub tls_cert: Option<PathBuf>,

/// The path to the certificate key to use for https, if this is not set, normal http will be used. The key should be in PKCS#8 format
#[clap(long, env = "SPIN_TLS_KEY", requires = "tls-cert")]
#[arg(long, env = "SPIN_TLS_KEY", requires = "tls_cert")]
pub tls_key: Option<PathBuf>,
}

Expand Down Expand Up @@ -177,11 +230,10 @@ impl TriggerExecutor for HttpTrigger {
}

if let Some(tls) = tls {
self.serve_tls(listen_addr, tls).await?
self.serve_tls(listen_addr, tls).await
} else {
self.serve(listen_addr).await?
};
Ok(())
self.serve(listen_addr).await
}
}
}

Expand Down Expand Up @@ -293,7 +345,7 @@ impl HttpTrigger {
.body(Body::empty())?)
}

async fn serve(self, listen_addr: SocketAddr) -> Result<()> {
async fn serve(self, listen_addr: SocketAddress) -> Result<()> {
let self_ = Arc::new(self);
let make_service = make_service_fn(|conn: &AddrStream| {
let self_ = self_.clone();
Expand All @@ -307,14 +359,14 @@ impl HttpTrigger {
}
});

Server::try_bind(&listen_addr)
Server::try_bind(&listen_addr.into())
.with_context(|| format!("Unable to listen on {}", listen_addr))?
.serve(make_service)
.await?;
Ok(())
}

async fn serve_tls(self, listen_addr: SocketAddr, tls: TlsConfig) -> Result<()> {
async fn serve_tls(self, listen_addr: SocketAddress, tls: TlsConfig) -> Result<()> {
let self_ = Arc::new(self);
let make_service = make_service_fn(|conn: &TlsStream<TcpStream>| {
let self_ = self_.clone();
Expand All @@ -340,7 +392,7 @@ impl HttpTrigger {
}
});

let listener = TcpListener::bind(&listen_addr)
let listener = TcpListener::bind::<SocketAddr>(listen_addr.into())
.await
.with_context(|| format!("Unable to listen on {}", listen_addr))?;

Expand Down Expand Up @@ -369,19 +421,6 @@ pub struct AppInfo {
pub bindle_version: Option<String>,
}

fn parse_listen_addr(addr: &str) -> anyhow::Result<SocketAddr> {
let addrs: Vec<SocketAddr> = addr.to_socket_addrs()?.collect();
// Prefer 127.0.0.1 over e.g. [::1] because CHANGE IS HARD
if let Some(addr) = addrs
.iter()
.find(|addr| addr.is_ipv4() && addr.ip() == Ipv4Addr::LOCALHOST)
{
return Ok(*addr);
}
// Otherwise, take the first addr (OS preference)
addrs.into_iter().next().context("couldn't resolve address")
}

fn set_req_uri(req: &mut Request<Body>, scheme: Scheme) -> Result<()> {
const DEFAULT_HOST: &str = "localhost";

Expand Down Expand Up @@ -649,7 +688,7 @@ mod tests {

#[test]
fn parse_listen_addr_prefers_ipv4() {
let addr = parse_listen_addr("localhost:12345").unwrap();
let addr = SocketAddr::from_str("localhost:12345").unwrap();
assert_eq!(addr.ip(), Ipv4Addr::LOCALHOST);
assert_eq!(addr.port(), 12345);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/trigger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = { workspace = true }
[dependencies]
anyhow = "1.0"
async-trait = "0.1"
clap = { version = "3.1.15", features = ["derive", "env"] }
clap = { version = "4.1", features = ["derive", "env"] }
ctrlc = { version = "3.2", features = ["termination"] }
dirs = "4"
futures = "0.3"
Expand Down
Loading