Skip to main content

Mountain/RPC/CocoonService/Terminal/
OpenTerminal.rs

1
2//! Spawn a new PTY via `TerminalProvider::CreateTerminal`. Builds the
3//! options JSON `TerminalStateDTO::Create` expects (name + shellPath +
4//! shellArgs + cwd) and forwards through.
5
6use serde_json::json;
7use tonic::{Response, Status};
8use CommonLibrary::Terminal::TerminalProvider::TerminalProvider;
9
10use crate::{
11	RPC::CocoonService::CocoonServiceImpl,
12	Vine::Generated::{Empty, OpenTerminalRequest},
13	dev_log,
14};
15
16pub async fn Fn(Service:&CocoonServiceImpl, Request:OpenTerminalRequest) -> Result<Response<Empty>, Status> {
17	dev_log!("cocoon", "[CocoonService] Opening terminal: {}", Request.name);
18
19	let Options = json!({
20		"name": Request.name,
21		"shellPath": if Request.shell_path.is_empty() { serde_json::Value::Null } else { json!(Request.shell_path) },
22		"shellArgs": Request.shell_args,
23		"cwd": if Request.cwd.is_empty() { serde_json::Value::Null } else { json!(Request.cwd) },
24	});
25
26	match Service.environment.CreateTerminal(Options).await {
27		Ok(Info) => {
28			dev_log!("cocoon", "[CocoonService] Terminal created: {:?}", Info);
29
30			Ok(Response::new(Empty {}))
31		},
32
33		Err(Error) => {
34			dev_log!("cocoon", "error: [CocoonService] open_terminal failed: {}", Error);
35
36			Err(Status::internal(format!("open_terminal: {}", Error)))
37		},
38	}
39}