Skip to main content

Mountain/RPC/CocoonService/Terminal/
OpenTerminal.rs

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