2023-12-06 17:09:49 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
use clap::{Parser, Subcommand};
|
2023-12-06 20:46:31 +00:00
|
|
|
use std::path::PathBuf;
|
2023-12-06 17:09:49 +00:00
|
|
|
|
|
|
|
use crate::{
|
2024-02-21 10:38:50 +00:00
|
|
|
account::command::AccountSubcommand,
|
2023-12-06 20:46:31 +00:00
|
|
|
completion::command::CompletionGenerateCommand,
|
2023-12-06 17:09:49 +00:00
|
|
|
config::{self, TomlConfig},
|
2024-02-21 10:38:50 +00:00
|
|
|
envelope::command::EnvelopeSubcommand,
|
|
|
|
flag::command::FlagSubcommand,
|
|
|
|
folder::command::FolderSubcommand,
|
2023-12-06 20:46:31 +00:00
|
|
|
manual::command::ManualGenerateCommand,
|
2024-02-21 10:38:50 +00:00
|
|
|
message::{
|
|
|
|
attachment::command::AttachmentSubcommand, command::MessageSubcommand,
|
|
|
|
template::command::TemplateSubcommand,
|
|
|
|
},
|
2023-12-06 17:09:49 +00:00
|
|
|
output::{ColorFmt, OutputFmt},
|
|
|
|
printer::Printer,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Parser, Debug)]
|
2023-12-08 11:18:18 +00:00
|
|
|
#[command(name = "himalaya", author, version, about)]
|
|
|
|
#[command(propagate_version = true, infer_subcommands = true)]
|
2023-12-06 17:09:49 +00:00
|
|
|
pub struct Cli {
|
|
|
|
#[command(subcommand)]
|
2024-01-03 21:49:39 +00:00
|
|
|
pub command: Option<HimalayaCommand>,
|
2023-12-06 17:09:49 +00:00
|
|
|
|
2024-03-16 21:20:19 +00:00
|
|
|
/// Override the default configuration file path.
|
2023-12-06 17:09:49 +00:00
|
|
|
///
|
2024-03-16 21:20:19 +00:00
|
|
|
/// The given paths are shell-expanded then canonicalized (if
|
|
|
|
/// applicable). If the first path does not point to a valid file,
|
|
|
|
/// the wizard will propose to assist you in the creation of the
|
|
|
|
/// configuration file. Other paths are merged with the first one,
|
|
|
|
/// which allows you to separate your public config from your
|
|
|
|
/// private(s) one(s).
|
2023-12-31 23:27:45 +00:00
|
|
|
#[arg(short, long = "config", global = true)]
|
2023-12-07 11:19:45 +00:00
|
|
|
#[arg(value_name = "PATH", value_parser = config::path_parser)]
|
2024-03-16 21:20:19 +00:00
|
|
|
pub config_paths: Vec<PathBuf>,
|
2023-12-06 17:09:49 +00:00
|
|
|
|
2024-03-16 21:20:19 +00:00
|
|
|
/// Customize the output format.
|
2023-12-06 17:09:49 +00:00
|
|
|
///
|
|
|
|
/// 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
|
2023-12-07 11:19:45 +00:00
|
|
|
#[arg(long, short, global = true)]
|
|
|
|
#[arg(value_name = "FORMAT", value_enum, default_value_t = Default::default())]
|
2023-12-06 17:09:49 +00:00
|
|
|
pub output: OutputFmt,
|
|
|
|
|
|
|
|
/// Control when to use colors
|
|
|
|
///
|
|
|
|
/// The default setting is 'auto', which means himalaya will try
|
|
|
|
/// to guess when to use colors. For example, if himalaya is
|
|
|
|
/// printing to a terminal, then it will use colors, but if it is
|
|
|
|
/// redirected to a file or a pipe, then it will suppress color
|
|
|
|
/// output. himalaya will suppress color output in some other
|
|
|
|
/// circumstances as well. For example, if the TERM environment
|
|
|
|
/// variable is not set or set to 'dumb', then himalaya will not
|
|
|
|
/// use colors.
|
|
|
|
///
|
|
|
|
/// The possible values are:
|
|
|
|
///
|
|
|
|
/// - never: colors will never be used
|
|
|
|
///
|
|
|
|
/// - always: colors will always be used regardless of where output is sent
|
|
|
|
///
|
|
|
|
/// - ansi: like 'always', but emits ANSI escapes (even in a Windows console)
|
|
|
|
///
|
|
|
|
/// - auto: himalaya tries to be smart
|
2023-12-07 11:19:45 +00:00
|
|
|
#[arg(long, short = 'C', global = true)]
|
|
|
|
#[arg(value_name = "MODE", value_enum, default_value_t = Default::default())]
|
2023-12-06 17:09:49 +00:00
|
|
|
pub color: ColorFmt,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Subcommand, Debug)]
|
2023-12-06 20:46:31 +00:00
|
|
|
pub enum HimalayaCommand {
|
2023-12-07 11:19:45 +00:00
|
|
|
#[command(subcommand)]
|
|
|
|
#[command(alias = "accounts")]
|
2023-12-06 20:46:31 +00:00
|
|
|
Account(AccountSubcommand),
|
|
|
|
|
2023-12-07 11:19:45 +00:00
|
|
|
#[command(subcommand)]
|
2023-12-09 08:36:26 +00:00
|
|
|
#[command(visible_alias = "mailbox", aliases = ["mailboxes", "mboxes", "mbox"])]
|
2023-12-07 11:19:45 +00:00
|
|
|
#[command(alias = "folders")]
|
2023-12-06 20:46:31 +00:00
|
|
|
Folder(FolderSubcommand),
|
2023-12-06 17:09:49 +00:00
|
|
|
|
2023-12-07 11:19:45 +00:00
|
|
|
#[command(subcommand)]
|
|
|
|
#[command(alias = "envelopes")]
|
2023-12-06 22:12:06 +00:00
|
|
|
Envelope(EnvelopeSubcommand),
|
|
|
|
|
2023-12-07 11:19:45 +00:00
|
|
|
#[command(subcommand)]
|
|
|
|
#[command(alias = "flags")]
|
2023-12-07 09:10:18 +00:00
|
|
|
Flag(FlagSubcommand),
|
|
|
|
|
2023-12-07 11:19:45 +00:00
|
|
|
#[command(subcommand)]
|
|
|
|
#[command(alias = "messages", alias = "msgs", alias = "msg")]
|
|
|
|
Message(MessageSubcommand),
|
|
|
|
|
2023-12-07 21:37:28 +00:00
|
|
|
#[command(subcommand)]
|
2023-12-08 11:18:18 +00:00
|
|
|
#[command(alias = "attachments")]
|
|
|
|
Attachment(AttachmentSubcommand),
|
2023-12-07 21:37:28 +00:00
|
|
|
|
2023-12-07 20:59:12 +00:00
|
|
|
#[command(subcommand)]
|
2023-12-08 11:18:18 +00:00
|
|
|
#[command(alias = "templates", alias = "tpls", alias = "tpl")]
|
|
|
|
Template(TemplateSubcommand),
|
2023-12-07 20:59:12 +00:00
|
|
|
|
2023-12-06 20:46:31 +00:00
|
|
|
#[command(arg_required_else_help = true)]
|
2023-12-07 11:19:45 +00:00
|
|
|
#[command(alias = "manuals", alias = "mans")]
|
2023-12-06 20:46:31 +00:00
|
|
|
Manual(ManualGenerateCommand),
|
2023-12-06 17:09:49 +00:00
|
|
|
|
2023-12-06 20:46:31 +00:00
|
|
|
#[command(arg_required_else_help = true)]
|
2023-12-07 11:19:45 +00:00
|
|
|
#[command(alias = "completions")]
|
2023-12-06 20:46:31 +00:00
|
|
|
Completion(CompletionGenerateCommand),
|
2023-12-06 17:09:49 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 20:46:31 +00:00
|
|
|
impl HimalayaCommand {
|
2024-03-16 21:20:19 +00:00
|
|
|
pub async fn execute(self, printer: &mut impl Printer, config_paths: &[PathBuf]) -> Result<()> {
|
2023-12-06 17:09:49 +00:00
|
|
|
match self {
|
2023-12-31 23:27:45 +00:00
|
|
|
Self::Account(cmd) => {
|
2024-03-16 21:20:19 +00:00
|
|
|
let config = TomlConfig::from_paths_or_default(config_paths).await?;
|
2023-12-31 23:27:45 +00:00
|
|
|
cmd.execute(printer, &config).await
|
|
|
|
}
|
|
|
|
Self::Folder(cmd) => {
|
2024-03-16 21:20:19 +00:00
|
|
|
let config = TomlConfig::from_paths_or_default(config_paths).await?;
|
2023-12-31 23:27:45 +00:00
|
|
|
cmd.execute(printer, &config).await
|
|
|
|
}
|
|
|
|
Self::Envelope(cmd) => {
|
2024-03-16 21:20:19 +00:00
|
|
|
let config = TomlConfig::from_paths_or_default(config_paths).await?;
|
2023-12-31 23:27:45 +00:00
|
|
|
cmd.execute(printer, &config).await
|
|
|
|
}
|
|
|
|
Self::Flag(cmd) => {
|
2024-03-16 21:20:19 +00:00
|
|
|
let config = TomlConfig::from_paths_or_default(config_paths).await?;
|
2023-12-31 23:27:45 +00:00
|
|
|
cmd.execute(printer, &config).await
|
|
|
|
}
|
|
|
|
Self::Message(cmd) => {
|
2024-03-16 21:20:19 +00:00
|
|
|
let config = TomlConfig::from_paths_or_default(config_paths).await?;
|
2023-12-31 23:27:45 +00:00
|
|
|
cmd.execute(printer, &config).await
|
|
|
|
}
|
|
|
|
Self::Attachment(cmd) => {
|
2024-03-16 21:20:19 +00:00
|
|
|
let config = TomlConfig::from_paths_or_default(config_paths).await?;
|
2023-12-31 23:27:45 +00:00
|
|
|
cmd.execute(printer, &config).await
|
|
|
|
}
|
|
|
|
Self::Template(cmd) => {
|
2024-03-16 21:20:19 +00:00
|
|
|
let config = TomlConfig::from_paths_or_default(config_paths).await?;
|
2023-12-31 23:27:45 +00:00
|
|
|
cmd.execute(printer, &config).await
|
|
|
|
}
|
2023-12-06 20:46:31 +00:00
|
|
|
Self::Manual(cmd) => cmd.execute(printer).await,
|
2023-12-26 12:52:26 +00:00
|
|
|
Self::Completion(cmd) => cmd.execute().await,
|
2023-12-06 17:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|