2020-03-19 08:45:57 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2020 Balena Ltd.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Command from '@oclif/command';
|
2020-03-24 08:51:19 +00:00
|
|
|
import { InsufficientPrivilegesError } from './errors';
|
2020-03-19 08:45:57 +00:00
|
|
|
|
2020-03-19 16:48:32 +00:00
|
|
|
export default abstract class BalenaCommand extends Command {
|
2020-03-19 08:45:57 +00:00
|
|
|
/**
|
|
|
|
* When set to true, command will be listed in `help`,
|
|
|
|
* otherwise listed in `help --verbose` with secondary commands.
|
|
|
|
*/
|
|
|
|
public static primary = false;
|
2020-03-19 16:48:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2020-03-24 08:51:19 +00:00
|
|
|
/**
|
|
|
|
* Require authentication to run.
|
|
|
|
* When set to true, command will exit with an error
|
|
|
|
* if user is not already logged in.
|
|
|
|
*/
|
|
|
|
public static authenticated = false;
|
|
|
|
|
2020-03-25 17:07:17 +00:00
|
|
|
/**
|
|
|
|
* Accept piped input.
|
|
|
|
* When set to true, command will read from stdin during init
|
|
|
|
* and make contents available on member `stdin`.
|
|
|
|
*/
|
|
|
|
public static readStdin = false;
|
|
|
|
|
|
|
|
public stdin: string;
|
|
|
|
|
2020-03-24 08:51:19 +00:00
|
|
|
/**
|
|
|
|
* Throw InsufficientPrivilegesError if not root on Mac/Linux
|
|
|
|
* or non-Administrator on Windows.
|
|
|
|
*
|
|
|
|
* Called automatically if `root=true`.
|
|
|
|
* Can be called explicitly by command implementation, if e.g.:
|
|
|
|
* - check should only be done conditionally
|
|
|
|
* - other code needs to execute before check
|
|
|
|
*/
|
|
|
|
protected static async checkElevatedPrivileges() {
|
|
|
|
const isElevated = await (await import('is-elevated'))();
|
|
|
|
if (!isElevated) {
|
|
|
|
throw new InsufficientPrivilegesError(
|
|
|
|
'You need root/admin privileges to run this command',
|
|
|
|
);
|
2020-03-19 16:48:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 08:51:19 +00:00
|
|
|
/**
|
|
|
|
* Throw NotLoggedInError if not logged in.
|
|
|
|
*
|
|
|
|
* Called automatically if `authenticated=true`.
|
|
|
|
* Can be called explicitly by command implementation, if e.g.:
|
|
|
|
* - check should only be done conditionally
|
|
|
|
* - other code needs to execute before check
|
|
|
|
*
|
|
|
|
* Note, currently public to allow use outside of derived commands
|
|
|
|
* (as some command implementations require this. Can be made protected
|
|
|
|
* if this changes).
|
|
|
|
*/
|
|
|
|
public static async checkLoggedIn() {
|
|
|
|
await (await import('./utils/patterns')).checkLoggedIn();
|
|
|
|
}
|
|
|
|
|
2020-03-25 17:07:17 +00:00
|
|
|
/**
|
|
|
|
* Read stdin contents and make available to command.
|
|
|
|
*
|
|
|
|
* This approach could be improved in the future to automatically set argument
|
|
|
|
* values from stdin based in configuration, minimising command implementation.
|
|
|
|
*/
|
|
|
|
protected async getStdin() {
|
|
|
|
this.stdin = await (await import('get-stdin'))();
|
|
|
|
}
|
|
|
|
|
2020-03-19 16:48:32 +00:00
|
|
|
protected async init() {
|
2020-03-25 17:07:17 +00:00
|
|
|
const ctr = this.constructor as typeof BalenaCommand;
|
2020-03-24 08:51:19 +00:00
|
|
|
|
2020-03-25 17:07:17 +00:00
|
|
|
if (ctr.root) {
|
2020-03-24 08:51:19 +00:00
|
|
|
await BalenaCommand.checkElevatedPrivileges();
|
|
|
|
}
|
|
|
|
|
2020-03-25 17:07:17 +00:00
|
|
|
if (ctr.authenticated) {
|
2020-03-24 08:51:19 +00:00
|
|
|
await BalenaCommand.checkLoggedIn();
|
|
|
|
}
|
2020-03-25 17:07:17 +00:00
|
|
|
|
|
|
|
if (ctr.readStdin) {
|
|
|
|
await this.getStdin();
|
|
|
|
}
|
2020-03-19 16:48:32 +00:00
|
|
|
}
|
2020-03-19 08:45:57 +00:00
|
|
|
}
|