Skip to content

Commit 6be0434

Browse files
authored
Use docker compose command if present (intersystems-community#1057)
1 parent 4cada5b commit 6be0434

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

src/commands/serverActions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ export async function serverActions(): Promise<void> {
104104
actions.push({
105105
id: "openDockerTerminal",
106106
label: "Open Terminal in Docker",
107-
detail: "Use docker-compose to start session inside configured service",
107+
detail: "Use Docker Compose to start session inside configured service",
108108
});
109109
}
110110
if (workspaceState.get(workspaceFolder.toLowerCase() + ":docker", false)) {
111111
actions.push({
112112
id: "openDockerShell",
113113
label: "Open Shell in Docker",
114-
detail: "Use docker-compose to start shell inside configured service",
114+
detail: "Use Docker Compose to start shell inside configured service",
115115
});
116116
}
117117
actions.push({

src/utils/index.ts

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,20 @@ export function notNull(el: any): boolean {
397397
return el !== null;
398398
}
399399

400+
/** Determine the compose command to use (`docker-compose` or `docker compose`). */
401+
async function composeCommand(cwd?: string): Promise<string> {
402+
return new Promise<string>((resolve) => {
403+
let cmd = "docker compose";
404+
exec(`${cmd} version`, { cwd }, (error) => {
405+
if (error) {
406+
// 'docker compose' is not present, so default to 'docker-compose'
407+
cmd = "docker-compose";
408+
}
409+
resolve(cmd);
410+
});
411+
});
412+
}
413+
400414
export async function portFromDockerCompose(): Promise<{ port: number; docker: boolean; service?: string }> {
401415
// When running remotely, behave as if there is no docker-compose object within objectscript.conn
402416
if (extensionContext.extension.extensionKind === vscode.ExtensionKind.Workspace) {
@@ -432,8 +446,7 @@ export async function portFromDockerCompose(): Promise<{ port: number; docker: b
432446
}
433447

434448
const envFileParam = envFile ? `--env-file ${envFile}` : "";
435-
const exe = process.platform === "win32" ? "docker-compose.exe" : "docker-compose";
436-
const cmd = `${exe} -f ${file} ${envFileParam} `;
449+
const cmd = `${await composeCommand(cwd)} -f ${file} ${envFileParam} `;
437450

438451
return new Promise((resolve, reject) => {
439452
exec(`${cmd} ps --services --filter status=running`, { cwd }, (error, stdout) => {
@@ -466,17 +479,27 @@ export async function terminalWithDocker(): Promise<vscode.Terminal> {
466479
const terminalName = `ObjectScript:${workspace}`;
467480
let terminal = terminals.find((t) => t.name == terminalName && t.exitStatus == undefined);
468481
if (!terminal) {
469-
const exe = process.platform === "win32" ? "docker-compose.exe" : "docker-compose";
470-
terminal = vscode.window.createTerminal(terminalName, exe, [
471-
"-f",
472-
file,
473-
"exec",
474-
service,
475-
"/bin/bash",
476-
"-c",
477-
`[ -f /tmp/vscodesession.pid ] && kill $(cat /tmp/vscodesession.pid) >/dev/null 2>&1 ; echo $$ > /tmp/vscodesession.pid;
482+
let exe = await composeCommand();
483+
const argsArr: string[] = [];
484+
if (exe == "docker compose") {
485+
const exeSplit = exe.split(" ");
486+
exe = exeSplit[0];
487+
argsArr.push(exeSplit[1]);
488+
}
489+
terminal = vscode.window.createTerminal(
490+
terminalName,
491+
exe,
492+
argsArr.concat([
493+
"-f",
494+
file,
495+
"exec",
496+
service,
497+
"/bin/bash",
498+
"-c",
499+
`[ -f /tmp/vscodesession.pid ] && kill $(cat /tmp/vscodesession.pid) >/dev/null 2>&1 ; echo $$ > /tmp/vscodesession.pid;
478500
$(command -v ccontrol || command -v iris) session $ISC_PACKAGE_INSTANCENAME -U ${ns}`,
479-
]);
501+
])
502+
);
480503
terminals.push(terminal);
481504
}
482505
terminal.show(true);
@@ -491,8 +514,18 @@ export async function shellWithDocker(): Promise<vscode.Terminal> {
491514
const terminalName = `Shell:${workspace}`;
492515
let terminal = terminals.find((t) => t.name == terminalName && t.exitStatus == undefined);
493516
if (!terminal) {
494-
const exe = process.platform === "win32" ? "docker-compose.exe" : "docker-compose";
495-
terminal = vscode.window.createTerminal(terminalName, exe, ["-f", file, "exec", service, "/bin/bash"]);
517+
let exe = await composeCommand();
518+
const argsArr: string[] = [];
519+
if (exe == "docker compose") {
520+
const exeSplit = exe.split(" ");
521+
exe = exeSplit[0];
522+
argsArr.push(exeSplit[1]);
523+
}
524+
terminal = vscode.window.createTerminal(
525+
terminalName,
526+
exe,
527+
argsArr.concat(["-f", file, "exec", service, "/bin/bash"])
528+
);
496529
terminals.push(terminal);
497530
}
498531
terminal.show(true);

0 commit comments

Comments
 (0)