mirror of
https://github.com/pimalaya/himalaya.git
synced 2026-06-17 13:17:55 +08:00
list imap mailboxes
This commit is contained in:
+123
-116
@@ -1,40 +1,59 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
use color_eyre::Result;
|
||||
use pimalaya_tui::{
|
||||
use anyhow::Result;
|
||||
use clap::{Args, CommandFactory, Parser, Subcommand};
|
||||
use pimalaya_toolbox::{
|
||||
config::TomlConfig,
|
||||
long_version,
|
||||
terminal::{
|
||||
cli::{
|
||||
arg::path_parser,
|
||||
printer::{OutputFmt, Printer},
|
||||
clap::{
|
||||
args::{AccountArg, JsonFlag, LogFlags},
|
||||
commands::{CompletionCommand, ManualCommand},
|
||||
parsers::path_parser,
|
||||
},
|
||||
config::TomlConfig as _,
|
||||
printer::Printer,
|
||||
},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
account::command::AccountSubcommand,
|
||||
completion::command::CompletionGenerateCommand,
|
||||
config::TomlConfig,
|
||||
envelope::command::EnvelopeSubcommand,
|
||||
flag::command::FlagSubcommand,
|
||||
folder::command::FolderSubcommand,
|
||||
manual::command::ManualGenerateCommand,
|
||||
message::{
|
||||
attachment::command::AttachmentSubcommand, command::MessageSubcommand,
|
||||
template::command::TemplateSubcommand,
|
||||
},
|
||||
// account::command::AccountSubcommand,
|
||||
account::Account,
|
||||
config::Config,
|
||||
folder::command::MailboxCommand, // message::{
|
||||
// attachment::command::AttachmentSubcommand, command::MessageSubcommand,
|
||||
// template::command::TemplateSubcommand,
|
||||
// },
|
||||
};
|
||||
|
||||
/// IMAP CLI (requires `imap` cargo feature).
|
||||
///
|
||||
/// This command gives you access to the IMAP CLI API, and allows
|
||||
/// you to manage IMAP mailboxes: list mailboxes, read messages,
|
||||
/// add flags etc.
|
||||
#[derive(Debug, Subcommand)]
|
||||
#[command(rename_all = "lowercase")]
|
||||
pub enum ImapCommand {
|
||||
#[command(subcommand)]
|
||||
#[command(aliases = ["mboxes", "mbox"])]
|
||||
Mailboxes(MailboxCommand),
|
||||
}
|
||||
|
||||
impl ImapCommand {
|
||||
pub fn execute(self, printer: &mut impl Printer, account: Account) -> Result<()> {
|
||||
match self {
|
||||
Self::Mailboxes(cmd) => cmd.execute(printer, account),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(name = env!("CARGO_PKG_NAME"))]
|
||||
#[command(author, version, about)]
|
||||
#[command(long_version = long_version!())]
|
||||
#[command(propagate_version = true, infer_subcommands = true)]
|
||||
pub struct Cli {
|
||||
pub struct HimalayaCli {
|
||||
#[command(subcommand)]
|
||||
pub command: Option<HimalayaCommand>,
|
||||
pub command: BackendCommand,
|
||||
|
||||
/// Override the default configuration file path.
|
||||
///
|
||||
@@ -49,123 +68,111 @@ pub struct Cli {
|
||||
#[arg(short, long = "config", global = true, env = "HIMALAYA_CONFIG")]
|
||||
#[arg(value_name = "PATH", value_parser = path_parser, value_delimiter = ':')]
|
||||
pub config_paths: Vec<PathBuf>,
|
||||
#[command(flatten)]
|
||||
pub account: AccountArg,
|
||||
#[command(flatten)]
|
||||
pub json: JsonFlag,
|
||||
#[command(flatten)]
|
||||
pub log: LogFlags,
|
||||
}
|
||||
|
||||
/// Customize the output format.
|
||||
///
|
||||
/// The output format determine how to display commands output to
|
||||
/// the terminal.
|
||||
///
|
||||
/// The possible values are:
|
||||
///
|
||||
/// - json: output will be in a form of a JSON-compatible object
|
||||
///
|
||||
/// - plain: output will be in a form of either a plain text or
|
||||
/// table, depending on the command
|
||||
#[arg(long, short, global = true)]
|
||||
#[arg(value_name = "FORMAT", value_enum, default_value_t = Default::default())]
|
||||
pub output: OutputFmt,
|
||||
#[derive(Debug, Subcommand)]
|
||||
pub enum BackendCommand {
|
||||
#[command(subcommand)]
|
||||
Imap(ImapCommand),
|
||||
}
|
||||
|
||||
/// Disable all logs.
|
||||
///
|
||||
/// Same as running command with `RUST_LOG=off` environment
|
||||
/// variable.
|
||||
#[arg(long, global = true)]
|
||||
#[arg(conflicts_with = "debug")]
|
||||
#[arg(conflicts_with = "trace")]
|
||||
pub quiet: bool,
|
||||
|
||||
/// Enable debug logs.
|
||||
///
|
||||
/// Same as running command with `RUST_LOG=debug` environment
|
||||
/// variable.
|
||||
#[arg(long, global = true)]
|
||||
#[arg(conflicts_with = "quiet")]
|
||||
#[arg(conflicts_with = "trace")]
|
||||
pub debug: bool,
|
||||
|
||||
/// Enable verbose trace logs with backtrace.
|
||||
///
|
||||
/// Same as running command with `RUST_LOG=trace` and
|
||||
/// `RUST_BACKTRACE=1` environment variables.
|
||||
#[arg(long, global = true)]
|
||||
#[arg(conflicts_with = "quiet")]
|
||||
#[arg(conflicts_with = "debug")]
|
||||
pub trace: bool,
|
||||
impl BackendCommand {
|
||||
pub fn execute(
|
||||
self,
|
||||
printer: &mut impl Printer,
|
||||
config_paths: &[PathBuf],
|
||||
account_name: Option<&str>,
|
||||
) -> Result<()> {
|
||||
match self {
|
||||
Self::Imap(cmd) => {
|
||||
let config = Config::from_paths_or_default(config_paths)?;
|
||||
let (_, account) = config.get_account(account_name)?;
|
||||
cmd.execute(printer, account)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Debug)]
|
||||
pub enum HimalayaCommand {
|
||||
#[command(subcommand)]
|
||||
#[command(alias = "accounts")]
|
||||
Account(AccountSubcommand),
|
||||
|
||||
// #[command(subcommand)]
|
||||
// #[command(alias = "accounts")]
|
||||
// Account(AccountSubcommand),
|
||||
#[command(subcommand)]
|
||||
#[command(visible_alias = "mailbox", aliases = ["mailboxes", "mboxes", "mbox"])]
|
||||
#[command(alias = "folders")]
|
||||
Folder(FolderSubcommand),
|
||||
Folder(MailboxCommand),
|
||||
|
||||
#[command(subcommand)]
|
||||
#[command(alias = "envelopes")]
|
||||
Envelope(EnvelopeSubcommand),
|
||||
// #[command(subcommand)]
|
||||
// #[command(alias = "envelopes")]
|
||||
// Envelope(EnvelopeSubcommand),
|
||||
|
||||
#[command(subcommand)]
|
||||
#[command(alias = "flags")]
|
||||
Flag(FlagSubcommand),
|
||||
// #[command(subcommand)]
|
||||
// #[command(alias = "flags")]
|
||||
// Flag(FlagSubcommand),
|
||||
|
||||
#[command(subcommand)]
|
||||
#[command(alias = "messages", alias = "msgs", alias = "msg")]
|
||||
Message(MessageSubcommand),
|
||||
// #[command(subcommand)]
|
||||
// #[command(alias = "messages", alias = "msgs", alias = "msg")]
|
||||
// Message(MessageSubcommand),
|
||||
|
||||
#[command(subcommand)]
|
||||
#[command(alias = "attachments")]
|
||||
Attachment(AttachmentSubcommand),
|
||||
|
||||
#[command(subcommand)]
|
||||
#[command(alias = "templates", alias = "tpls", alias = "tpl")]
|
||||
Template(TemplateSubcommand),
|
||||
// #[command(subcommand)]
|
||||
// #[command(alias = "attachments")]
|
||||
// Attachment(AttachmentSubcommand),
|
||||
|
||||
// #[command(subcommand)]
|
||||
// #[command(alias = "templates", alias = "tpls", alias = "tpl")]
|
||||
// Template(TemplateSubcommand),
|
||||
#[command(arg_required_else_help = true, alias = "mans")]
|
||||
Manuals(ManualCommand),
|
||||
#[command(arg_required_else_help = true)]
|
||||
#[command(alias = "manuals", alias = "mans")]
|
||||
Manual(ManualGenerateCommand),
|
||||
|
||||
#[command(arg_required_else_help = true)]
|
||||
#[command(alias = "completions")]
|
||||
Completion(CompletionGenerateCommand),
|
||||
Completions(CompletionCommand),
|
||||
}
|
||||
|
||||
impl HimalayaCommand {
|
||||
pub async fn execute(self, printer: &mut impl Printer, config_paths: &[PathBuf]) -> Result<()> {
|
||||
pub fn execute(
|
||||
self,
|
||||
printer: &mut impl Printer,
|
||||
config_paths: &[PathBuf],
|
||||
account_name: Option<&str>,
|
||||
) -> Result<()> {
|
||||
match self {
|
||||
Self::Account(cmd) => {
|
||||
let config = TomlConfig::from_paths_or_default(config_paths).await?;
|
||||
cmd.execute(printer, config, config_paths.first()).await
|
||||
}
|
||||
// Self::Account(cmd) => {
|
||||
// let config = TomlConfig::from_paths_or_default(config_paths).await?;
|
||||
// cmd.execute(printer, config, config_paths.first()).await
|
||||
// }
|
||||
Self::Folder(cmd) => {
|
||||
let config = TomlConfig::from_paths_or_default(config_paths).await?;
|
||||
cmd.execute(printer, &config).await
|
||||
let config = Config::from_paths_or_default(config_paths)?;
|
||||
let (_, account) = config.get_account(account_name)?;
|
||||
cmd.execute(printer, account)
|
||||
}
|
||||
Self::Envelope(cmd) => {
|
||||
let config = TomlConfig::from_paths_or_default(config_paths).await?;
|
||||
cmd.execute(printer, &config).await
|
||||
}
|
||||
Self::Flag(cmd) => {
|
||||
let config = TomlConfig::from_paths_or_default(config_paths).await?;
|
||||
cmd.execute(printer, &config).await
|
||||
}
|
||||
Self::Message(cmd) => {
|
||||
let config = TomlConfig::from_paths_or_default(config_paths).await?;
|
||||
cmd.execute(printer, &config).await
|
||||
}
|
||||
Self::Attachment(cmd) => {
|
||||
let config = TomlConfig::from_paths_or_default(config_paths).await?;
|
||||
cmd.execute(printer, &config).await
|
||||
}
|
||||
Self::Template(cmd) => {
|
||||
let config = TomlConfig::from_paths_or_default(config_paths).await?;
|
||||
cmd.execute(printer, &config).await
|
||||
}
|
||||
Self::Manual(cmd) => cmd.execute(printer).await,
|
||||
Self::Completion(cmd) => cmd.execute().await,
|
||||
// Self::Envelope(cmd) => {
|
||||
// let config = TomlConfig::from_paths_or_default(config_paths).await?;
|
||||
// cmd.execute(printer, &config).await
|
||||
// }
|
||||
// Self::Flag(cmd) => {
|
||||
// let config = TomlConfig::from_paths_or_default(config_paths).await?;
|
||||
// cmd.execute(printer, &config).await
|
||||
// }
|
||||
// Self::Message(cmd) => {
|
||||
// let config = TomlConfig::from_paths_or_default(config_paths).await?;
|
||||
// cmd.execute(printer, &config).await
|
||||
// }
|
||||
// Self::Attachment(cmd) => {
|
||||
// let config = TomlConfig::from_paths_or_default(config_paths).await?;
|
||||
// cmd.execute(printer, &config).await
|
||||
// }
|
||||
// Self::Template(cmd) => {
|
||||
// let config = TomlConfig::from_paths_or_default(config_paths).await?;
|
||||
// cmd.execute(printer, &config).await
|
||||
// }
|
||||
Self::Manuals(cmd) => cmd.execute(printer, HimalayaCli::command()),
|
||||
Self::Completions(cmd) => cmd.execute(printer, HimalayaCli::command()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user