diff --git a/src/cli.rs b/src/cli.rs index 8c3e860c..4c2b1dff 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,50 +1,22 @@ use std::path::PathBuf; use anyhow::Result; -use clap::{Args, CommandFactory, Parser, Subcommand}; +use clap::{Parser, Subcommand}; use pimalaya_toolbox::{ config::TomlConfig, long_version, terminal::{ clap::{ args::{AccountArg, JsonFlag, LogFlags}, - commands::{CompletionCommand, ManualCommand}, parsers::path_parser, }, printer::Printer, }, }; -use crate::{ - // 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), - } - } -} +use crate::config::Config; +#[cfg(feature = "imap")] +use crate::imap::command::ImapCommand; #[derive(Parser, Debug)] #[command(name = env!("CARGO_PKG_NAME"))] @@ -78,6 +50,7 @@ pub struct HimalayaCli { #[derive(Debug, Subcommand)] pub enum BackendCommand { + #[cfg(feature = "imap")] #[command(subcommand)] Imap(ImapCommand), } @@ -98,81 +71,3 @@ impl BackendCommand { } } } - -#[derive(Subcommand, Debug)] -pub enum HimalayaCommand { - // #[command(subcommand)] - // #[command(alias = "accounts")] - // Account(AccountSubcommand), - #[command(subcommand)] - #[command(visible_alias = "mailbox", aliases = ["mailboxes", "mboxes", "mbox"])] - #[command(alias = "folders")] - Folder(MailboxCommand), - - // #[command(subcommand)] - // #[command(alias = "envelopes")] - // Envelope(EnvelopeSubcommand), - - // #[command(subcommand)] - // #[command(alias = "flags")] - // Flag(FlagSubcommand), - - // #[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(arg_required_else_help = true, alias = "mans")] - Manuals(ManualCommand), - #[command(arg_required_else_help = true)] - Completions(CompletionCommand), -} - -impl HimalayaCommand { - 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::Folder(cmd) => { - 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::Manuals(cmd) => cmd.execute(printer, HimalayaCli::command()), - Self::Completions(cmd) => cmd.execute(printer, HimalayaCli::command()), - } - } -} diff --git a/src/completion/command.rs b/src/completion/command.rs deleted file mode 100644 index 152a2ae7..00000000 --- a/src/completion/command.rs +++ /dev/null @@ -1,32 +0,0 @@ -use std::io; - -use clap::{value_parser, CommandFactory, Parser}; -use clap_complete::Shell; -use color_eyre::Result; -use tracing::info; - -use crate::cli::Cli; - -/// Print completion script for the given shell to stdout. -/// -/// This command allows you to generate completion script for a given -/// shell. The script is printed to the standard output. If you want -/// to write it to a file, just use unix redirection. -#[derive(Debug, Parser)] -pub struct CompletionGenerateCommand { - /// Shell for which completion script should be generated for. - #[arg(value_parser = value_parser!(Shell))] - pub shell: Shell, -} - -impl CompletionGenerateCommand { - pub async fn execute(self) -> Result<()> { - info!("executing generate completion command"); - - let mut cmd = Cli::command(); - let name = cmd.get_name().to_string(); - clap_complete::generate(self.shell, &mut cmd, name, &mut io::stdout()); - - Ok(()) - } -} diff --git a/src/completion/mod.rs b/src/completion/mod.rs deleted file mode 100644 index 9fe79612..00000000 --- a/src/completion/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod command; diff --git a/src/imap/command.rs b/src/imap/command.rs new file mode 100644 index 00000000..2dd19e7e --- /dev/null +++ b/src/imap/command.rs @@ -0,0 +1,26 @@ +use anyhow::Result; +use clap::Subcommand; +use pimalaya_toolbox::terminal::printer::Printer; + +use crate::{account::Account, imap::mailbox::command::MailboxCommand}; + +/// 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), + } + } +} diff --git a/src/folder/arg/mod.rs b/src/imap/mailbox/arg/mod.rs similarity index 100% rename from src/folder/arg/mod.rs rename to src/imap/mailbox/arg/mod.rs diff --git a/src/folder/arg/name.rs b/src/imap/mailbox/arg/name.rs similarity index 100% rename from src/folder/arg/name.rs rename to src/imap/mailbox/arg/name.rs diff --git a/src/folder/command/add.rs b/src/imap/mailbox/command/add.rs similarity index 100% rename from src/folder/command/add.rs rename to src/imap/mailbox/command/add.rs diff --git a/src/folder/command/delete.rs b/src/imap/mailbox/command/delete.rs similarity index 100% rename from src/folder/command/delete.rs rename to src/imap/mailbox/command/delete.rs diff --git a/src/folder/command/expunge.rs b/src/imap/mailbox/command/expunge.rs similarity index 100% rename from src/folder/command/expunge.rs rename to src/imap/mailbox/command/expunge.rs diff --git a/src/folder/command/list.rs b/src/imap/mailbox/command/list.rs similarity index 100% rename from src/folder/command/list.rs rename to src/imap/mailbox/command/list.rs diff --git a/src/folder/command/mod.rs b/src/imap/mailbox/command/mod.rs similarity index 92% rename from src/folder/command/mod.rs rename to src/imap/mailbox/command/mod.rs index 6be6a517..d1e45f86 100644 --- a/src/folder/command/mod.rs +++ b/src/imap/mailbox/command/mod.rs @@ -1,14 +1,14 @@ // mod add; // mod delete; // mod expunge; -mod list; +pub mod list; // mod purge; use anyhow::Result; use clap::Subcommand; use pimalaya_toolbox::terminal::printer::Printer; -use crate::{account::Account, folder::command::list::ListMailboxesCommand}; +use crate::{account::Account, imap::mailbox::command::list::ListMailboxesCommand}; /// Create, list and purge mailboxes. /// diff --git a/src/folder/command/purge.rs b/src/imap/mailbox/command/purge.rs similarity index 100% rename from src/folder/command/purge.rs rename to src/imap/mailbox/command/purge.rs diff --git a/src/folder/mod.rs b/src/imap/mailbox/mod.rs similarity index 100% rename from src/folder/mod.rs rename to src/imap/mailbox/mod.rs diff --git a/src/manual/mod.rs b/src/imap/mod.rs similarity index 50% rename from src/manual/mod.rs rename to src/imap/mod.rs index 9fe79612..45932a43 100644 --- a/src/manual/mod.rs +++ b/src/imap/mod.rs @@ -1 +1,2 @@ pub mod command; +pub mod mailbox; diff --git a/src/lib.rs b/src/lib.rs index 856e6779..2a9c7283 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,8 @@ pub mod account; pub mod cli; pub mod config; -pub mod folder; +#[cfg(feature = "imap")] +pub mod imap; pub mod sasl; pub mod stream; pub mod tls; diff --git a/src/manual/command.rs b/src/manual/command.rs deleted file mode 100644 index 2857efb2..00000000 --- a/src/manual/command.rs +++ /dev/null @@ -1,62 +0,0 @@ -use std::{fs, path::PathBuf}; - -use clap::{CommandFactory, Parser}; -use clap_mangen::Man; -use color_eyre::Result; -use pimalaya_tui::terminal::cli::printer::Printer; -use tracing::info; - -use crate::{cli::Cli, dir_parser}; - -/// Generate manual pages to the given directory. -/// -/// This command allows you to generate manual pages (following the -/// man page format) to the given directory. If the directory does not -/// exist, it will be created. Any existing man pages will be -/// overriden. -#[derive(Debug, Parser)] -pub struct ManualGenerateCommand { - /// Directory where man files should be generated in. - #[arg(value_parser = dir_parser)] - pub dir: PathBuf, -} - -impl ManualGenerateCommand { - pub async fn execute(self, printer: &mut impl Printer) -> Result<()> { - info!("executing generate manuals command"); - - let cmd = Cli::command(); - let cmd_name = cmd.get_name().to_string(); - let subcmds = cmd.get_subcommands().cloned().collect::>(); - let subcmds_len = subcmds.len() + 1; - - let mut buffer = Vec::new(); - Man::new(cmd).render(&mut buffer)?; - - fs::create_dir_all(&self.dir)?; - printer.log(format!("Generating man page for command {cmd_name}…\n"))?; - fs::write(self.dir.join(format!("{}.1", cmd_name)), buffer)?; - - for subcmd in subcmds { - let subcmd_name = subcmd.get_name().to_string(); - - let mut buffer = Vec::new(); - Man::new(subcmd).render(&mut buffer)?; - - printer.log(format!( - "Generating man page for subcommand {subcmd_name}…\n" - ))?; - fs::write( - self.dir.join(format!("{}-{}.1", cmd_name, subcmd_name)), - buffer, - )?; - } - - printer.log(format!( - "{subcmds_len} man page(s) successfully generated in {}!\n", - self.dir.display() - ))?; - - Ok(()) - } -}