mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-02-20 09:26:42 +00:00
Merge pull request #1674 from balena-io/add-oclif-root-support
Add support for `root` property on oclif commands
This commit is contained in:
commit
d138c40ebd
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user