refactor stdin for send and tpl cmds

This commit is contained in:
Clément DOUIN
2021-05-31 14:19:55 +02:00
parent f71720e6b9
commit 28448310c7
4 changed files with 18 additions and 3 deletions
+3 -2
View File
@@ -1,3 +1,4 @@
use atty::Stream;
use clap;
use error_chain::error_chain;
use log::{debug, error, trace};
@@ -100,7 +101,7 @@ pub fn msg_subcmds<'a>() -> Vec<clap::App<'a, 'a>> {
.arg(attachment_arg()),
clap::SubCommand::with_name("send")
.about("Sends a raw message")
.arg(clap::Arg::with_name("message").raw(true)),
.arg(clap::Arg::with_name("message").raw(true).last(true)),
clap::SubCommand::with_name("save")
.about("Saves a raw message")
.arg(clap::Arg::with_name("message").raw(true)),
@@ -558,7 +559,7 @@ fn msg_matches_send(app: &App, matches: &clap::ArgMatches) -> Result<bool> {
let mut imap_conn = ImapConnector::new(&app.account)?;
let msg = if matches.is_present("message") {
let msg = if atty::is(Stream::Stdin) {
matches
.value_of("message")
.unwrap_or_default()
+13 -1
View File
@@ -1,7 +1,9 @@
use atty::Stream;
use clap;
use error_chain::error_chain;
use log::{debug, trace};
use mailparse;
use std::io::{self, BufRead};
use crate::{app::App, imap::model::ImapConnector, msg::tpl::model::Tpl};
@@ -156,7 +158,17 @@ fn tpl_matches_new(app: &App, matches: &clap::ArgMatches) -> Result<bool> {
tpl.header(key, val);
}
if let Some(body) = matches.value_of("body") {
if atty::isnt(Stream::Stdin) {
let body = io::stdin()
.lock()
.lines()
.filter_map(|ln| ln.ok())
.map(|ln| ln.to_string())
.collect::<Vec<_>>()
.join("\n");
debug!("overriden body from stdin: {:?}", body);
tpl.body(body);
} else if let Some(body) = matches.value_of("body") {
debug!("overriden body: {:?}", body);
tpl.body(body);
};