From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <d.csapak@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id BAA939C6D5
 for <pmg-devel@lists.proxmox.com>; Tue, 24 Oct 2023 14:55:24 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 9CB6E8324
 for <pmg-devel@lists.proxmox.com>; Tue, 24 Oct 2023 14:55:24 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pmg-devel@lists.proxmox.com>; Tue, 24 Oct 2023 14:55:23 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 4C74744B67
 for <pmg-devel@lists.proxmox.com>; Tue, 24 Oct 2023 14:55:23 +0200 (CEST)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pmg-devel@lists.proxmox.com
Date: Tue, 24 Oct 2023 14:55:21 +0200
Message-Id: <20231024125521.769887-1-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.30.2
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.012 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [options.to, options.host, main.rs]
Subject: [pmg-devel] [PATCH log-tracker] use pico-args instead of clap
X-BeenThere: pmg-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Mail Gateway development discussion
 <pmg-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pmg-devel>, 
 <mailto:pmg-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pmg-devel/>
List-Post: <mailto:pmg-devel@lists.proxmox.com>
List-Help: <mailto:pmg-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pmg-devel>, 
 <mailto:pmg-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Tue, 24 Oct 2023 12:55:24 -0000

Instead of upgrading from clap3 to clap4 (which seems to change their
interface every year or so), switch to the much smaller pico-args.

It has almost all features we need (except producing help output), but
wihout any dependencies. This increases compile time and reduces the
size of the resulting binary. It also reduces the lines of code.

The only difference is now the different output for errors, e.g. for
missing values of options.

Help output is copied from the old clap output.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 Cargo.toml  |   2 +-
 src/main.rs | 223 +++++++++++++++++++---------------------------------
 2 files changed, 83 insertions(+), 142 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index 725976f..82a9a4f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -11,7 +11,7 @@ exclude = [ "build", "debian" ]
 
 [dependencies]
 anyhow = "1"
-clap = { version = "3.2.23", features = ["cargo"] }
 flate2 = "1.0"
 libc = "0.2"
 proxmox-time = "1.1"
+pico-args = { version = "0.4", features = ["combined-flags"] }
diff --git a/src/main.rs b/src/main.rs
index e55f17b..45b0eca 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -13,111 +13,55 @@ use anyhow::{bail, Error};
 use flate2::read;
 use libc::time_t;
 
-use clap::{App, Arg};
-
 mod time;
 use time::{Tm, CAL_MTOD};
 
