Skip to content

Commit bb9b694

Browse files
author
Sander Ronde
committed
Pass docker.environment setting contents to docker (fixes #116)
1 parent 080b73e commit bb9b694

File tree

7 files changed

+97
-59
lines changed

7 files changed

+97
-59
lines changed

client/src/lib/editorConfig.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
// eslint-disable-next-line node/no-extraneous-import
22
import type { TypedWorkspaceConfiguration } from 'vscode-generate-package-json/dist/types/src/configuration';
33

4+
import type {
5+
ConfigSettings,
6+
ExternalConfigSettings,
7+
} from '../../../shared/config';
48
import type { LanguageClient } from 'vscode-languageclient/node';
5-
import type { ConfigSettings } from '../../../shared/config';
69
import { watcherNotification } from './notificationChannels';
710
import { config } from '../../../shared/commands/defs';
811
import type { ExtensionContext } from 'vscode';
912
import { window, workspace } from 'vscode';
1013
import { CLIENT_PREFIX, log } from './log';
1114

12-
export function getEditorConfiguration(): TypedWorkspaceConfiguration<ConfigSettings> {
15+
export function getEditorConfiguration(): TypedWorkspaceConfiguration<
16+
ConfigSettings & ExternalConfigSettings
17+
> {
1318
const document = window.activeTextEditor?.document;
1419

1520
if (document) {

client/src/lib/setup.ts

+23-25
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
assertUnreachable,
33
getConfigFile,
44
pathExists,
5-
execute,
5+
docker,
66
getPathMapper,
77
} from '../../../shared/util';
88
import type { WatcherNotificationFileData } from '../../../shared/notificationChannels';
@@ -545,36 +545,36 @@ class DockerSetupSteps extends SetupSteps {
545545
};
546546
private async _dockerPathExists(path: string): Promise<boolean> {
547547
return (
548-
await execute('docker', [
549-
'exec',
550-
this._state.dockerContainerName,
551-
'sh',
552-
'-c',
553-
`[ -f ${path} ]`,
554-
])
548+
await docker(
549+
[
550+
'exec',
551+
this._state.dockerContainerName,
552+
'sh',
553+
'-c',
554+
`[ -f ${path} ]`,
555+
],
556+
getEditorConfiguration().get('docker.environment')
557+
)
555558
).success;
556559
}
557560

558561
private async _getDockerCwd(): Promise<string> {
559562
return (
560-
await execute('docker', [
561-
'exec',
562-
this._state.dockerContainerName,
563-
'pwd',
564-
])
563+
await docker(
564+
['exec', this._state.dockerContainerName, 'pwd'],
565+
getEditorConfiguration().get('docker.environment')
566+
)
565567
).stdout.trim();
566568
}
567569

568570
private async _dockerContainerNameStep(
569571
input: MultiStepInput,
570572
next: InputStep
571573
): Promise<InputStep> {
572-
const { stdout, success } = await execute('docker', [
573-
'ps',
574-
'-a',
575-
'--format',
576-
'{{json .Names}}',
577-
]);
574+
const { stdout, success } = await docker(
575+
['ps', '-a', '--format', '{{json .Names}}'],
576+
getEditorConfiguration().get('docker.environment')
577+
);
578578
const dockerContainers = success
579579
? stdout
580580
.trim()
@@ -602,12 +602,10 @@ class DockerSetupSteps extends SetupSteps {
602602
typeof choice === 'string' ? choice : choice.label;
603603

604604
const containerExists = (
605-
await execute('docker', [
606-
'exec',
607-
this._state.dockerContainerName,
608-
'echo',
609-
'1',
610-
])
605+
await docker(
606+
['exec', this._state.dockerContainerName, 'echo', '1'],
607+
getEditorConfiguration().get('docker.environment')
608+
)
611609
).success;
612610

613611
if (containerExists) {

server/src/lib/checkConfigManager.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { execute, getConfigFile, getPathMapper } from '../../../shared/util';
2-
import { getEditorConfiguration } from './editorConfig';
1+
import { getDockerEnvironment, getEditorConfiguration } from './editorConfig';
2+
import { docker, getConfigFile, getPathMapper } from '../../../shared/util';
33
import { showErrorOnce } from './errorUtil';
44
import type { ClassConfig } from './types';
55
import * as fs from 'fs/promises';
@@ -47,13 +47,16 @@ export class ConfigurationManager {
4747
.dockerContainerName;
4848
if (dockerContainerName) {
4949
const exists = (
50-
await execute('docker', [
51-
'exec',
52-
dockerContainerName,
53-
'sh',
54-
'-c',
55-
`[ -${isDir ? 'd' : 'f'} ${filePath} ]`,
56-
])
50+
await docker(
51+
[
52+
'exec',
53+
dockerContainerName,
54+
'sh',
55+
'-c',
56+
`[ -${isDir ? 'd' : 'f'} ${filePath} ]`,
57+
],
58+
await getDockerEnvironment(classConfig)
59+
)
5760
).success;
5861
return exists ? filePath : null;
5962
}

server/src/lib/editorConfig.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import type {
2+
ConfigSettingsWithoutPrefix,
3+
DockerConfigSettings,
4+
} from '../../../shared/config';
15
import { replaceHomeDir, replaceVariables } from '../../../shared/variables';
2-
import type { ConfigSettingsWithoutPrefix } from '../../../shared/config';
36
import { fromEntries } from '../../../shared/util';
47
import type { ClassConfig } from './types';
58

@@ -62,3 +65,17 @@ export async function getEditorConfiguration(
6265
false,
6366
};
6467
}
68+
69+
export async function getDockerEnvironment(
70+
classConfig: Pick<ClassConfig, 'connection' | 'workspaceFolders'>
71+
): Promise<Record<string, string> | null> {
72+
const workspaceFolders = await classConfig.workspaceFolders.get();
73+
const scope = workspaceFolders?.default.toString();
74+
const editorConfig = {
75+
...((await classConfig.connection.workspace.getConfiguration({
76+
scopeUri: scope,
77+
section: 'docker',
78+
})) as DockerConfigSettings),
79+
};
80+
return editorConfig['docker.environment'];
81+
}

server/src/lib/process.ts

+24-19
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import type {
22
ChildProcess,
33
SpawnSyncOptionsWithStringEncoding,
44
} from 'child_process';
5+
import { getDockerEnvironment, getEditorConfiguration } from './editorConfig';
56
import { processNotification } from './notificationChannels';
67
import type { AsyncDisposable, ClassConfig } from './types';
7-
import { getEditorConfiguration } from './editorConfig';
8-
import { execute, wait } from '../../../shared/util';
8+
import { docker, wait } from '../../../shared/util';
99
import { debug, sanitizeFilePath } from './debug';
1010
import { exec, spawn } from 'child_process';
1111
import { default as psTree } from 'ps-tree';
@@ -85,15 +85,19 @@ export class Process implements AsyncDisposable {
8585

8686
private static async _getDockerChildPids(
8787
containerName: string,
88-
pid: number
88+
pid: number,
89+
classConfig: ClassConfig
8990
): Promise<number[]> {
90-
const result = await execute('docker', [
91-
'exec',
92-
containerName,
93-
'sh',
94-
'-c',
95-
"ls /proc | grep -E '^[0-9]+$' | xargs -I{} cat /proc/{}/stat",
96-
]);
91+
const result = await docker(
92+
[
93+
'exec',
94+
containerName,
95+
'sh',
96+
'-c',
97+
"ls /proc | grep -E '^[0-9]+$' | xargs -I{} cat /proc/{}/stat",
98+
],
99+
await getDockerEnvironment(classConfig)
100+
);
97101
if (!result.stdout) {
98102
return [];
99103
}
@@ -267,16 +271,17 @@ export class Process implements AsyncDisposable {
267271
.dockerContainerName;
268272
const pids = [
269273
pid,
270-
...(await Process._getDockerChildPids(containerName, pid)),
271-
];
272-
return pids.map((pid) =>
273-
execute('docker', [
274-
'exec',
274+
...(await Process._getDockerChildPids(
275275
containerName,
276-
'kill',
277-
'-9',
278-
pid.toString(),
279-
]).then(() => undefined)
276+
pid,
277+
this._classConfig
278+
)),
279+
];
280+
return pids.map(async (pid) =>
281+
docker(
282+
['exec', containerName, 'kill', '-9', pid.toString()],
283+
await getDockerEnvironment(this._classConfig)
284+
).then(() => undefined)
280285
);
281286
}
282287

shared/config.ts

+6
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ export type ConfigSettings = Omit<
1616
/** @deprecated */
1717
'phpstan.enableLanguageServer'?: boolean;
1818
};
19+
20+
export type DockerConfigSettings = {
21+
'docker.environment': Record<string, string>;
22+
};
23+
24+
export type ExternalConfigSettings = DockerConfigSettings;

shared/util.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ function getAbsolutePath(filePath: string | null, cwd?: string): string | null {
175175
return path.join(cwd, filePath);
176176
}
177177

178-
export async function execute(
179-
binary: string,
178+
export async function docker(
180179
args: ReadonlyArray<string>,
180+
dockerEnv: Record<string, string> | null,
181181
options: Omit<
182182
SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe>,
183183
'stdio'
@@ -189,8 +189,12 @@ export async function execute(
189189
stderr: string;
190190
err: Error | null;
191191
}> {
192-
const proc = spawn(binary, args, {
192+
const proc = spawn('docker', args, {
193193
...options,
194+
env: {
195+
...process.env,
196+
...dockerEnv,
197+
},
194198
stdio: ['ignore', 'pipe', 'pipe'],
195199
});
196200
let stdout = '';

0 commit comments

Comments
 (0)