use anyhow::Result;
use clap::Parser;
#[derive(Parser)]
#[command(name = "pidge")]
#[command(author, version, about)]
#[command(long_about = "A fast CLI for e-mail and calendar.\n\n\
Manage one or more e-mail accounts and browse, search, send, and reply \
to e-mail from your terminal.")]
#[command(propagate_version = true)]
pub struct Cli {
#[arg(short, long, action = clap::ArgAction::Count, global = true)]
pub verbose: u8,
#[arg(short, long, global = true)]
pub quiet: bool,
#[arg(long, global = true)]
pub no_color: bool,
#[arg(long, global = true)]
pub json: bool,
#[command(subcommand)]
pub command: Option<Commands>,
}
#[derive(clap::Subcommand)]
pub enum Commands {
Ai {
#[command(subcommand)]
command: Option<AiCommands>,
},
Account {
#[command(subcommand)]
command: AccountCommands,
},
Mail {
#[command(subcommand)]
command: MailCommands,
},
Trust {
#[command(subcommand)]
command: TrustCommands,
},
Drafts {
#[command(subcommand)]
command: DraftsCommands,
},
Completion {
#[arg(value_enum)]
shell: Shell,
},
Version,
}
#[derive(clap::Subcommand)]
pub enum AiCommands {
Test {
message: Option<String>,
},
Enable,
Disable,
Config,
Status,
Skill {
#[arg(long)]
emit: bool,
#[arg(long)]
from_source: bool,
},
}
#[derive(clap::Subcommand)]
pub enum AccountCommands {
Add {
#[arg(long, value_enum, default_value_t = StorageBackendArg::Keychain)]
store: StorageBackendArg,
},
List,
Remove {
email: Option<String>,
#[arg(long, conflicts_with = "email")]
all: bool,
#[arg(short = 'y', long)]
yes: bool,
},
Default {
#[command(subcommand)]
command: Option<DefaultCommands>,
},
MigrateStorage {
email: String,
#[arg(long = "to", value_enum)]
to: StorageBackendArg,
},
}
#[derive(clap::Subcommand)]
pub enum DefaultCommands {
#[command(name = "e-mail")]
EMail {
email: String,
},
Calendar {
email: String,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum StorageBackendArg {
Keychain,
File,
}
impl From<StorageBackendArg> for pidge_core::TokenStorage {
fn from(v: StorageBackendArg) -> Self {
match v {
StorageBackendArg::Keychain => Self::Keychain,
StorageBackendArg::File => Self::File,
}
}
}
#[derive(clap::Subcommand)]
pub enum MailCommands {
List {
#[arg(long)]
account: Vec<String>,
#[arg(short = 'n', long, default_value = "25")]
limit: usize,
#[arg(short = 'p', long, default_value = "1")]
page: usize,
#[arg(long)]
unread: bool,
#[arg(short = 'c', long, conflicts_with_all = ["table", "full"])]
compact: bool,
#[arg(short = 't', long, conflicts_with = "full")]
table: bool,
#[arg(short = 'f', long)]
full: bool,
},
Show {
fragment: String,
#[arg(short = 'r', long)]
mark_read: bool,
#[arg(long)]
show_images: bool,
#[arg(long, hide = true)]
raw_html: bool,
},
Search {
query: String,
#[arg(long)]
account: Vec<String>,
#[arg(short = 'n', long, default_value = "25")]
limit: usize,
#[arg(short = 'c', long, conflicts_with_all = ["table", "full"])]
compact: bool,
#[arg(short = 't', long, conflicts_with = "full")]
table: bool,
#[arg(short = 'f', long)]
full: bool,
},
#[command(name = "mark-read")]
MarkRead {
fragment: String,
},
#[command(name = "mark-unread")]
MarkUnread {
fragment: String,
},
Flag {
fragment: String,
},
Unflag {
fragment: String,
},
Archive {
fragment: String,
},
Delete {
fragment: Option<String>,
#[arg(long, conflicts_with = "fragment")]
older_than: Option<String>,
#[arg(long)]
account: Vec<String>,
#[arg(short = 'y', long)]
yes: bool,
},
New(ComposeArgs),
Reply {
fragment: String,
#[command(flatten)]
compose: ReplyArgs,
},
#[command(name = "reply-all")]
ReplyAll {
fragment: String,
#[command(flatten)]
compose: ReplyArgs,
},
Forward {
fragment: String,
#[command(flatten)]
compose: ForwardArgs,
},
}
#[derive(clap::Args, Debug, Clone, Default)]
pub struct ComposeArgs {
#[arg(long)]
pub from: Option<String>,
#[arg(long, value_delimiter = ',')]
pub to: Vec<String>,
#[arg(long, value_delimiter = ',')]
pub cc: Vec<String>,
#[arg(long, value_delimiter = ',')]
pub bcc: Vec<String>,
#[arg(long)]
pub subject: Option<String>,
#[arg(long, conflicts_with = "body_file")]
pub body: Option<String>,
#[arg(long)]
pub body_file: Option<String>,
#[arg(long)]
pub confirm: bool,
#[arg(long)]
pub draft: bool,
#[arg(long)]
pub attach: Vec<std::path::PathBuf>,
}
#[derive(clap::Args, Debug, Clone, Default)]
pub struct ReplyArgs {
#[arg(long)]
pub from: Option<String>,
#[arg(long, conflicts_with = "body_file")]
pub body: Option<String>,
#[arg(long)]
pub body_file: Option<String>,
#[arg(short = 'y', long)]
pub yes: bool,
#[arg(long)]
pub draft: bool,
#[arg(long)]
pub attach: Vec<std::path::PathBuf>,
}
#[derive(clap::Args, Debug, Clone, Default)]
pub struct ForwardArgs {
#[arg(long)]
pub from: Option<String>,
#[arg(long, value_delimiter = ',')]
pub to: Vec<String>,
#[arg(long, conflicts_with = "body_file")]
pub body: Option<String>,
#[arg(long)]
pub body_file: Option<String>,
#[arg(short = 'y', long)]
pub yes: bool,
#[arg(long)]
pub draft: bool,
#[arg(long)]
pub attach: Vec<std::path::PathBuf>,
}
pub const MAIL_SUBCOMMAND_NAMES: &[&str] = &[
"list",
"show",
"search",
"mark-read",
"mark-unread",
"flag",
"unflag",
"archive",
"new",
"reply",
"reply-all",
"forward",
"delete",
"help",
];
#[derive(clap::Subcommand)]
pub enum DraftsCommands {
List {
#[arg(long)]
account: Vec<String>,
#[arg(short = 'n', long, default_value = "25")]
limit: usize,
#[arg(short = 'p', long, default_value = "1")]
page: usize,
#[arg(short = 'c', long)]
compact: bool,
},
Show {
fragment: String,
},
Edit {
fragment: String,
},
Send {
fragment: String,
#[arg(short = 'y', long)]
yes: bool,
},
Delete {
fragment: String,
#[arg(short = 'y', long)]
yes: bool,
},
Attachments {
#[command(subcommand)]
command: DraftAttachmentCommands,
},
}
#[derive(clap::Subcommand)]
pub enum DraftAttachmentCommands {
List {
fragment: String,
},
Add {
fragment: String,
path: std::path::PathBuf,
},
Remove {
fragment: String,
name: String,
},
}
#[derive(clap::Subcommand)]
pub enum TrustCommands {
List,
Add {
email: String,
},
Remove {
email: String,
},
}
#[derive(Clone, clap::ValueEnum)]
pub enum Shell {
Bash,
Zsh,
Fish,
Powershell,
}
impl Cli {
pub async fn run(self) -> Result<()> {
match self.command {
Some(Commands::Ai { command }) => crate::commands::ai::run(command).await,
Some(Commands::Account { command }) => {
crate::commands::account::run(command, self.json).await
}
Some(Commands::Mail { command }) => {
crate::commands::mail::run(command, self.json).await
}
Some(Commands::Trust { command }) => {
crate::commands::trust::run(command, self.json).await
}
Some(Commands::Drafts { command }) => {
crate::commands::drafts::run(command, self.json).await
}
Some(Commands::Completion { shell }) => {
crate::commands::completion::generate_completions(shell);
Ok(())
}
Some(Commands::Version) => {
crate::banner::print_banner_with_version();
Ok(())
}
None => {
use clap::CommandFactory;
let mut cmd = Self::command();
cmd.print_help()?;
println!();
Ok(())
}
}
}
}