From 8030cf16ca676e20840618f1f623f5caf43dcd3c Mon Sep 17 00:00:00 2001 From: Ralph Caraveo Date: Thu, 9 Jul 2020 20:45:17 -0700 Subject: [PATCH 1/4] Adds a debug option for espanso when executing shell commands for greater context --- src/extension/shell.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/extension/shell.rs b/src/extension/shell.rs index f1b9da4..82186ea 100644 --- a/src/extension/shell.rs +++ b/src/extension/shell.rs @@ -123,7 +123,7 @@ impl super::Extension for ShellExtension { fn calculate(&self, params: &Mapping, args: &Vec) -> Option { let cmd = params.get(&Value::from("cmd")); if cmd.is_none() { - warn!("No 'cmd' parameter specified for shell variable"); + warn!("No 'cmd' parameter specified for shell `iable"); return None; } let cmd = cmd.unwrap().as_str().unwrap(); @@ -171,6 +171,19 @@ impl super::Extension for ShellExtension { warn!("Shell command reported error: \n{}", error_str); } + // Check if debug flag set, provide additional context when an error occurs. + let debug_opt = params.get(&Value::from("debug")); + let with_debug = if let Some(value) = debug_opt { + let val = value.as_bool(); + val.unwrap_or(false) + }else{ + false + }; + + if with_debug { + error!("debug for shell cmd '{}', exit_status '{}', stdout '{}', stderr '{}'", cmd, output.status, output_str, error_str); + } + // If specified, trim the output let trim_opt = params.get(&Value::from("trim")); let should_trim = if let Some(value) = trim_opt { From a9043e3c5bb48fefda6a4d52cbf9bd7bc7163583 Mon Sep 17 00:00:00 2001 From: Ralph Caraveo Date: Thu, 9 Jul 2020 20:50:33 -0700 Subject: [PATCH 2/4] fix typo --- src/extension/shell.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extension/shell.rs b/src/extension/shell.rs index 82186ea..05a51b0 100644 --- a/src/extension/shell.rs +++ b/src/extension/shell.rs @@ -123,7 +123,7 @@ impl super::Extension for ShellExtension { fn calculate(&self, params: &Mapping, args: &Vec) -> Option { let cmd = params.get(&Value::from("cmd")); if cmd.is_none() { - warn!("No 'cmd' parameter specified for shell `iable"); + warn!("No 'cmd' parameter specified for shell variable"); return None; } let cmd = cmd.unwrap().as_str().unwrap(); From 0c49adcc129d38372a1c04cb9dfb5f3afe5a43bf Mon Sep 17 00:00:00 2001 From: Ralph Caraveo Date: Fri, 10 Jul 2020 21:28:20 -0700 Subject: [PATCH 3/4] change error to info --- src/extension/shell.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/extension/shell.rs b/src/extension/shell.rs index 05a51b0..ad3dad5 100644 --- a/src/extension/shell.rs +++ b/src/extension/shell.rs @@ -17,7 +17,7 @@ * along with espanso. If not, see . */ -use log::{error, warn}; +use log::{info, error, warn}; use regex::{Captures, Regex}; use serde_yaml::{Mapping, Value}; use std::process::{Command, Output}; @@ -181,7 +181,7 @@ impl super::Extension for ShellExtension { }; if with_debug { - error!("debug for shell cmd '{}', exit_status '{}', stdout '{}', stderr '{}'", cmd, output.status, output_str, error_str); + info!("debug for shell cmd '{}', exit_status '{}', stdout '{}', stderr '{}'", cmd, output.status, output_str, error_str); } // If specified, trim the output From 239461e520ae7524d1cde468c1a71cba5e2cbd46 Mon Sep 17 00:00:00 2001 From: Ralph Caraveo Date: Fri, 10 Jul 2020 21:47:10 -0700 Subject: [PATCH 4/4] just render the original command --- src/extension/shell.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/extension/shell.rs b/src/extension/shell.rs index ad3dad5..1907513 100644 --- a/src/extension/shell.rs +++ b/src/extension/shell.rs @@ -17,7 +17,7 @@ * along with espanso. If not, see . */ -use log::{info, error, warn}; +use log::{error, info, warn}; use regex::{Captures, Regex}; use serde_yaml::{Mapping, Value}; use std::process::{Command, Output}; @@ -126,11 +126,12 @@ impl super::Extension for ShellExtension { warn!("No 'cmd' parameter specified for shell variable"); return None; } - let cmd = cmd.unwrap().as_str().unwrap(); + + let original_cmd = cmd.unwrap().as_str().unwrap(); // Render positional parameters in args let cmd = POS_ARG_REGEX - .replace_all(&cmd, |caps: &Captures| { + .replace_all(&original_cmd, |caps: &Captures| { let position_str = caps.name("pos").unwrap().as_str(); let position = position_str.parse::().unwrap_or(-1); if position >= 0 && position < args.len() as i32 { @@ -176,12 +177,15 @@ impl super::Extension for ShellExtension { let with_debug = if let Some(value) = debug_opt { let val = value.as_bool(); val.unwrap_or(false) - }else{ + } else { false }; - + if with_debug { - info!("debug for shell cmd '{}', exit_status '{}', stdout '{}', stderr '{}'", cmd, output.status, output_str, error_str); + info!( + "debug for shell cmd '{}', exit_status '{}', stdout '{}', stderr '{}'", + original_cmd, output.status, output_str, error_str + ); } // If specified, trim the output