From 8554737cd55b5b24728edc192ec4d540d0dd5397 Mon Sep 17 00:00:00 2001 From: Jeremiah Russell Date: Mon, 6 Oct 2025 09:54:31 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(trash):=20add=20trash=20cli?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Implement a command-line interface for the trash functionality - Allow users to specify max results, pages, labels, and query parameters --- src/trash_cli.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/trash_cli.rs diff --git a/src/trash_cli.rs b/src/trash_cli.rs new file mode 100644 index 0000000..238e6cd --- /dev/null +++ b/src/trash_cli.rs @@ -0,0 +1,50 @@ +use clap::Parser; +use cull_gmail::{Error, Labels, Trash}; + +/// Command line options for the list subcommand +#[derive(Debug, Parser)] +pub struct TrashCli { + /// Maximum results per page + #[arg(short, long, default_value = cull_gmail::DEFAULT_MAX_RESULTS)] + max_results: u32, + /// Maximum number of pages (0=all) + #[arg(short, long, default_value = "1")] + pages: u32, + /// Labels to filter the message list + #[arg(short, long)] + labels: Vec, + /// Query string to select messages to list + #[arg(short = 'Q', long)] + query: Option, +} + +impl TrashCli { + pub(crate) async fn run(&self, credential_file: &str) -> Result<(), Error> { + let mut list = Trash::new(credential_file).await?; + + if !self.labels.is_empty() { + // add labels if any specified + let label_list = Labels::new(credential_file, false).await?; + + log::trace!("labels found and setup {label_list:#?}"); + log::debug!("labels from command line: {:?}", self.labels); + let mut label_ids = Vec::new(); + for label in &self.labels { + if let Some(id) = label_list.get_label_id(label) { + label_ids.push(id) + } + } + list.add_labels(label_ids.as_slice()); + } + + if let Some(query) = self.query.as_ref() { + list.set_query(query) + } + + log::trace!("Max results: `{}`", self.max_results); + list.set_max_results(self.max_results); + log::debug!("List max results set to {}", list.max_results()); + + list.run(self.pages).await + } +}