From 3852a7e6fa0f6f849c4fe87e580f9a3fd1274383 Mon Sep 17 00:00:00 2001
From: Roman Nikitenko <rnikiten@redhat.com>
Date: Fri, 12 Jul 2024 14:57:44 +0300
Subject: [PATCH] feat: Provide cwd for che terminal creation

Signed-off-by: Roman Nikitenko <rnikiten@redhat.com>

rh-pre-commit.version: 2.2.0
rh-pre-commit.check-secrets: ENABLED
---
 code/extensions/che-terminal/src/extension.ts | 20 ++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/code/extensions/che-terminal/src/extension.ts b/code/extensions/che-terminal/src/extension.ts
index e3ddce3c0aa..b7dc439a142 100644
--- a/code/extensions/che-terminal/src/extension.ts
+++ b/code/extensions/che-terminal/src/extension.ts
@@ -44,7 +44,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<Api> {
 
 		// containerName is undefined in case the user closed the QuickPick
 		if (containerName) {
-			const pty = new MachineExecPTY(machineExecClient, containerName);
+			const cwd = await getCwd();
+			const pty = new MachineExecPTY(machineExecClient, containerName, undefined, cwd);
 			const terminal = vscode.window.createTerminal({ name: `${containerName} container`, pty });
 			terminal.show();
 		}
@@ -127,3 +128,20 @@ export class MachineExecPTY implements vscode.Pseudoterminal {
 
 export function deactivate(): void {
 }
+
+async function getCwd(): Promise<string | undefined> {
+	const folders = vscode.workspace.workspaceFolders;
+	if (folders === undefined || folders.length < 1) {
+		return process.env.PROJECTS_ROOT as string;
+	}
+
+	if (folders.length === 1) {
+		return folders[0].uri.path
+	}
+
+	const options = {
+		placeHolder: "Select current working directory for new terminal"
+	};
+	const workspace: vscode.WorkspaceFolder = await vscode.commands.executeCommand('_workbench.pickWorkspaceFolder', [options]);
+	return workspace.uri.path;
+}