v1.0.0-rc.7 Update clap dependency, rewrite deprecated code, now has better color output

This commit is contained in:
Tim-Paik 2021-10-24 21:46:08 +08:00
parent 027d336643
commit 02b6c26874
3 changed files with 153 additions and 117 deletions

97
Cargo.lock generated
View File

@ -535,19 +535,31 @@ dependencies = [
[[package]]
name = "chrono-tz"
version = "0.5.3"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2554a3155fec064362507487171dcc4edc3df60cb10f3a1fb10ed8094822b120"
checksum = "64c01c1c607d25c71bbaa67c113d6c6b36c434744b4fd66691d711b5b1bc0c8b"
dependencies = [
"chrono",
"chrono-tz-build",
"phf",
]
[[package]]
name = "chrono-tz-build"
version = "0.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db058d493fb2f65f41861bfed7e3fe6335264a9f0f92710cab5bdf01fef09069"
dependencies = [
"parse-zoneinfo",
"phf",
"phf_codegen",
]
[[package]]
name = "clap"
version = "3.0.0-beta.4"
version = "3.0.0-beta.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fcd70aa5597dbc42f7217a543f9ef2768b2ef823ba29036072d30e1d88e98406"
checksum = "feff3878564edb93745d58cf63e17b63f24142506e7a20c87a5521ed7bfb1d63"
dependencies = [
"atty",
"bitflags",
@ -559,14 +571,14 @@ dependencies = [
"termcolor",
"terminal_size",
"textwrap",
"vec_map",
"unicase",
]
[[package]]
name = "clap_derive"
version = "3.0.0-beta.4"
version = "3.0.0-beta.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b5bb0d655624a0b8770d1c178fb8ffcb1f91cc722cb08f451e3dc72465421ac"
checksum = "8b15c6b4f786ffb6192ffe65a36855bc1fc2444bcd0945ae16748dcd6ed7d0d3"
dependencies = [
"heck",
"proc-macro-error",
@ -1312,9 +1324,12 @@ checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
[[package]]
name = "os_str_bytes"
version = "3.1.0"
version = "4.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6acbef58a60fe69ab50510a55bc8cdd4d6cf2283d27ad338f54cb52747a9cf2d"
checksum = "addaa943333a514159c80c97ff4a93306530d965d27e139188283cd13e06a799"
dependencies = [
"memchr",
]
[[package]]
name = "parking_lot"
@ -1399,6 +1414,45 @@ dependencies = [
"sha-1 0.8.2",
]
[[package]]
name = "phf"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9fc3db1018c4b59d7d582a739436478b6035138b6aecbce989fc91c3e98409f"
dependencies = [
"phf_shared",
]
[[package]]
name = "phf_codegen"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fb1c3a8bc4dd4e5cfce29b44ffc14bedd2ee294559a294e2a4d4c9e9a6a13cd"
dependencies = [
"phf_generator",
"phf_shared",
]
[[package]]
name = "phf_generator"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d5285893bb5eb82e6aaf5d59ee909a06a16737a8970984dd7746ba9283498d6"
dependencies = [
"phf_shared",
"rand 0.8.4",
]
[[package]]
name = "phf_shared"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6796ad771acdc0123d2a88dc428b5e38ef24456743ddb1744ed628f9815c096"
dependencies = [
"siphasher",
"uncased",
]
[[package]]
name = "pin-project"
version = "0.4.28"
@ -1846,6 +1900,12 @@ dependencies = [
"libc",
]
[[package]]
name = "siphasher"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "533494a8f9b724d33625ab53c6c4800f7cc445895924a8ef649222dcb76e938b"
[[package]]
name = "slab"
version = "0.4.4"
@ -1984,9 +2044,9 @@ dependencies = [
[[package]]
name = "tera"
version = "1.12.1"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf95b0d8a46da5fe3ea119394a6c7f1e745f9de359081641c99946e2bf55d4f2"
checksum = "ed0c0eee8fbbbaab449287574b292f21ca53224b92a07b4a23266b77376f0ce7"
dependencies = [
"chrono",
"chrono-tz",
@ -2272,6 +2332,15 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c"
[[package]]
name = "uncased"
version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5baeed7327e25054889b9bd4f975f32e5f4c5d434042d59ab6cd4142c0a76ed0"
dependencies = [
"version_check 0.9.3",
]
[[package]]
name = "unic-char-property"
version = "0.9.0"
@ -2420,12 +2489,6 @@ dependencies = [
"v_escape",
]
[[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.1.5"

View File

@ -11,16 +11,16 @@ actix-http = "2.2"
actix-web = {version = "3.3", features = ["rustls"]}
actix-web-httpauth = "0.5"
chrono = "0.4"
clap = {version = "3.0.0-beta.4", features = ["wrap_help", "color"]}
clap = {version = "3.0.0-beta.5", features = ["wrap_help", "color"]}
env_logger = "0.9"
lazy_static = "1.4"
log = "0.4"
mime_guess = "2"
mime_guess = "2.0"
regex = "1.5"
rustls = "0.18"
serde = "1"
serde = "1.0"
sha2 = "0.9"
tera = "1"
tera = "1.13"
toml = "0.5"
urlencoding = "2.1"

View File

@ -12,6 +12,7 @@ use actix_web::{
dev::{self, Service, ServiceResponse},
http, middleware, App, HttpResponse, HttpServer,
};
use clap::Arg;
use env_logger::fmt::Color;
use log::{error, info};
use sha2::Digest;
@ -342,102 +343,74 @@ async fn validator(
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let matches = clap_app!((crate_name!()) =>
(version: crate_version!())
(author: crate_authors!())
(about: crate_description!())
(@arg noindex: --noindex "Disable automatic index page generation")
(@arg compress: -c --compress "Enable streaming compression (Content-length/segment download will be disabled)")
// (@arg upload: -u --upload "Enable file upload")
(@arg nocache: --nocache "Disable HTTP cache")
(@arg nocolor: --nocolor "Disable cli colors")
(@arg cors: --cors [VALUE] min_values(0) max_values(1) "Enable CORS")
(@arg spa: --spa "Enable Single-Page Application mode (always serve /index.html when the file is not found)")
(@arg dotfiles: -d --dotfiles "Show dotfiles")
(@arg open: -o --open "Open the page in the default browser")
(@arg quiet: -q --quiet "Disable access log output")
(@arg quietall: --quietall "Disable all output")
(@arg ROOT: default_value["."] {
|path| match std::fs::metadata(path) {
Ok(meta) => {
if meta.is_dir() {
Ok(())
} else {
Err("Parameter is not a directory".to_owned())
}
}
Err(e) => Err(e.to_string()),
let check_does_dir_exits = |path: &str| match std::fs::metadata(path) {
Ok(meta) => {
if meta.is_dir() {
Ok(())
} else {
Err("Parameter is not a directory".to_owned())
}
} "Root directory")
(@arg address: -a --address +takes_value default_value["0.0.0.0"] {
|s| match IpAddr::from_str(s) {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
}
Err(e) => Err(e.to_string()),
};
let check_does_file_exits = |path: &str| match std::fs::metadata(path) {
Ok(metadata) => {
if metadata.is_file() {
Ok(())
} else {
Err("Parameter is not a file".to_owned())
}
} "IP address to serve on")
(@arg port: -p --port +takes_value default_value["8000"] {
|s| match s.parse::<u16>() {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
}
} "Port to serve on")
(@arg auth: --auth +takes_value {
|s| {
let parts = s.splitn(2, ':').collect::<Vec<&str>>();
if parts.len() < 2 || parts.len() >= 2 && parts[1].is_empty() {
Err("Password not found".to_owned())
} else if parts[0].is_empty() {
Err("Username not found".to_owned())
} else {
Ok(())
}
}
} "HTTP Auth (username:password)")
(@arg cert: --cert +takes_value {
|s| match std::fs::metadata(s) {
Ok(metadata) => {
if metadata.is_file() {
Ok(())
} else {
Err("Parameter is not a file".to_owned())
}
}
Err(e) => Err(e.to_string()),
}
} "Path of TLS/SSL public key (certificate)")
(@arg key: --key +takes_value {
|s| match std::fs::metadata(s) {
Ok(metadata) => {
if metadata.is_file() {
Ok(())
} else {
Err("Parameter is not a file".to_owned())
}
}
Err(e) => Err(e.to_string()),
}
} "Path of TLS/SSL private key")
(@subcommand doc =>
(about: "Open cargo doc via local server (Need cargo installation)")
(@arg nocolor: --nocolor "Disable cli colors")
(@arg noopen: -no --noopen "Do not open the page in the default browser")
(@arg log: --log "Enable access log output [default: false]")
(@arg quietall: --quietall "Disable all output")
(@arg address: -a --address +takes_value default_value["0.0.0.0"] {
|s| match IpAddr::from_str(s) {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
}
} "IP address to serve on")
(@arg port: -p --port +takes_value default_value["8000"] {
|s| match s.parse::<u16>() {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
}
} "Port to serve on")
}
Err(e) => Err(e.to_string()),
};
let check_is_ip_addr = |s: &str| match IpAddr::from_str(s) {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
};
let check_is_port_num = |s: &str| match s.parse::<u16>() {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
};
let check_is_auth = |s: &str| {
let parts = s.splitn(2, ':').collect::<Vec<&str>>();
if parts.len() < 2 || parts.len() >= 2 && parts[1].is_empty() {
Err("Password not found".to_owned())
} else if parts[0].is_empty() {
Err("Username not found".to_owned())
} else {
Ok(())
}
};
let matches = clap::App::new(crate_name!())
.version(crate_version!())
.author(crate_authors!())
.about(crate_description!())
.arg(Arg::new("noindex").long("noindex").about("Disable automatic index page generation"))
.arg(Arg::new("compress").short('c').long("compress").about("Enable streaming compression (Content-length/segment download will be disabled)"))
.arg(Arg::new("nocache").long("nocache").about("Disable HTTP cache"))
.arg(Arg::new("nocolor").long("nocolor").about("Disable cli colors"))
.arg(Arg::new("cors").long("cors").takes_value(true).min_values(0).max_values(1).about("Enable CORS [with custom value]"))
.arg(Arg::new("spa").long("spa").about("Enable Single-Page Application mode (always serve /index.html when the file is not found)"))
.arg(Arg::new("dotfiles").short('d').long("dotfiles").about("Show dotfiles"))
.arg(Arg::new("open").short('o').long("open").about("Open the page in the default browser"))
.arg(Arg::new("quiet").short('q').long("quiet").about("Disable access log output"))
.arg(Arg::new("quietall").long("quietall").about("Disable all output"))
.arg(Arg::new("ROOT").default_value(".").validator(check_does_dir_exits).about("Root directory"))
.arg(Arg::new("address").short('a').long("address").default_value("0.0.0.0").takes_value(true).validator(check_is_ip_addr).about("IP address to serve on"))
.arg(Arg::new("port").short('p').long("port").default_value("8000").takes_value(true).validator(check_is_port_num).about("Port to serve on"))
.arg(Arg::new("auth").long("auth").takes_value(true).validator(check_is_auth).about("HTTP Auth (username:password)"))
.arg(Arg::new("cert").long("cert").takes_value(true).validator(check_does_file_exits).about("Path of TLS/SSL public key (certificate)"))
.arg(Arg::new("key").long("key").takes_value(true).validator(check_does_file_exits).about("Path of TLS/SSL private key"))
.subcommand(clap::App::new("doc")
.about("Open cargo doc via local server (Need cargo installation)")
.arg(Arg::new("nocolor").long("nocolor").about("Disable cli colors"))
.arg(Arg::new("noopen").long("noopen").about("Do not open the page in the default browser"))
.arg(Arg::new("log").long("log").about("Enable access log output [default: disabled]"))
.arg(Arg::new("quietall").long("quietall").about("Disable all output"))
.arg(Arg::new("address").short('a').long("address").default_value("0.0.0.0").takes_value(true).validator(check_is_ip_addr).about("IP address to serve on"))
.arg(Arg::new("port").short('p').long("port").default_value("8000").takes_value(true).validator(check_is_port_num).about("Port to serve on"))
)
)
.get_matches();
.get_matches();
set_var(
"ROOT",
@ -608,11 +581,11 @@ async fn main() -> std::io::Result<()> {
.format(|buf, record| {
let data = record.args().to_string();
let mut style = buf.style();
let blue = style.set_color(Color::Rgb(52, 152, 219));
let blue = style.set_color(Color::Cyan);
let mut style = buf.style();
let red = style.set_color(Color::Rgb(231, 76, 60));
let red = style.set_color(Color::Red);
let mut style = buf.style();
let green = style.set_color(Color::Rgb(76, 175, 80));
let green = style.set_color(Color::Green);
if record.target() == "actix_web::middleware::logger" {
let data: Vec<&str> = data.splitn(5, '^').collect();
let time = blue.value(