add silent flag (#74)

This commit is contained in:
Clément DOUIN 2021-04-08 18:42:34 +02:00
parent 8e6740db65
commit ebf1b854be
No known key found for this signature in database
GPG key ID: 69C9B9CFFDEE2DEF
7 changed files with 43 additions and 27 deletions

View file

@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Wiki entry for Gmail users [#58]
- Info logs for copy/move/delete cmd + silent mode [#74]
## [0.2.3] - 2021-04-08
@ -132,4 +133,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#58]: https://github.com/soywod/himalaya/issues/58
[#61]: https://github.com/soywod/himalaya/issues/61
[#71]: https://github.com/soywod/himalaya/issues/71
[#74]: https://github.com/soywod/himalaya/issues/74
[#75]: https://github.com/soywod/himalaya/issues/75

View file

@ -82,15 +82,16 @@ for all the options.*
## Usage
```
himalaya 0.2.0
himalaya 0.2.3
soywod <clement.douin@posteo.net>
📫 Minimalist CLI email client
USAGE:
himalaya [OPTIONS] [SUBCOMMAND]
himalaya [FLAGS] [OPTIONS] [SUBCOMMAND]
FLAGS:
-h, --help Prints help information
-s, --silent Disables any output
-V, --version Prints version information
OPTIONS:

View file

@ -36,7 +36,7 @@ use crate::{
imap::cli::{imap_matches, imap_subcmds},
mbox::cli::{mbox_matches, mbox_source_arg, mbox_subcmds},
msg::cli::{msg_matches, msg_subcmds},
output::cli::output_arg,
output::cli::output_args,
};
error_chain! {
@ -53,7 +53,7 @@ fn run() -> Result<()> {
.version(env!("CARGO_PKG_VERSION"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.author(env!("CARGO_PKG_AUTHORS"))
.arg(output_arg())
.args(&output_args())
.arg(account_arg())
.arg(mbox_source_arg())
.subcommands(flag_subcmds())

View file

@ -37,11 +37,12 @@ pub fn mbox_matches(matches: &ArgMatches) -> Result<bool> {
let config = Config::new_from_file()?;
let account = config.find_account_by_name(matches.value_of("account"))?;
let output_fmt = matches.value_of("output").unwrap();
let silent = matches.is_present("silent");
if let Some(_) = matches.subcommand_matches("mailboxes") {
let mut imap_conn = ImapConnector::new(&account)?;
let mboxes = imap_conn.list_mboxes()?;
print(&output_fmt, mboxes)?;
print(&output_fmt, &silent, mboxes)?;
imap_conn.logout();
return Ok(true);
}

View file

@ -163,6 +163,7 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> {
let config = Config::new_from_file()?;
let account = config.find_account_by_name(matches.value_of("account"))?;
let output_fmt = matches.value_of("output").unwrap();
let silent = matches.is_present("silent");
let mbox = matches.value_of("mailbox").unwrap();
if let Some(matches) = matches.subcommand_matches("list") {
@ -171,7 +172,7 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> {
let page: usize = matches.value_of("page").unwrap().parse().unwrap();
let msgs = imap_conn.list_msgs(&mbox, &page_size, &page)?;
let msgs = Msgs::from(&msgs);
print(&output_fmt, msgs)?;
print(&output_fmt, &silent, msgs)?;
imap_conn.logout();
return Ok(());
}
@ -206,7 +207,7 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> {
.join(" ");
let msgs = imap_conn.search_msgs(&mbox, &query, &page_size, &page)?;
let msgs = Msgs::from(&msgs);
print(&output_fmt, msgs)?;
print(&output_fmt, &silent, msgs)?;
imap_conn.logout();
return Ok(());
}
@ -217,7 +218,7 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> {
let mime = format!("text/{}", matches.value_of("mime-type").unwrap());
let msg = imap_conn.read_msg(&mbox, &uid)?;
let msg = ReadableMsg::from_bytes(&mime, &msg)?;
print(&output_fmt, msg)?;
print(&output_fmt, &silent, msg)?;
imap_conn.logout();
return Ok(());
}
@ -304,7 +305,7 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> {
if let Some(matches) = matches.subcommand_matches("template") {
if let Some(_) = matches.subcommand_matches("new") {
let tpl = Msg::build_new_tpl(&config, &account)?;
print(&output_fmt, &tpl)?;
print(&output_fmt, &silent, &tpl)?;
}
if let Some(matches) = matches.subcommand_matches("reply") {
@ -316,7 +317,7 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> {
} else {
msg.build_reply_tpl(&config, &account)?
};
print(&output_fmt, &tpl)?;
print(&output_fmt, &silent, &tpl)?;
imap_conn.logout();
}
@ -325,7 +326,7 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> {
let uid = matches.value_of("uid").unwrap();
let msg = Msg::from(imap_conn.read_msg(&mbox, &uid)?);
let tpl = msg.build_forward_tpl(&config, &account)?;
print(&output_fmt, &tpl)?;
print(&output_fmt, &silent, &tpl)?;
imap_conn.logout();
}
@ -440,6 +441,7 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> {
print(
&output_fmt,
&silent,
Info(format!(
"Message {} successfully copied to folder `{}`",
&uid, &target
@ -461,6 +463,7 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> {
print(
&output_fmt,
&silent,
Info(format!(
"Message {} successfully moved to folder `{}`",
&uid, &target
@ -477,6 +480,7 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> {
print(
&output_fmt,
&silent,
Info(format!("Message {} successfully deleted", &uid)),
)?;
return Ok(());
@ -506,7 +510,7 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> {
let mut imap_conn = ImapConnector::new(&account)?;
let msgs = imap_conn.list_msgs(&mbox, &10, &0)?;
let msgs = Msgs::from(&msgs);
print(&output_fmt, msgs)?;
print(&output_fmt, &silent, msgs)?;
imap_conn.logout();
Ok(())
}

View file

@ -1,11 +1,17 @@
use clap::Arg;
pub fn output_arg<'a>() -> Arg<'a, 'a> {
Arg::with_name("output")
.long("output")
.short("o")
.help("Defines the output format")
.value_name("STRING")
.possible_values(&["plain", "json"])
.default_value("plain")
pub fn output_args<'a>() -> Vec<Arg<'a, 'a>> {
vec![
Arg::with_name("output")
.long("output")
.short("o")
.help("Defines the output format")
.value_name("STRING")
.possible_values(&["plain", "json"])
.default_value("plain"),
Arg::with_name("silent")
.long("silent")
.short("s")
.help("Disables any output"),
]
}

View file

@ -41,13 +41,15 @@ pub fn run_cmd(cmd: &str) -> Result<String> {
Ok(String::from_utf8(output.stdout)?)
}
pub fn print<T: fmt::Display + Serialize>(output_type: &str, item: T) -> Result<()> {
match output_type {
"json" => print!(
"{}",
serde_json::to_string(&item).chain_err(|| "Could not decode JSON")?
),
"text" | _ => println!("{}", item.to_string()),
pub fn print<T: fmt::Display + Serialize>(output_type: &str, silent: &bool, item: T) -> Result<()> {
if silent == &false {
match output_type {
"json" => print!(
"{}",
serde_json::to_string(&item).chain_err(|| "Could not decode JSON")?
),
"text" | _ => println!("{}", item.to_string()),
}
}
Ok(())