Mountain/IPC/WindServiceHandlers/Terminal/
AttachToProcess.rs1
2use std::sync::Arc;
15
16use CommonLibrary::{Environment::Requires::Requires, Terminal::TerminalProvider::TerminalProvider};
17use serde_json::{Value, json};
18
19use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, dev_log};
20
21pub async fn Fn(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
22 let TerminalId = match Arguments.first() {
23 Some(Value::Number(N)) => N.as_u64().unwrap_or(0),
24
25 Some(Value::Object(Obj)) => Obj.get("id").and_then(Value::as_u64).unwrap_or(0),
26
27 _ => 0,
28 };
29
30 if TerminalId == 0 {
31 dev_log!("terminal", "warn: [AttachToProcess] called with id=0, ignoring");
32
33 return Ok(Value::Null);
34 }
35
36 let Provider:Arc<dyn TerminalProvider> = RunTime.Environment.Require();
37
38 match Provider.GetTerminalProcessId(TerminalId).await {
41 Ok(Some(Pid)) => {
42 dev_log!("terminal", "[AttachToProcess] attached id={} pid={}", TerminalId, Pid);
43
44 Ok(json!({ "id": TerminalId, "pid": Pid }))
45 },
46
47 Ok(None) => {
48 dev_log!(
49 "terminal",
50 "warn: [AttachToProcess] id={} not found in active terminals",
51 TerminalId
52 );
53
54 Ok(Value::Null)
55 },
56
57 Err(Error) => {
58 dev_log!("terminal", "warn: [AttachToProcess] id={} error: {}", TerminalId, Error);
59
60 Ok(Value::Null)
61 },
62 }
63}