fix logging and switch to clap derive

This commit is contained in:
LordMZTE 2022-04-02 00:58:39 +02:00
parent 05de59253b
commit 8c7790e3e2
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
4 changed files with 50 additions and 42 deletions

View file

@ -14,7 +14,6 @@ itertools = "0.10.3"
miette = { version = "4.3.0", features = ["fancy"] } miette = { version = "4.3.0", features = ["fancy"] }
serde_json = "1.0.79" serde_json = "1.0.79"
smart-default = "0.6.0" smart-default = "0.6.0"
structopt = "0.3.26"
term_size = "0.3.2" term_size = "0.3.2"
termcolor = "1.1.3" termcolor = "1.1.3"
trust-dns-resolver = { version = "0.21.2", features = ["tokio-runtime"] } trust-dns-resolver = { version = "0.21.2", features = ["tokio-runtime"] }
@ -23,6 +22,7 @@ viuer = "0.6.0"
serde = { version = "1.0.136", features = ["derive"] } serde = { version = "1.0.136", features = ["derive"] }
tracing-subscriber = "0.3.10" tracing-subscriber = "0.3.10"
tracing = "0.1.32" tracing = "0.1.32"
clap = { version = "3.1.8", features = ["derive"] }
[dependencies.async-minecraft-ping] [dependencies.async-minecraft-ping]
git = "https://github.com/LordMZTE/async-minecraft-ping.git" git = "https://github.com/LordMZTE/async-minecraft-ping.git"

View file

@ -7,13 +7,13 @@ use crossterm::{
use image::{DynamicImage, ImageFormat}; use image::{DynamicImage, ImageFormat};
use itertools::Itertools; use itertools::Itertools;
use miette::{bail, miette, IntoDiagnostic, WrapErr}; use miette::{bail, miette, IntoDiagnostic, WrapErr};
use tracing::info; use serde::Deserialize;
use std::{ use std::{
io::{self, Cursor, Write}, io::{self, Cursor, Write},
net::IpAddr, net::IpAddr,
}; };
use tracing::info;
use trust_dns_resolver::TokioAsyncResolver; use trust_dns_resolver::TokioAsyncResolver;
use serde::Deserialize;
pub mod output; pub mod output;

View file

@ -1,67 +1,65 @@
use async_minecraft_ping::{ConnectionConfig, ServerDescription, StatusResponse}; use async_minecraft_ping::{ConnectionConfig, ServerDescription, StatusResponse};
use clap::Parser;
use itertools::Itertools; use itertools::Itertools;
use miette::{IntoDiagnostic, WrapErr}; use miette::{IntoDiagnostic, WrapErr};
use structopt::StructOpt;
use time::{Duration, Instant}; use time::{Duration, Instant};
use tokio::time; use tokio::time;
use mcstat::{ use mcstat::{
get_table, mc_formatted_to_ansi, none_if_empty, output::Table, parse_base64_image, get_table,
resolve_address, EitherStatusResponse, mc_formatted_to_ansi,
none_if_empty,
output::Table,
parse_base64_image,
resolve_address,
EitherStatusResponse,
}; };
use tracing::info; use tracing::{info, Level};
#[derive(Debug, StructOpt)] /// Queries information about a minecraft server
#[structopt( #[derive(Debug, Parser)]
name = "mcstat", #[clap(name = "mcstat")]
about = "queries information about a minecraft server"
)]
struct Opt { struct Opt {
#[structopt( /// The Address to ping. By default, a SRV lookup will be made to resolve
index = 1, /// this, unless the port is specified
help = "The Address to ping. By default, a SRV lookup will be made to resolve this, \
unless the port is specified."
)]
ip: String, ip: String,
#[structopt( /// the protocol version to use
long = "protocol", #[clap(long = "protocol", default_value = "751")]
help = "the protocol version to use",
default_value = "751"
)]
protocol_version: usize, protocol_version: usize,
#[structopt( /// the time before the server ping times out in milliseconds
long, #[clap(long, short, default_value = "5000")]
short,
help = "the time before the server ping times out in milliseconds",
default_value = "5000"
)]
timeout: u64, timeout: u64,
#[structopt(long, short, help = "print raw json response")] /// print raw json response
#[clap(long, short)]
raw: bool, raw: bool,
#[structopt(long, short, help = "print mod list")] /// print mod list
#[clap(long, short)]
mods: bool, mods: bool,
#[structopt( /// print mod versions
long, #[clap(long, short = 'V', requires = "mods")]
short = "v",
requires = "mods",
help = "also prints mod versions"
)]
modversions: bool, modversions: bool,
#[structopt(long, help = "displays forge mod channels if the server sends them")] /// displays forge mod channels if the server sends them
#[clap(long)]
channels: bool, channels: bool,
#[structopt(long, short, help = "print the server's favicon to stdout")] /// print the server's favicon to stdout
#[clap(long, short)]
image: bool, image: bool,
#[structopt(short, requires = "image", help = "size of the favicon ascii art")] /// size of the favicon ascii art
#[clap(short, requires = "image")]
size: Option<u32>, size: Option<u32>,
/// use verbose logging
#[clap(long, short, parse(from_occurrences))]
verbose: u32,
} }
impl Opt { impl Opt {
@ -79,9 +77,19 @@ impl Opt {
#[tokio::main] #[tokio::main]
async fn main() -> miette::Result<()> { async fn main() -> miette::Result<()> {
tracing_subscriber::fmt().pretty().init(); let opt = Opt::parse();
let opt = Opt::from_args(); let log_level = match opt.verbose {
0 => Level::ERROR,
1 => Level::INFO,
2 => Level::DEBUG,
_ => Level::TRACE,
};
tracing_subscriber::fmt()
.compact()
.with_max_level(log_level)
.init();
let (addr, port) = resolve_address(&opt.ip) let (addr, port) = resolve_address(&opt.ip)
.await .await

View file

@ -1,8 +1,8 @@
use smart_default::SmartDefault;
use std::{ use std::{
cmp::{max, min}, cmp::{max, min},
io::{self, Write}, io::{self, Write},
}; };
use smart_default::SmartDefault;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
#[derive(SmartDefault)] #[derive(SmartDefault)]