Skip to content

Commit e276370

Browse files
committed
The exec command now supports a -u option to override the run-as user
1 parent 80e4334 commit e276370

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/lib/controller/DevController.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export default class DevController {
5353
public execute(command: string, args: string[] = []): Promise<void> {
5454
try {
5555
let container = this.devSpec.defaultServiceName;
56+
let runAsUser = this.devSpec.command_defaults.user;
5657
switch (command) {
5758
case 'help':
5859
case 'commands':
@@ -81,14 +82,20 @@ export default class DevController {
8182
case 'sync':
8283
return this.sync();
8384
case 'exec':
84-
if (args.length >= 2 && args[0] == '-c') {
85-
container = args[1];
86-
args = args.slice(2);
85+
for (let i = 0; i < 2; i++) {
86+
if (args.length >= 2 && args[0] == '-c') {
87+
container = args[1];
88+
args = args.slice(2);
89+
}
90+
if (args.length >= 2 && args[0] == '-u') {
91+
runAsUser = args[1];
92+
args = args.slice(2);
93+
}
8794
}
8895
if (!args.length) {
89-
return Promise.reject('Syntax: exec [-c service_name] <program> [args...]');
96+
return Promise.reject('Syntax: exec [-c service_name] [-u user] <program> [args...]');
9097
}
91-
return this.exec(args[0], args.slice(1), container);
98+
return this.exec(args[0], args.slice(1), container, runAsUser);
9299
case 'logs':
93100
if (args.length >= 2 && args[0] == '-c') {
94101
container = args[1];
@@ -159,12 +166,12 @@ export default class DevController {
159166
await this.runCustomActions('sync');
160167
}
161168

162-
public async exec(command: string, args: string[]=[], service?: string) {
169+
public async exec(command: string, args: string[]=[], service?: string, runAsUser?: string) {
163170
service = service ?? this.devSpec.defaultServiceName;
164171
if (!(typeof service === 'string') || !service.length) {
165172
throw 'Unable to determine which container to use. Please specify a service name using -c';
166173
}
167-
await this.dockerComposeExec(service, command, args);
174+
await this.dockerComposeExec(service, command, args, runAsUser);
168175
}
169176

170177
public async logs(service?: string) {

src/main.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ const options = yargs(hideBin(process.argv))
7777
desc: 'Optionally specify the service name of the container to use, to override the default',
7878
type: 'string',
7979
requiresArg: true,
80+
}).option('u', {
81+
desc: 'Optionally specify the name or UID of the user to run as, to override the default',
82+
type: 'string',
83+
requiresArg: true,
8084
}).example('$0 exec -c app -- bash -c set', 'In order to avoid command arguments being interpreted as dev arguments, you can optionally use a -- separator, as shown, before the command line to execute.')
8185
)
8286
.command('logs', 'Print the recent log output from a container that is part of the devSpec. Then, watch and continue to print any new log messages that arrive.',
@@ -144,7 +148,10 @@ async function run() {
144148
if (Array.isArray(options['c'])) {
145149
throw 'You can only specify the -c option once.';
146150
}
147-
return controller.exec(options['program'] as string, args, options['c'] as string);
151+
if (Array.isArray(options['u'])) {
152+
throw 'You can only specify the -u option once.';
153+
}
154+
return controller.exec(options['program'] as string, args, options['c'] as string, options['u'] as string);
148155
case 'logs':
149156
if (Array.isArray(options['c'])) {
150157
throw 'You can only specify the -c option once.';

0 commit comments

Comments
 (0)