aboutsummaryrefslogtreecommitdiffstats
path: root/zellij-utils/src
diff options
context:
space:
mode:
authorAram Drevekenin <aram@poor.dev>2023-10-16 13:42:19 +0200
committerGitHub <noreply@github.com>2023-10-16 13:42:19 +0200
commitd2b6fb5c5b0423143d71218196a56ea3e395cd47 (patch)
treeb913e91850c9eb98f554aec2dfc94cbe9b11bca6 /zellij-utils/src
parentfix(plugins): make new auxiliary functions public (#2765) (diff)
downloadsl0ppij-d2b6fb5c5b0423143d71218196a56ea3e395cd47.tar.gz
sl0ppij-d2b6fb5c5b0423143d71218196a56ea3e395cd47.tar.xz
sl0ppij-d2b6fb5c5b0423143d71218196a56ea3e395cd47.zip
feat(plugins): plugin run_command api (#2862)
* prototype * add tests * style(fmt): rustfmt * update comments * deprecation warning for execcmd
Diffstat (limited to 'zellij-utils/src')
-rw-r--r--zellij-utils/src/data.rs13
-rw-r--r--zellij-utils/src/errors.rs1
-rw-r--r--zellij-utils/src/plugin_api/event.proto14
-rw-r--r--zellij-utils/src/plugin_api/event.rs34
-rw-r--r--zellij-utils/src/plugin_api/plugin_command.proto19
-rw-r--r--zellij-utils/src/plugin_api/plugin_command.rs50
6 files changed, 127 insertions, 4 deletions
diff --git a/zellij-utils/src/data.rs b/zellij-utils/src/data.rs
index 3702dace..8ff43836 100644
--- a/zellij-utils/src/data.rs
+++ b/zellij-utils/src/data.rs
@@ -2,7 +2,7 @@ use crate::input::actions::Action;
use crate::input::config::ConversionError;
use clap::ArgEnum;
use serde::{Deserialize, Serialize};
-use std::collections::{HashMap, HashSet};
+use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt;
use std::path::{Path, PathBuf};
use std::str::FromStr;
@@ -496,6 +496,8 @@ pub enum Event {
/// A Result of plugin permission request
PermissionRequestResult(PermissionStatus),
SessionUpdate(Vec<SessionInfo>),
+ RunCommandResult(Option<i32>, Vec<u8>, Vec<u8>, BTreeMap<String, String>), // exit_code, STDOUT, STDERR,
+ // context
}
#[derive(
@@ -1064,4 +1066,13 @@ pub enum PluginCommand {
OpenTerminalInPlace(FileToOpen), // only used for the path as cwd
OpenFileInPlace(FileToOpen),
OpenCommandPaneInPlace(CommandToRun),
+ RunCommand(
+ Vec<String>,
+ BTreeMap<String, String>,
+ PathBuf,
+ BTreeMap<String, String>,
+ ), // command,
+ // env_Variables,
+ // cwd,
+ // context
}
diff --git a/zellij-utils/src/errors.rs b/zellij-utils/src/errors.rs
index 03fa6c81..cee98391 100644
--- a/zellij-utils/src/errors.rs
+++ b/zellij-utils/src/errors.rs
@@ -445,6 +445,7 @@ pub enum BackgroundJobContext {
ReadAllSessionInfosOnMachine,
ReportSessionInfo,
ReportLayoutInfo,
+ RunCommand,
Exit,
}
diff --git a/zellij-utils/src/plugin_api/event.proto b/zellij-utils/src/plugin_api/event.proto
index 4d5b805d..1463d8f0 100644
--- a/zellij-utils/src/plugin_api/event.proto
+++ b/zellij-utils/src/plugin_api/event.proto
@@ -40,6 +40,7 @@ enum EventType {
FileSystemDelete = 14;
PermissionRequestResult = 15;
SessionUpdate = 16;
+ RunCommandResult = 17;
}
message EventNameList {
@@ -61,6 +62,7 @@ message Event {
FileListPayload file_list_payload = 11;
PermissionRequestResultPayload permission_request_result_payload = 12;
SessionUpdatePayload session_update_payload = 13;
+ RunCommandResultPayload run_command_result_payload = 14;
}
}
@@ -68,6 +70,18 @@ message SessionUpdatePayload {
repeated SessionManifest session_manifests = 1;
}
+message RunCommandResultPayload {
+ optional int32 exit_code = 1;
+ bytes stdout = 2;
+ bytes stderr = 3;
+ repeated ContextItem context = 4;
+}
+
+message ContextItem {
+ string name = 1;
+ string value = 2;
+}
+
message PermissionRequestResultPayload {
bool granted = 1;
}
diff --git a/zellij-utils/src/plugin_api/event.rs b/zellij-utils/src/plugin_api/event.rs
index 9b0e9886..6547f383 100644
--- a/zellij-utils/src/plugin_api/event.rs
+++ b/zellij-utils/src/plugin_api/event.rs
@@ -183,6 +183,21 @@ impl TryFrom<ProtobufEvent> for Event {
},
_ => Err("Malformed payload for the SessionUpdate Event"),
},
+ Some(ProtobufEventType::RunCommandResult) => match protobuf_event.payload {
+ Some(ProtobufEventPayload::RunCommandResultPayload(run_command_result_payload)) => {
+ Ok(Event::RunCommandResult(
+ run_command_result_payload.exit_code,
+ run_command_result_payload.stdout,
+ run_command_result_payload.stderr,
+ run_command_result_payload
+ .context
+ .into_iter()
+ .map(|c_i| (c_i.name, c_i.value))
+ .collect(),
+ ))
+ },
+ _ => Err("Malformed payload for the RunCommandResult Event"),
+ },
None => Err("Unknown Protobuf Event"),
}
}
@@ -338,6 +353,23 @@ impl TryFrom<Event> for ProtobufEvent {
payload: Some(event::Payload::SessionUpdatePayload(session_update_payload)),
})
},
+ Event::RunCommandResult(exit_code, stdout, stderr, context) => {
+ let run_command_result_payload = RunCommandResultPayload {
+ exit_code,
+ stdout,
+ stderr,
+ context: context
+ .into_iter()
+ .map(|(name, value)| ContextItem { name, value })
+ .collect(),
+ };
+ Ok(ProtobufEvent {
+ name: ProtobufEventType::RunCommandResult as i32,
+ payload: Some(event::Payload::RunCommandResultPayload(
+ run_command_result_payload,
+ )),
+ })
+ },
}
}
}
@@ -783,6 +815,7 @@ impl TryFrom<ProtobufEventType> for EventType {
ProtobufEventType::FileSystemDelete => EventType::FileSystemDelete,
ProtobufEventType::PermissionRequestResult => EventType::PermissionRequestResult,
ProtobufEventType::SessionUpdate => EventType::SessionUpdate,
+ ProtobufEventType::RunCommandResult => EventType::RunCommandResult,
})
}
}
@@ -808,6 +841,7 @@ impl TryFrom<EventType> for ProtobufEventType {
EventType::FileSystemDelete => ProtobufEventType::FileSystemDelete,
EventType::PermissionRequestResult => ProtobufEventType::PermissionRequestResult,
EventType::SessionUpdate => ProtobufEventType::SessionUpdate,
+ EventType::RunCommandResult => ProtobufEventType::RunCommandResult,
})
}
}
diff --git a/zellij-utils/src/plugin_api/plugin_command.proto b/zellij-utils/src/plugin_api/plugin_command.proto
index 17b15381..e0b26184 100644
--- a/zellij-utils/src/plugin_api/plugin_command.proto
+++ b/zellij-utils/src/plugin_api/plugin_command.proto
@@ -82,6 +82,7 @@ enum CommandName {
OpenTerminalInPlace = 68;
OpenCommandInPlace = 69;
OpenFileInPlace = 70;
+ RunCommand = 71;
}
message PluginCommand {
@@ -128,6 +129,7 @@ message PluginCommand {
OpenFilePayload open_file_in_place_payload = 40;
OpenFilePayload open_terminal_in_place_payload = 41;
OpenCommandPanePayload open_command_pane_in_place_payload = 42;
+ RunCommandPayload run_command_payload = 43;
}
}
@@ -170,6 +172,23 @@ message ExecCmdPayload {
repeated string command_line = 1;
}
+message RunCommandPayload {
+ repeated string command_line = 1;
+ repeated EnvVariable env_variables = 2;
+ string cwd = 3;
+ repeated ContextItem context = 4;
+}
+
+message EnvVariable {
+ string name = 1;
+ string value = 2;
+}
+
+message ContextItem {
+ string name = 1;
+ string value = 2;
+}
+
message PluginMessagePayload {
api.message.Message message = 1;
}
diff --git a/zellij-utils/src/plugin_api/plugin_command.rs b/zellij-utils/src/plugin_api/plugin_command.rs
index 3d34f10f..181953d4 100644
--- a/zellij-utils/src/plugin_api/plugin_command.rs
+++ b/zellij-utils/src/plugin_api/plugin_command.rs
@@ -3,9 +3,10 @@ pub use super::generated_api::api::{
event::EventNameList as ProtobufEventNameList,
input_mode::InputMode as ProtobufInputMode,
plugin_command::{
- plugin_command::Payload, CommandName, ExecCmdPayload, IdAndNewName, MovePayload,
- OpenCommandPanePayload, OpenFilePayload, PluginCommand as ProtobufPluginCommand,
- PluginMessagePayload, RequestPluginPermissionPayload, ResizePayload, SetTimeoutPayload,
+ plugin_command::Payload, CommandName, ContextItem, EnvVariable, ExecCmdPayload,
+ IdAndNewName, MovePayload, OpenCommandPanePayload, OpenFilePayload,
+ PluginCommand as ProtobufPluginCommand, PluginMessagePayload,
+ RequestPluginPermissionPayload, ResizePayload, RunCommandPayload, SetTimeoutPayload,
SubscribePayload, SwitchSessionPayload, SwitchTabToPayload, UnsubscribePayload,
},
plugin_permission::PermissionType as ProtobufPermissionType,
@@ -14,7 +15,9 @@ pub use super::generated_api::api::{
use crate::data::{ConnectToSession, PermissionType, PluginCommand};
+use std::collections::BTreeMap;
use std::convert::TryFrom;
+use std::path::PathBuf;
impl TryFrom<ProtobufPluginCommand> for PluginCommand {
type Error = &'static str;
@@ -553,6 +556,27 @@ impl TryFrom<ProtobufPluginCommand> for PluginCommand {
},
_ => Err("Mismatched payload for OpenCommandPaneInPlace"),
},
+ Some(CommandName::RunCommand) => match protobuf_plugin_command.payload {
+ Some(Payload::RunCommandPayload(run_command_payload)) => {
+ let env_variables: BTreeMap<String, String> = run_command_payload
+ .env_variables
+ .into_iter()
+ .map(|e| (e.name, e.value))
+ .collect();
+ let context: BTreeMap<String, String> = run_command_payload
+ .context
+ .into_iter()
+ .map(|e| (e.name, e.value))
+ .collect();
+ Ok(PluginCommand::RunCommand(
+ run_command_payload.command_line,
+ env_variables,
+ PathBuf::from(run_command_payload.cwd),
+ context,
+ ))
+ },
+ _ => Err("Mismatched payload for RunCommand"),
+ },
None => Err("Unrecognized plugin command"),
}
}
@@ -928,6 +952,26 @@ impl TryFrom<PluginCommand> for ProtobufPluginCommand {
},
)),
}),
+ PluginCommand::RunCommand(command_line, env_variables, cwd, context) => {
+ let env_variables: Vec<_> = env_variables
+ .into_iter()
+ .map(|(name, value)| EnvVariable { name, value })
+ .collect();
+ let context: Vec<_> = context
+ .into_iter()
+ .map(|(name, value)| ContextItem { name, value })
+ .collect();
+ let cwd = cwd.display().to_string();
+ Ok(ProtobufPluginCommand {
+ name: CommandName::RunCommand as i32,
+ payload: Some(Payload::RunCommandPayload(RunCommandPayload {
+ command_line,
+ env_variables,
+ cwd,
+ context,
+ })),
+ })
+ },
}
}
}