From 400f0752272199cab25a84443e725a619f5fa027 Mon Sep 17 00:00:00 2001 From: Nugine Date: Tue, 16 Jan 2024 18:32:46 +0800 Subject: [PATCH] s3s-fs: cli and logging --- crates/s3s-fs/src/main.rs | 30 +++++++++++++++++++++--------- crates/s3s-proxy/src/main.rs | 4 ++-- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/crates/s3s-fs/src/main.rs b/crates/s3s-fs/src/main.rs index 3a9dc2de..663c86df 100644 --- a/crates/s3s-fs/src/main.rs +++ b/crates/s3s-fs/src/main.rs @@ -7,10 +7,11 @@ use s3s_fs::Result; use s3s::auth::SimpleAuth; use s3s::service::S3ServiceBuilder; +use std::io::IsTerminal; use std::net::TcpListener; use std::path::PathBuf; -use clap::Parser; +use clap::{CommandFactory, Parser}; use hyper::server::Server; use tracing::info; @@ -26,11 +27,11 @@ struct Opt { port: u16, /// Access key used for authentication. - #[arg(long, requires("secret-key"))] + #[arg(long)] access_key: Option, /// Secret key used for authentication. - #[arg(long, requires("access-key"))] + #[arg(long)] secret_key: Option, /// Domain name used for virtual-hosted-style requests. @@ -45,8 +46,7 @@ fn setup_tracing() { use tracing_subscriber::EnvFilter; let env_filter = EnvFilter::from_default_env(); - // let enable_color = std::io::stdout().is_terminal(); // TODO - let enable_color = false; + let enable_color = std::io::stdout().is_terminal(); tracing_subscriber::fmt() .pretty() @@ -55,18 +55,28 @@ fn setup_tracing() { .init(); } -fn check_cli_args(opt: &Opt) -> Result<(), String> { +fn check_cli_args(opt: &Opt) { + use clap::error::ErrorKind; + + let mut cmd = Opt::command(); + + // TODO: how to specify the requirements with clap derive API? + if let (Some(_), None) | (None, Some(_)) = (&opt.access_key, &opt.secret_key) { + let msg = "access key and secret key must be specified together"; + cmd.error(ErrorKind::MissingRequiredArgument, msg).exit(); + } + if let Some(ref s) = opt.domain_name { if s.contains('/') { - return Err(format!("expected domain name, found URL-like string: {s:?}")); + let msg = format!("expected domain name, found URL-like string: {s:?}"); + cmd.error(ErrorKind::InvalidValue, msg).exit(); } } - Ok(()) } fn main() -> Result { let opt = Opt::parse(); - check_cli_args(&opt).map_err(s3s_fs::Error::from_string)?; + check_cli_args(&opt); setup_tracing(); @@ -85,11 +95,13 @@ async fn run(opt: Opt) -> Result { // Enable authentication if let (Some(ak), Some(sk)) = (opt.access_key, opt.secret_key) { b.set_auth(SimpleAuth::from_single(ak, sk)); + info!("authentication is enabled"); } // Enable parsing virtual-hosted-style requests if let Some(domain_name) = opt.domain_name { b.set_base_domain(domain_name); + info!("virtual-hosted-style requests are enabled"); } b.build() diff --git a/crates/s3s-proxy/src/main.rs b/crates/s3s-proxy/src/main.rs index 1ad6dca0..233dd044 100644 --- a/crates/s3s-proxy/src/main.rs +++ b/crates/s3s-proxy/src/main.rs @@ -5,6 +5,7 @@ use s3s::auth::SimpleAuth; use s3s::service::S3ServiceBuilder; use std::error::Error; +use std::io::IsTerminal; use std::net::TcpListener; use aws_credential_types::provider::ProvideCredentials; @@ -32,8 +33,7 @@ fn setup_tracing() { use tracing_subscriber::EnvFilter; let env_filter = EnvFilter::from_default_env(); - // let enable_color = std::io::stdout().is_terminal(); // TODO - let enable_color = false; + let enable_color = std::io::stdout().is_terminal(); tracing_subscriber::fmt() .pretty()