♻️ refactor(cli): restructure cli commands for better organization
- rename `label_cli.rs` to `labels_cli.rs` - rename `message_cli.rs` to `messages_cli.rs` - move config related commands to `rules config` subcommand - introduce `rules run` subcommand
This commit is contained in:
committed by
Jeremiah Russell
parent
7c2bcd37b4
commit
3beab7d82d
@@ -2,9 +2,9 @@ use clap::Parser;
|
|||||||
use cull_gmail::{Error, GmailClient};
|
use cull_gmail::{Error, GmailClient};
|
||||||
|
|
||||||
#[derive(Debug, Parser)]
|
#[derive(Debug, Parser)]
|
||||||
pub struct LabelCli {}
|
pub struct LabelsCli {}
|
||||||
|
|
||||||
impl LabelCli {
|
impl LabelsCli {
|
||||||
pub async fn run(&self, client: GmailClient) -> Result<(), Error> {
|
pub async fn run(&self, client: GmailClient) -> Result<(), Error> {
|
||||||
client.show_label();
|
client.show_label();
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -1,16 +1,14 @@
|
|||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
|
|
||||||
mod config_cli;
|
mod labels_cli;
|
||||||
mod label_cli;
|
mod messages_cli;
|
||||||
mod message_cli;
|
|
||||||
mod rules_cli;
|
mod rules_cli;
|
||||||
|
|
||||||
use cull_gmail::{Config, GmailClient, Result};
|
use cull_gmail::{Config, GmailClient, Result};
|
||||||
use std::error::Error as stdError;
|
use std::error::Error as stdError;
|
||||||
|
|
||||||
use config_cli::ConfigCli;
|
use labels_cli::LabelsCli;
|
||||||
use label_cli::LabelCli;
|
use messages_cli::MessagesCli;
|
||||||
use message_cli::MessageCli;
|
|
||||||
use rules_cli::RulesCli;
|
use rules_cli::RulesCli;
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
@@ -24,21 +22,14 @@ struct Cli {
|
|||||||
|
|
||||||
#[derive(Subcommand, Debug)]
|
#[derive(Subcommand, Debug)]
|
||||||
enum SubCmds {
|
enum SubCmds {
|
||||||
/// Configure rules and labels
|
|
||||||
#[clap(
|
|
||||||
name = "config",
|
|
||||||
display_order = 1,
|
|
||||||
next_help_heading = "Configuration"
|
|
||||||
)]
|
|
||||||
Config(ConfigCli),
|
|
||||||
/// List messages
|
/// List messages
|
||||||
#[clap(name = "message", display_order = 3, next_help_heading = "Messages")]
|
#[clap(name = "messages", display_order = 3, next_help_heading = "Labels")]
|
||||||
Message(MessageCli),
|
Message(MessagesCli),
|
||||||
/// List labels
|
/// List labels
|
||||||
#[clap(name = "label", display_order = 2, next_help_heading = "Labels")]
|
#[clap(name = "labels", display_order = 2, next_help_heading = "Rules")]
|
||||||
Labels(LabelCli),
|
Labels(LabelsCli),
|
||||||
/// Run the rules from the rules configuration
|
/// Configure and run rules
|
||||||
#[clap(name = "run", display_order = 6, next_help_heading = "Rule Processing")]
|
#[clap(name = "rules", display_order = 2)]
|
||||||
Rules(RulesCli),
|
Rules(RulesCli),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,10 +63,9 @@ async fn run(args: Cli) -> Result<()> {
|
|||||||
let mut client = GmailClient::new(config.credential_file()).await?;
|
let mut client = GmailClient::new(config.credential_file()).await?;
|
||||||
|
|
||||||
match args.sub_command {
|
match args.sub_command {
|
||||||
SubCmds::Config(config_cli) => config_cli.run(config),
|
SubCmds::Message(messages_cli) => messages_cli.run(&mut client).await,
|
||||||
SubCmds::Message(list_cli) => list_cli.run(&mut client).await,
|
SubCmds::Labels(labels_cli) => labels_cli.run(client).await,
|
||||||
SubCmds::Labels(label_cli) => label_cli.run(client).await,
|
SubCmds::Rules(rules_cli) => rules_cli.run(&mut client, config).await,
|
||||||
SubCmds::Rules(run_cli) => run_cli.run(&mut client, config).await,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ enum MessageAction {
|
|||||||
|
|
||||||
/// Command line options for the list subcommand
|
/// Command line options for the list subcommand
|
||||||
#[derive(Debug, Parser)]
|
#[derive(Debug, Parser)]
|
||||||
pub struct MessageCli {
|
pub struct MessagesCli {
|
||||||
/// Maximum results per page
|
/// Maximum results per page
|
||||||
#[arg(short, long,display_order = 1, help_heading = "Config", default_value = cull_gmail::DEFAULT_MAX_RESULTS)]
|
#[arg(short, long,display_order = 1, help_heading = "Config", default_value = cull_gmail::DEFAULT_MAX_RESULTS)]
|
||||||
max_results: u32,
|
max_results: u32,
|
||||||
@@ -34,7 +34,7 @@ pub struct MessageCli {
|
|||||||
action: MessageAction,
|
action: MessageAction,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MessageCli {
|
impl MessagesCli {
|
||||||
pub(crate) async fn run(&self, client: &mut GmailClient) -> Result<()> {
|
pub(crate) async fn run(&self, client: &mut GmailClient) -> Result<()> {
|
||||||
self.set_parameters(client)?;
|
self.set_parameters(client)?;
|
||||||
|
|
||||||
@@ -1,60 +1,34 @@
|
|||||||
use clap::Parser;
|
use clap::{Parser, Subcommand};
|
||||||
use cull_gmail::{Config, EolAction, GmailClient, Result, RuleProcessor};
|
|
||||||
|
mod config_cli;
|
||||||
|
mod run_cli;
|
||||||
|
|
||||||
|
use cull_gmail::{Config, GmailClient, Result};
|
||||||
|
|
||||||
|
use config_cli::ConfigCli;
|
||||||
|
use run_cli::RunCli;
|
||||||
|
|
||||||
|
#[derive(Subcommand, Debug)]
|
||||||
|
enum SubCmds {
|
||||||
|
/// Configure end-of-life rules
|
||||||
|
#[clap(name = "config")]
|
||||||
|
Config(ConfigCli),
|
||||||
|
/// Run end-of-life rules
|
||||||
|
#[clap(name = "run")]
|
||||||
|
Run(RunCli),
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Parser)]
|
#[derive(Debug, Parser)]
|
||||||
pub struct RulesCli {
|
pub struct RulesCli {
|
||||||
/// Execute the action
|
#[command(subcommand)]
|
||||||
#[clap(short, long, display_order = 1, help_heading = "Action")]
|
sub_command: SubCmds,
|
||||||
execute: bool,
|
|
||||||
/// Skip any rules that apply the action `trash`
|
|
||||||
#[clap(short = 't', long, display_order = 2, help_heading = "Skip Action")]
|
|
||||||
skip_trash: bool,
|
|
||||||
/// Skip any rules that apply the action `delete`
|
|
||||||
#[clap(short = 'd', long, display_order = 3, help_heading = "Skip Action")]
|
|
||||||
skip_delete: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RulesCli {
|
impl RulesCli {
|
||||||
pub async fn run(&self, client: &mut GmailClient, config: Config) -> Result<()> {
|
pub async fn run(&self, client: &mut GmailClient, config: Config) -> Result<()> {
|
||||||
let rules = config.get_rules_by_label();
|
match &self.sub_command {
|
||||||
|
SubCmds::Config(config_cli) => config_cli.run(config),
|
||||||
for label in config.labels() {
|
SubCmds::Run(run_cli) => run_cli.run(client, config).await,
|
||||||
let Some(rule) = rules.get(&label) else {
|
|
||||||
log::warn!("no rule found for label `{label}`");
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
log::info!("Executing rule `#{}` for label `{label}`", rule.describe());
|
|
||||||
client.set_rule(rule.clone());
|
|
||||||
client.set_execute(self.execute);
|
|
||||||
client.find_rule_and_messages_for_label(&label).await?;
|
|
||||||
let Some(action) = client.action() else {
|
|
||||||
log::warn!("no valid action specified for rule #{}", rule.id());
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
if self.execute {
|
|
||||||
match action {
|
|
||||||
EolAction::Trash => {
|
|
||||||
log::info!("***executing trash messages***");
|
|
||||||
if client.batch_trash().await.is_err() {
|
|
||||||
log::warn!("Move to trash failed for label `{label}`");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
EolAction::Delete => {
|
|
||||||
log::info!("***executing final delete messages***");
|
|
||||||
if client.batch_delete().await.is_err() {
|
|
||||||
log::warn!("Delete failed for label `{label}`");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log::warn!("Execution stopped for dry run");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
60
src/cli/rules_cli/run_cli.rs
Normal file
60
src/cli/rules_cli/run_cli.rs
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
use clap::Parser;
|
||||||
|
use cull_gmail::{Config, EolAction, GmailClient, Result, RuleProcessor};
|
||||||
|
|
||||||
|
#[derive(Debug, Parser)]
|
||||||
|
pub struct RunCli {
|
||||||
|
/// Execute the action
|
||||||
|
#[clap(short, long, display_order = 1, help_heading = "Action")]
|
||||||
|
execute: bool,
|
||||||
|
/// Skip any rules that apply the action `trash`
|
||||||
|
#[clap(short = 't', long, display_order = 2, help_heading = "Skip Action")]
|
||||||
|
skip_trash: bool,
|
||||||
|
/// Skip any rules that apply the action `delete`
|
||||||
|
#[clap(short = 'd', long, display_order = 3, help_heading = "Skip Action")]
|
||||||
|
skip_delete: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RunCli {
|
||||||
|
pub async fn run(&self, client: &mut GmailClient, config: Config) -> Result<()> {
|
||||||
|
let rules = config.get_rules_by_label();
|
||||||
|
|
||||||
|
for label in config.labels() {
|
||||||
|
let Some(rule) = rules.get(&label) else {
|
||||||
|
log::warn!("no rule found for label `{label}`");
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
log::info!("Executing rule `#{}` for label `{label}`", rule.describe());
|
||||||
|
client.set_rule(rule.clone());
|
||||||
|
client.set_execute(self.execute);
|
||||||
|
client.find_rule_and_messages_for_label(&label).await?;
|
||||||
|
let Some(action) = client.action() else {
|
||||||
|
log::warn!("no valid action specified for rule #{}", rule.id());
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
if self.execute {
|
||||||
|
match action {
|
||||||
|
EolAction::Trash => {
|
||||||
|
log::info!("***executing trash messages***");
|
||||||
|
if client.batch_trash().await.is_err() {
|
||||||
|
log::warn!("Move to trash failed for label `{label}`");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EolAction::Delete => {
|
||||||
|
log::info!("***executing final delete messages***");
|
||||||
|
if client.batch_delete().await.is_err() {
|
||||||
|
log::warn!("Delete failed for label `{label}`");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log::warn!("Execution stopped for dry run");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user