+fn print_usage() {
+    println!(
+        "\
+pmg-log-tracker {}
+Proxmox Mailgateway Log Tracker. Tool to scan mail logs.
+
+USAGE:
+    pmg-log-tracker [OPTIONS]
+
+OPTIONS:
+    -e, --endtime <TIME>            End time (YYYY-MM-DD HH:MM:SS) or seconds since epoch
+    -f, --from <SENDER>             Mails from SENDER
+    -g, --exclude-greylist          Exclude greylist entries
+    -h, --host <HOST>               Hostname or Server IP
+        --help                      Print help information
+    -i, --inputfile <INPUTFILE>     Input file to use instead of /var/log/syslog, or '-' for stdin
+    -l, --limit <MAX>               Print MAX entries [default: 0]
+    -m, --message-id <MSGID>        Message ID (exact match)
+    -n, --exclude-ndr               Exclude NDR entries
+    -q, --queue-id <QID>            Queue ID (exact match), can be specified multiple times
+    -s, --starttime <TIME>          Start time (YYYY-MM-DD HH:MM:SS) or seconds since epoch
+    -t, --to <RECIPIENT>            Mails to RECIPIENT
+    -v, --verbose                   Verbose output, can be specified multiple times
+    -V, --version                   Print version information
+    -x, --search-string <STRING>    Search for string",
+        env!("CARGO_PKG_VERSION")
+    );
+}
+
 fn main() -> Result<(), Error> {
-    let matches = App::new(clap::crate_name!())
-        .version(clap::crate_version!())
-        .about(clap::crate_description!())
-        .arg(
-            Arg::with_name("verbose")
-                .short('v')
-                .long("verbose")
-                .help("Verbose output, can be specified multiple times")
-                .action(clap::ArgAction::Count),
-        )
-        .arg(
-            Arg::with_name("inputfile")
-                .short('i')
-                .long("inputfile")
-                .help("Input file to use instead of /var/log/syslog, or '-' for stdin")
-                .value_name("INPUTFILE"),
-        )
-        .arg(
-            Arg::with_name("host")
-                .short('h')
-                .long("host")
-                .help("Hostname or Server IP")
-                .value_name("HOST"),
-        )
-        .arg(
-            Arg::with_name("from")
-                .short('f')
-                .long("from")
-                .help("Mails from SENDER")
-                .value_name("SENDER"),
-        )
-        .arg(
-            Arg::with_name("to")
-                .short('t')
-                .long("to")
-                .help("Mails to RECIPIENT")
-                .value_name("RECIPIENT"),
-        )
-        .arg(
-            Arg::with_name("start")
-                .short('s')
-                .long("starttime")
-                .help("Start time (YYYY-MM-DD HH:MM:SS) or seconds since epoch")
-                .value_name("TIME"),
-        )
-        .arg(
-            Arg::with_name("end")
-                .short('e')
-                .long("endtime")
-                .help("End time (YYYY-MM-DD HH:MM:SS) or seconds since epoch")
-                .value_name("TIME"),
-        )
-        .arg(
-            Arg::with_name("msgid")
-                .short('m')
-                .long("message-id")
-                .help("Message ID (exact match)")
-                .value_name("MSGID"),
-        )
-        .arg(
-            Arg::with_name("qids")
-                .short('q')
-                .long("queue-id")
-                .help("Queue ID (exact match), can be specified multiple times")
-                .value_name("QID")
-                .multiple(true)
-                .number_of_values(1),
-        )
-        .arg(
-            Arg::with_name("search")
-                .short('x')
-                .long("search-string")
-                .help("Search for string")
-                .value_name("STRING"),
-        )
-        .arg(
-            Arg::with_name("limit")
-                .short('l')
-                .long("limit")
-                .help("Print MAX entries")
-                .value_name("MAX")
-                .default_value("0"),
-        )
-        .arg(
-            Arg::with_name("exclude_greylist")
-                .short('g')
-                .long("exclude-greylist")
-                .help("Exclude greylist entries"),
-        )
-        .arg(
-            Arg::with_name("exclude_ndr")
-                .short('n')
-                .long("exclude-ndr")
-                .help("Exclude NDR entries"),
-        )
-        .get_matches();
+    let mut args = pico_args::Arguments::from_env();
+    if args.contains("--help") {
+        print_usage();
+        return Ok(());
+    }
 
     let mut parser = Parser::new()?;
-    parser.handle_args(matches)?;
+    parser.handle_args(&mut args)?;
+
+    let remaining_options = args.finish();
+    if !remaining_options.is_empty() {
+        bail!(
+            "Found invalid arguments: {:?}",
+            remaining_options.join(", ".as_ref())
+        )
+    }
 
     println!("# LogReader: {}", std::process::id());
     println!("# Query options");
@@ -1970,16 +1914,16 @@ impl Parser {
         count + 1
     }
 
-    fn handle_args(&mut self, args: clap::ArgMatches) -> Result<(), Error> {
-        if let Some(inputfile) = args.value_of("inputfile") {
-            self.options.inputfile = inputfile.to_string();
+    fn handle_args(&mut self, args: &mut pico_args::Arguments) -> Result<(), Error> {
+        if let Some(inputfile) = args.opt_value_from_str(["-i", "--inputfile"])? {
+            self.options.inputfile = inputfile;
         }
 
-        if let Some(start) = args.value_of("start") {
-            if let Ok(res) = time::strptime(start, c_str!("%F %T")) {
+        if let Some(start) = args.opt_value_from_str::<_, String>(["-s", "--starttime"])? {
+            if let Ok(res) = time::strptime(&start, c_str!("%F %T")) {
                 self.options.start = res.as_utc_to_epoch();
                 self.start_tm = res;
-            } else if let Ok(res) = time::strptime(start, c_str!("%s")) {
+            } else if let Ok(res) = time::strptime(&start, c_str!("%s")) {
                 self.options.start = res.as_utc_to_epoch();
                 self.start_tm = res;
             } else {
@@ -1994,11 +1938,11 @@ impl Parser {
             self.start_tm = ltime;
         }
 
-        if let Some(end) = args.value_of("end") {
-            if let Ok(res) = time::strptime(end, c_str!("%F %T")) {
+        if let Some(end) = args.opt_value_from_str::<_, String>(["-e", "--endtime"])? {
+            if let Ok(res) = time::strptime(&end, c_str!("%F %T")) {
                 self.options.end = res.as_utc_to_epoch();
                 self.end_tm = res;
-            } else if let Ok(res) = time::strptime(end, c_str!("%s")) {
+            } else if let Ok(res) = time::strptime(&end, c_str!("%s")) {
                 self.options.end = res.as_utc_to_epoch();
                 self.end_tm = res;
             } else {
@@ -2013,53 +1957,50 @@ impl Parser {
             bail!("end time before start time");
         }
 
-        self.options.limit = match args.value_of("limit") {
+        self.options.limit = match args.opt_value_from_str::<_, String>(["-l", "--limit"])? {
             Some(l) => l.parse().unwrap(),
             None => 0,
         };
 
-        if let Some(qids) = args.values_of("qids") {
-            for q in qids {
-                let ltime: time_t = 0;
-                let rel_line_nr: libc::c_ulong = 0;
-                let input = CString::new(q)?;
-                let bytes = concat!("T%08lXL%08lX", "\0");
-                let format =
-                    unsafe { std::ffi::CStr::from_bytes_with_nul_unchecked(bytes.as_bytes()) };
-                if unsafe {
-                    libc::sscanf(input.as_ptr(), format.as_ptr(), &ltime, &rel_line_nr) == 2
-                } {
-                    self.options
-                        .match_list
-                        .push(Match::RelLineNr(ltime, rel_line_nr));
-                } else {
-                    self.options
-                        .match_list
-                        .push(Match::Qid(q.as_bytes().into()));
-                }
+        while let Some(q) = args.opt_value_from_str::<_, String>(["-q", "--queue-id"])? {
+            let ltime: time_t = 0;
+            let rel_line_nr: libc::c_ulong = 0;
+            let input = CString::new(q.as_str())?;
+            let bytes = concat!("T%08lXL%08lX", "\0");
+            let format = unsafe { std::ffi::CStr::from_bytes_with_nul_unchecked(bytes.as_bytes()) };
+            if unsafe { libc::sscanf(input.as_ptr(), format.as_ptr(), &ltime, &rel_line_nr) == 2 } {
+                self.options
+                    .match_list
+                    .push(Match::RelLineNr(ltime, rel_line_nr));
+            } else {
+                self.options
+                    .match_list
+                    .push(Match::Qid(q.as_bytes().into()));
             }
         }
 
-        if let Some(from) = args.value_of("from") {
-            self.options.from = from.to_string();
+        if let Some(from) = args.opt_value_from_str(["-f", "--from"])? {
+            self.options.from = from;
         }
-        if let Some(to) = args.value_of("to") {
-            self.options.to = to.to_string();
+        if let Some(to) = args.opt_value_from_str(["-t", "--to"])? {
+            self.options.to = to;
         }
-        if let Some(host) = args.value_of("host") {
-            self.options.host = host.to_string();
+        if let Some(host) = args.opt_value_from_str(["-h", "--host"])? {
+            self.options.host = host;
         }
-        if let Some(msgid) = args.value_of("msgid") {
-            self.options.msgid = msgid.to_string();
+        if let Some(msgid) = args.opt_value_from_str(["-m", "--msgid"])? {
+            self.options.msgid = msgid;
         }
 
-        self.options.exclude_greylist = args.is_present("exclude_greylist");
-        self.options.exclude_ndr = args.is_present("exclude_ndr");
+        self.options.exclude_greylist = args.contains(["-g", "--exclude-greylist"]);
+        self.options.exclude_ndr = args.contains(["-n", "--exclude-ndr"]);
 
-        self.options.verbose = args.get_count("verbose") as _;
+        while args.contains(["-v", "--verbose"]) {
+            self.options.verbose += 1;
+        }
 
-        if let Some(string_match) = args.value_of("search") {
-            self.options.string_match = string_match.to_string();
+        if let Some(string_match) = args.opt_value_from_str(["-x", "--search-string"])? {
+            self.options.string_match = string_match;
         }
 
         Ok(())
-- 
2.30.2