add info log for copy/move/delete cmds

This commit is contained in:
Clément DOUIN 2021-04-08 18:33:12 +02:00
parent aaedfd36b8
commit 8e6740db65
No known key found for this signature in database
GPG key ID: 69C9B9CFFDEE2DEF
2 changed files with 56 additions and 9 deletions

View file

@ -9,7 +9,7 @@ use crate::{
input,
mbox::cli::mbox_target_arg,
msg::model::{Attachments, Msg, Msgs, ReadableMsg},
output::utils::print,
output::utils::{print, Info},
smtp,
};
@ -437,6 +437,14 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> {
flags.push(Flag::Seen);
imap_conn.append_msg(target, &msg.raw, &flags)?;
imap_conn.logout();
print(
&output_fmt,
Info(format!(
"Message {} successfully copied to folder `{}`",
&uid, &target
)),
)?;
return Ok(());
}
@ -450,6 +458,14 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> {
imap_conn.append_msg(target, &msg.raw, msg.flags.deref())?;
imap_conn.add_flags(mbox, uid, "\\Seen \\Deleted")?;
imap_conn.logout();
print(
&output_fmt,
Info(format!(
"Message {} successfully moved to folder `{}`",
&uid, &target
)),
)?;
return Ok(());
}
@ -458,6 +474,11 @@ pub fn msg_matches(matches: &ArgMatches) -> Result<()> {
let uid = matches.value_of("uid").unwrap();
imap_conn.add_flags(mbox, uid, "\\Seen \\Deleted")?;
imap_conn.logout();
print(
&output_fmt,
Info(format!("Message {} successfully deleted", &uid)),
)?;
return Ok(());
}

View file

@ -1,25 +1,51 @@
use error_chain::error_chain;
use serde::Serialize;
use std::{fmt::Display, process::Command};
use serde::{
ser::{self, SerializeStruct},
Serialize,
};
use std::{fmt, process::Command, result};
error_chain! {}
error_chain! {
foreign_links {
Utf8(std::string::FromUtf8Error);
Io(std::io::Error);
}
}
pub struct Info(pub String);
impl fmt::Display for Info {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl ser::Serialize for Info {
fn serialize<S>(&self, serializer: S) -> result::Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
let mut state = serializer.serialize_struct("Info", 1)?;
state.serialize_field("info", &self.0)?;
state.end()
}
}
pub fn run_cmd(cmd: &str) -> Result<String> {
let output = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", cmd]).output()
} else {
Command::new("sh").arg("-c").arg(cmd).output()
}
.chain_err(|| "Run command failed")?;
}?;
Ok(String::from_utf8(output.stdout).chain_err(|| "Invalid utf8 output")?)
Ok(String::from_utf8(output.stdout)?)
}
pub fn print<T: Display + Serialize>(output_type: &str, item: T) -> Result<()> {
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(|| "Invalid JSON string")?
serde_json::to_string(&item).chain_err(|| "Could not decode JSON")?
),
"text" | _ => println!("{}", item.to_string()),
}