Add support for root property on oclif commands

Change-type: patch
Signed-off-by: Scott Lowe <scott@balena.io>
This commit is contained in:
Scott Lowe 2020-03-19 17:48:32 +01:00
parent dabe81c31b
commit f24c4a036c

View File

@ -16,11 +16,36 @@
*/
import Command from '@oclif/command';
import { ExpectedError } from './errors';
export default abstract class extends Command {
export default abstract class BalenaCommand extends Command {
/**
* When set to true, command will be listed in `help`,
* otherwise listed in `help --verbose` with secondary commands.
*/
public static primary = false;
/**
* Require elevated privileges to run.
* When set to true, command will exit with an error
* if executed without root on Mac/Linux
* or if executed by non-Administrator on Windows.
*/
public static root = false;
protected async checkElevatedPrivileges() {
const root = (this.constructor as typeof BalenaCommand).root;
if (root) {
const isElevated = await (await import('is-elevated'))();
if (!isElevated) {
throw new ExpectedError(
'You need admin privileges to run this command',
);
}
}
}
protected async init() {
await this.checkElevatedPrivileges();
}
}