ANW-10 address review: drop ANWESEN_LOG, validate --bind at parse
This commit is contained in:
parent
317667cce7
commit
36492c8058
2 changed files with 14 additions and 4 deletions
11
src/cli.rs
11
src/cli.rs
|
|
@ -12,6 +12,7 @@
|
||||||
//! no flags. Each flag has a matching `ANWESEN_<UPPER>` environment variable
|
//! no flags. Each flag has a matching `ANWESEN_<UPPER>` environment variable
|
||||||
//! and CLI wins over env per the manual.
|
//! and CLI wins over env per the manual.
|
||||||
|
|
||||||
|
use std::net::SocketAddr;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use clap::{Parser, Subcommand, ValueEnum};
|
use clap::{Parser, Subcommand, ValueEnum};
|
||||||
|
|
@ -41,7 +42,7 @@ pub struct ServeArgs {
|
||||||
|
|
||||||
/// Listen address for the HTTP server.
|
/// Listen address for the HTTP server.
|
||||||
#[arg(long, env = "ANWESEN_BIND", default_value = "127.0.0.1:8080")]
|
#[arg(long, env = "ANWESEN_BIND", default_value = "127.0.0.1:8080")]
|
||||||
pub bind: String,
|
pub bind: SocketAddr,
|
||||||
|
|
||||||
/// Log verbosity.
|
/// Log verbosity.
|
||||||
#[arg(long, env = "ANWESEN_LOG_LEVEL", default_value = "info")]
|
#[arg(long, env = "ANWESEN_LOG_LEVEL", default_value = "info")]
|
||||||
|
|
@ -109,13 +110,19 @@ mod tests {
|
||||||
match cli.command {
|
match cli.command {
|
||||||
Command::Serve(a) => {
|
Command::Serve(a) => {
|
||||||
assert_eq!(a.vault, PathBuf::from("/tmp/v"));
|
assert_eq!(a.vault, PathBuf::from("/tmp/v"));
|
||||||
assert_eq!(a.bind, "0.0.0.0:9000");
|
assert_eq!(a.bind, "0.0.0.0:9000".parse::<SocketAddr>().unwrap());
|
||||||
assert!(matches!(a.log_level, LogLevel::Debug));
|
assert!(matches!(a.log_level, LogLevel::Debug));
|
||||||
}
|
}
|
||||||
_ => panic!("expected serve"),
|
_ => panic!("expected serve"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn serve_rejects_malformed_bind_at_parse_time() {
|
||||||
|
let err = parse(&["serve", "--vault", "/tmp/v", "--bind", "not-an-addr"]).unwrap_err();
|
||||||
|
assert_eq!(err.kind(), clap::error::ErrorKind::ValueValidation);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn doctor_rejects_bind() {
|
fn doctor_rejects_bind() {
|
||||||
// --bind is serve-only; doctor must not accept it.
|
// --bind is serve-only; doctor must not accept it.
|
||||||
|
|
|
||||||
|
|
@ -40,8 +40,11 @@ fn main() -> Result<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_logging(level: cli::LogLevel) {
|
fn init_logging(level: cli::LogLevel) {
|
||||||
let filter = tracing_subscriber::EnvFilter::try_from_env("ANWESEN_LOG")
|
// The User Manual lists --log-level / ANWESEN_LOG_LEVEL as the only knobs
|
||||||
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(level.as_filter_directive()));
|
// for verbosity, and pins "CLI flags win over environment variables".
|
||||||
|
// Resolution happens in clap; this function only builds the filter from
|
||||||
|
// the already-resolved level.
|
||||||
|
let filter = tracing_subscriber::EnvFilter::new(level.as_filter_directive());
|
||||||
let _ = tracing_subscriber::fmt()
|
let _ = tracing_subscriber::fmt()
|
||||||
.with_env_filter(filter)
|
.with_env_filter(filter)
|
||||||
.with_writer(std::io::stderr)
|
.with_writer(std::io::stderr)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue