From 8e6740db65b5ec3b9d51863695fcbf22b79b0173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Thu, 8 Apr 2021 18:33:12 +0200 Subject: [PATCH] add info log for copy/move/delete cmds --- src/msg/cli.rs | 23 ++++++++++++++++++++++- src/output/utils.rs | 42 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/src/msg/cli.rs b/src/msg/cli.rs index 34d54cb..0ab0774 100644 --- a/src/msg/cli.rs +++ b/src/msg/cli.rs @@ -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(()); } diff --git a/src/output/utils.rs b/src/output/utils.rs index 14f7a14..3ec4b5b 100644 --- a/src/output/utils.rs +++ b/src/output/utils.rs @@ -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(&self, serializer: S) -> result::Result + 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 { 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(output_type: &str, item: T) -> Result<()> { +pub fn print(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()), }