fix(shell): move code to avoid repetetion

This commit is contained in:
Byson94
2025-10-13 19:14:58 +05:30
parent 8927256f39
commit f3b2f4bce7
3 changed files with 17 additions and 28 deletions

View File

@@ -20,7 +20,7 @@ use nix::{
};
use rhai::Map;
use shared_utils::extract_props::*;
use std::process::{Command as synced_command, Stdio};
use std::process::Stdio;
use tokio::io::AsyncBufReadExt;
use tokio::io::BufReader;
use tokio::process::Command;
@@ -30,6 +30,7 @@ use tokio::sync::watch;
pub fn handle_listen(
var_name: String,
props: &Map,
shell: String,
store: ReactiveVarStore,
tx: tokio::sync::mpsc::UnboundedSender<String>,
) {
@@ -55,18 +56,7 @@ pub fn handle_listen(
let (shutdown_tx, mut shutdown_rx) = watch::channel(false);
SHUTDOWN_REGISTRY.lock().unwrap().push(shutdown_tx.clone());
// Check Dash and prefer if dash is installed.
let dash_installed: bool = synced_command::new("which")
.arg("dash")
.output()
.map(|o| o.status.success())
.unwrap_or(false);
let shell: &str = if dash_installed { "/bin/dash" } else { "/bin/sh" };
// Task to catch SIGINT and SIGTERM
tokio::spawn({
let shutdown_tx = shutdown_tx.clone();
async move {
@@ -87,7 +77,7 @@ pub fn handle_listen(
tokio::spawn(async move {
let mut child = unsafe {
Command::new(shell)
Command::new(&shell)
.arg("-c")
.arg(&cmd)
// .kill_on_drop(true)

View File

@@ -25,6 +25,7 @@ use std::sync::Mutex;
use std::{collections::HashMap, sync::Arc, sync::RwLock};
use tokio::sync::mpsc::UnboundedSender;
use tokio::sync::watch;
use std::process::Command;
pub type ReactiveVarStore = Arc<RwLock<HashMap<String, String>>>;
pub static SHUTDOWN_REGISTRY: Lazy<Mutex<Vec<watch::Sender<bool>>>> =
@@ -35,14 +36,23 @@ pub fn handle_state_changes(
tx: UnboundedSender<String>,
store: ReactiveVarStore,
) {
// Check Dash and prefer if dash is installed.
let dash_installed: bool = Command::new("which")
.arg("dash")
.output()
.map(|o| o.status.success())
.unwrap_or(false);
let shell = if dash_installed { String::from("/bin/dash") } else { String::from("/bin/sh") };
if let WidgetNode::Enter(children) = root_node {
for child in children {
match child {
WidgetNode::Poll { var, props } => {
handle_poll(var.to_string(), props, store.clone(), tx.clone());
handle_poll(var.to_string(), props, shell.clone(), store.clone(), tx.clone());
}
WidgetNode::Listen { var, props } => {
handle_listen(var.to_string(), props, store.clone(), tx.clone());
handle_listen(var.to_string(), props, shell.clone(), store.clone(), tx.clone());
}
_ => {}
}

View File

@@ -23,11 +23,10 @@ use tokio::process::Command;
use tokio::sync::watch;
use tokio::time::sleep;
use std::process::Command as synced_command;
pub fn handle_poll(
var_name: String,
props: &Map,
shell: String,
store: ReactiveVarStore,
tx: tokio::sync::mpsc::UnboundedSender<String>,
) {
@@ -57,19 +56,9 @@ pub fn handle_poll(
let (shutdown_tx, mut shutdown_rx) = watch::channel(false);
SHUTDOWN_REGISTRY.lock().unwrap().push(shutdown_tx.clone());
// Check Dash and prefer if dash is installed.
let dash_installed: bool = synced_command::new("which")
.arg("dash")
.output()
.map(|o| o.status.success())
.unwrap_or(false);
let shell: &str = if dash_installed { "/bin/dash" } else { "/bin/sh" };
tokio::spawn(async move {
// Spawn a persistent shell
let mut child = match Command::new(shell)
let mut child = match Command::new(&shell)
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.spawn()