Remove custom override of oclif Command class in favor of prerun hook

Change-type: patch
This commit is contained in:
myarmolinsky 2024-10-02 07:55:21 -04:00
parent ebe10360b3
commit 9cdde4f6c2
88 changed files with 263 additions and 374 deletions

View File

@ -99,7 +99,7 @@
"helpClass": "./build/help",
"topicSeparator": " ",
"hooks": {
"prerun": "./build/hooks/prerun/track",
"prerun": "./build/hooks/prerun",
"command_not_found": "./build/hooks/command-not-found/suggest"
},
"additionalHelpFlags": [

View File

@ -153,7 +153,7 @@ async function oclifRun(command: string[], options: AppOptions) {
}
})(!options.noFlush);
const { trackPromise } = await import('./hooks/prerun/track');
const { trackPromise } = await import('./hooks/prerun');
await Promise.all([trackPromise, deprecationPromise, runPromise]);
}

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import { ExpectedError } from '../../errors';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags } from '@oclif/core';
import Command from '../../command';
import { Flags, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy';

View File

@ -15,9 +15,7 @@
* limitations under the License.
*/
import { Flags, Args } from '@oclif/core';
import Command from '../../command';
import { Flags, Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { stripIndent } from '../../utils/lazy';

View File

@ -15,9 +15,7 @@
* limitations under the License.
*/
import { Flags, Args } from '@oclif/core';
import Command from '../../command';
import { Flags, Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args, Flags } from '@oclif/core';
import Command from '../../command';
import { Args, Flags, Command } from '@oclif/core';
import { getBalenaSdk } from '../../utils/lazy';
import * as cf from '../../utils/common-flags';
import * as compose from '../../utils/compose';
@ -106,13 +105,16 @@ ${dockerignoreHelp}
public async run() {
const { args: params, flags: options } = await this.parse(BuildCmd);
await Command.checkLoggedInIf(!!options.fleet);
const Logger = await import('../../utils/logger');
const { checkLoggedInIf } = await import('../../utils/patterns');
await checkLoggedInIf(!!options.fleet);
(await import('events')).defaultMaxListeners = 1000;
const sdk = getBalenaSdk();
const logger = await Command.getLogger();
const logger = Logger.getLogger();
logger.logDebug('Parsing input...');
// `build` accepts `source` as a parameter, but compose expects it as an option

View File

@ -15,9 +15,8 @@
* limitations under the License.
*/
import { Flags } from '@oclif/core';
import { Flags, Command } from '@oclif/core';
import type { Interfaces } from '@oclif/core';
import Command from '../../command';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, getCliForm, stripIndent } from '../../utils/lazy';
import {

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getVisuals, stripIndent } from '../../utils/lazy';

View File

@ -15,7 +15,7 @@
* limitations under the License.
*/
import Command from '../../command';
import { Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getVisuals, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags } from '@oclif/core';
import Command from '../../command';
import { Flags, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getVisuals, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getVisuals, stripIndent } from '../../utils/lazy';

View File

@ -15,10 +15,8 @@
* limitations under the License.
*/
import { Args, Flags } from '@oclif/core';
import { Args, Flags, Command } from '@oclif/core';
import type { ImageDescriptor } from '@balena/compose/dist/parse';
import Command from '../../command';
import { ExpectedError } from '../../errors';
import { getBalenaSdk, getChalk, stripIndent } from '../../utils/lazy';
import {
@ -155,7 +153,9 @@ ${dockerignoreHelp}
(await import('events')).defaultMaxListeners = 1000;
const logger = await Command.getLogger();
const Logger = await import('../../utils/logger');
const logger = Logger.getLogger();
logger.logDebug('Parsing input...');
const { fleet, image } = params;

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
import { ExpectedError } from '../../errors';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags, Args } from '@oclif/core';
import Command from '../../command';
import { Flags, Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { expandForAppName } from '../../utils/helpers';
import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags } from '@oclif/core';
import Command from '../../command';
import { Flags, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
import { applicationIdInfo } from '../../utils/messages';
@ -117,8 +116,9 @@ export default class DeviceInitCmd extends Command {
tmp.setGracefulCleanup();
const { downloadOSImage } = await import('../../utils/cloud');
const { getApplication } = await import('../../utils/sdk');
const Logger = await import('../../utils/logger');
const logger = await Command.getLogger();
const logger = Logger.getLogger();
const balena = getBalenaSdk();
// Get application and

View File

@ -15,7 +15,7 @@
* limitations under the License.
*/
import Command from '../../command';
import { Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { expandForAppName } from '../../utils/helpers';
import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags, Args } from '@oclif/core';
import Command from '../../command';
import { Flags, Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';

View File

@ -15,14 +15,13 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import { Args, Command } from '@oclif/core';
import type {
BalenaSDK,
Device,
PineOptions,
PineTypedResult,
} from 'balena-sdk';
import Command from '../../command';
import * as cf from '../../utils/common-flags';
import { ExpectedError } from '../../errors';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags, Args } from '@oclif/core';
import Command from '../../command';
import { Flags, Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent, getCliForm } from '../../utils/lazy';
import type { Device } from 'balena-sdk';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
import { getExpandedProp } from '../../utils/pine';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags, Args } from '@oclif/core';
import Command from '../../command';
import { Flags, Args, Command } from '@oclif/core';
import { ExpectedError } from '../../errors';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, getCliUx, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags } from '@oclif/core';
import Command from '../../command';
import { Flags, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import * as ca from '../../utils/common-args';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent, getCliForm } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags, Args } from '@oclif/core';
import Command from '../../command';
import { Flags, Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, getCliUx, stripIndent } from '../../utils/lazy';
import type {

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
import { ExpectedError } from '../../errors';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, getCliUx, stripIndent } from '../../utils/lazy';
import type { BalenaSDK } from 'balena-sdk';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, getCliUx, stripIndent } from '../../utils/lazy';
import type { BalenaSDK } from 'balena-sdk';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';

View File

@ -14,11 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Flags } from '@oclif/core';
import { Flags, Command } from '@oclif/core';
import type * as BalenaSdk from 'balena-sdk';
import * as _ from 'lodash';
import Command from '../../command';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy';

View File

@ -15,9 +15,8 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import { Args, Command } from '@oclif/core';
import type * as BalenaSdk from 'balena-sdk';
import Command from '../../command';
import { ExpectedError } from '../../errors';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
@ -111,7 +110,9 @@ export default class EnvAddCmd extends Command {
);
}
await Command.checkLoggedIn();
const { checkLoggedIn } = await import('../../utils/patterns');
await checkLoggedIn();
if (params.value == null) {
params.value = process.env[params.name];

View File

@ -14,9 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import * as ec from '../../utils/env-common';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
@ -63,7 +61,9 @@ export default class EnvRenameCmd extends Command {
public async run() {
const { args: params, flags: opt } = await this.parse(EnvRenameCmd);
await Command.checkLoggedIn();
const { checkLoggedIn } = await import('../../utils/patterns');
await checkLoggedIn();
await getBalenaSdk().pine.patch({
resource: ec.getVarResourceName(opt.config, opt.device, opt.service),

View File

@ -15,9 +15,7 @@
* limitations under the License.
*/
import { Flags, Args } from '@oclif/core';
import Command from '../../command';
import { Flags, Args, Command } from '@oclif/core';
import * as ec from '../../utils/env-common';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
import { parseAsInteger } from '../../utils/validation';
@ -67,7 +65,9 @@ export default class EnvRmCmd extends Command {
public async run() {
const { args: params, flags: opt } = await this.parse(EnvRmCmd);
await Command.checkLoggedIn();
const { checkLoggedIn } = await import('../../utils/patterns');
await checkLoggedIn();
const { confirm } = await import('../../utils/patterns');
await confirm(

View File

@ -14,11 +14,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Flags } from '@oclif/core';
import { Flags, Command } from '@oclif/core';
import type { Interfaces } from '@oclif/core';
import type * as SDK from 'balena-sdk';
import * as _ from 'lodash';
import Command from '../../command';
import { ExpectedError } from '../../errors';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy';
@ -113,7 +112,9 @@ export default class EnvsCmd extends Command {
const variables: EnvironmentVariableInfo[] = [];
await Command.checkLoggedIn();
const { checkLoggedIn } = await import('../../utils/patterns');
await checkLoggedIn();
if (!options.fleet && !options.device) {
throw new ExpectedError('Missing --fleet or --device option');

View File

@ -15,9 +15,7 @@
* limitations under the License.
*/
import { Flags, Args } from '@oclif/core';
import Command from '../../command';
import { Flags, Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { stripIndent } from '../../utils/lazy';

View File

@ -15,9 +15,7 @@
* limitations under the License.
*/
import { Flags } from '@oclif/core';
import Command from '../../command';
import { Flags, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import * as ca from '../../utils/common-args';
import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
import { getExpandedProp } from '../../utils/pine';

View File

@ -15,7 +15,7 @@
* limitations under the License.
*/
import Command from '../../command';
import { Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import * as ca from '../../utils/common-args';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';

View File

@ -15,9 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import * as ca from '../../utils/common-args';
import { getBalenaSdk, stripIndent, getCliForm } from '../../utils/lazy';

View File

@ -15,7 +15,7 @@
* limitations under the License.
*/
import Command from '../../command';
import { Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import * as ca from '../../utils/common-args';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';

View File

@ -15,11 +15,11 @@
* limitations under the License.
*/
import Command from '../../command';
import * as cf from '../../utils/common-flags';
import * as ca from '../../utils/common-args';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
import { applicationIdInfo } from '../../utils/messages';
import { Command } from '@oclif/core';
export default class FleetRmCmd extends Command {
public static description = stripIndent`

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';

View File

@ -16,10 +16,9 @@
*/
import type * as BalenaSdk from 'balena-sdk';
import Command from '../../command';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy';
import { Command } from '@oclif/core';
interface ExtendedApplication extends ApplicationWithDeviceTypeSlug {
device_count: number;

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import { stripIndent } from '../../utils/lazy';
// 'Internal' commands are called during the execution of other commands.

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args, Flags } from '@oclif/core';
import Command from '../../command';
import { Args, Flags, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
import { applicationIdInfo } from '../../utils/messages';
@ -77,7 +76,8 @@ export default class JoinCmd extends Command {
const promote = await import('../../utils/promote');
const sdk = getBalenaSdk();
const logger = await Command.getLogger();
const Logger = await import('../../utils/logger');
const logger = Logger.getLogger();
return promote.join(
logger,
sdk,

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import { ExpectedError } from '../../errors';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
@ -67,8 +66,6 @@ export default class KeyAddCmd extends Command {
public static authenticated = true;
public static readStdin = true;
public async run() {
const { args: params } = await this.parse(KeyAddCmd);
@ -76,8 +73,6 @@ export default class KeyAddCmd extends Command {
if (params.path != null) {
const { readFile } = (await import('fs')).promises;
key = await readFile(params.path, 'utf8');
} else if (this.stdin.length > 0) {
key = this.stdin;
} else {
throw new ExpectedError('No public key file or path provided.');
}

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy';
import { parseAsInteger } from '../../utils/validation';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
import { parseAsInteger } from '../../utils/validation';

View File

@ -15,7 +15,7 @@
* limitations under the License.
*/
import Command from '../../command';
import { Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { stripIndent } from '../../utils/lazy';
import { parseAsLocalHostnameOrIp } from '../../utils/validation';
@ -61,7 +60,8 @@ export default class LeaveCmd extends Command {
const { args: params } = await this.parse(LeaveCmd);
const promote = await import('../../utils/promote');
const logger = await Command.getLogger();
const Logger = await import('../../utils/logger');
const logger = Logger.getLogger();
return promote.leave(logger, params.deviceIpOrHostname);
}
}

View File

@ -15,9 +15,8 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import { Args, Command } from '@oclif/core';
import { promisify } from 'util';
import Command from '../../command';
import * as cf from '../../utils/common-flags';
import { stripIndent } from '../../utils/lazy';

View File

@ -15,9 +15,8 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import { Args, Command } from '@oclif/core';
import type { BlockDevice } from 'etcher-sdk/build/source-destination';
import Command from '../../command';
import { ExpectedError } from '../../errors';
import * as cf from '../../utils/common-flags';
import { getChalk, getVisuals, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags, Args } from '@oclif/core';
import Command from '../../command';
import { Flags, Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent, getCliForm } from '../../utils/lazy';
import { ExpectedError } from '../../errors';

View File

@ -15,7 +15,7 @@
* limitations under the License.
*/
import Command from '../../command';
import { Command } from '@oclif/core';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
export default class LogoutCmd extends Command {

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags, Args } from '@oclif/core';
import Command from '../../command';
import { Flags, Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
import type { LogMessage } from 'balena-sdk';
@ -151,8 +150,9 @@ export default class LogsCmd extends Command {
maxAttempts: 1 + (options['max-retry'] ?? MAX_RETRY),
});
} else {
const { checkLoggedIn } = await import('../../utils/patterns');
// Logs from cloud
await Command.checkLoggedIn();
await checkLoggedIn();
if (options.tail) {
const logStream = await balena.logs.subscribe(params.device, {
count: 100,

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags, Args } from '@oclif/core';
import Command from '../../command';
import { Flags, Args, Command } from '@oclif/core';
import { ExpectedError } from '../../errors';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
@ -53,14 +52,10 @@ export default class NoteCmd extends Command {
public static authenticated = true;
public static readStdin = true;
public async run() {
const { args: params, flags: options } = await this.parse(NoteCmd);
params.note = params.note || this.stdin;
if (params.note.length === 0) {
if (params.note?.length === 0) {
throw new ExpectedError('Missing note content');
}
@ -73,6 +68,6 @@ export default class NoteCmd extends Command {
const balena = getBalenaSdk();
return balena.models.device.setNote(options.device, params.note);
return balena.models.device.setNote(options.device, params.note ?? '');
}
}

View File

@ -15,7 +15,7 @@
* limitations under the License.
*/
import Command from '../../command';
import { Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags, Args } from '@oclif/core';
import Command from '../../command';
import { Flags, Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getCliForm, stripIndent } from '../../utils/lazy';
import * as _ from 'lodash';

View File

@ -15,12 +15,11 @@
* limitations under the License.
*/
import { Flags, Args } from '@oclif/core';
import { Flags, Args, Command } from '@oclif/core';
import type { Interfaces } from '@oclif/core';
import type * as BalenaSdk from 'balena-sdk';
import { promisify } from 'util';
import * as _ from 'lodash';
import Command from '../../command';
import { ExpectedError } from '../../errors';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent, getCliForm } from '../../utils/lazy';
@ -319,7 +318,9 @@ async function validateOptions(options: FlagsDef) {
);
}
await Command.checkLoggedIn();
const { checkLoggedIn } = await import('../../utils/patterns');
await checkLoggedIn();
}
/**

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags, Args } from '@oclif/core';
import Command from '../../command';
import { Flags, Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { stripIndent } from '../../utils/lazy';
@ -90,7 +89,8 @@ export default class OsDownloadCmd extends Command {
const { isESR } = await import('../../utils/image-manager');
if (options.version === 'menu-esr' || isESR(options.version)) {
try {
await OsDownloadCmd.checkLoggedIn();
const { checkLoggedIn } = await import('../../utils/patterns');
await checkLoggedIn();
} catch (e) {
const { ExpectedError, NotLoggedInError } = await import(
'../../errors'

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getCliForm, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags, Args } from '@oclif/core';
import Command from '../../command';
import { Flags, Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { stripIndent } from '../../utils/lazy';

View File

@ -15,7 +15,6 @@
* limitations under the License.
*/
import Command from '../../command';
import { ExpectedError } from '../../errors';
import * as cf from '../../utils/common-flags';
import {
@ -27,8 +26,7 @@ import {
import { applicationIdInfo } from '../../utils/messages';
import { dockerConnectionCliFlags } from '../../utils/docker';
import { parseAsInteger } from '../../utils/validation';
import { Flags, Args } from '@oclif/core';
import { Flags, Args, Command } from '@oclif/core';
import * as _ from 'lodash';
import type {
Application,

View File

@ -15,9 +15,8 @@
* limitations under the License.
*/
import { Flags, Args } from '@oclif/core';
import { Flags, Args, Command } from '@oclif/core';
import type { Interfaces } from '@oclif/core';
import Command from '../../command';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
import { dockerignoreHelp, registrySecretsHelp } from '../../utils/messages';
@ -227,7 +226,9 @@ export default class PushCmd extends Command {
public async run() {
const { args: params, flags: options } = await this.parse(PushCmd);
const logger = await Command.getLogger();
const Logger = await import('../../utils/logger');
const logger = Logger.getLogger();
logger.logDebug(`Using build source directory: ${options.source} `);
const sdk = getBalenaSdk();
@ -294,7 +295,9 @@ export default class PushCmd extends Command {
options['release-tag'] ?? [],
);
await Command.checkLoggedIn();
const { checkLoggedIn } = await import('../../utils/patterns');
await checkLoggedIn();
const [token, baseUrl] = await Promise.all([
sdk.auth.getToken(),
sdk.settings.get('balenaUrl'),

View File

@ -15,8 +15,8 @@
* limitations under the License.
*/
import { Command } from '@oclif/core';
import { commitOrIdArg } from '.';
import Command from '../../command';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags, Args, type Interfaces } from '@oclif/core';
import Command from '../../command';
import { Flags, Args, type Interfaces, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy';
import type * as BalenaSdk from 'balena-sdk';

View File

@ -15,8 +15,8 @@
* limitations under the License.
*/
import { Command } from '@oclif/core';
import { commitOrIdArg } from '.';
import Command from '../../command';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,8 @@
* limitations under the License.
*/
import { Command } from '@oclif/core';
import { commitOrIdArg } from '.';
import Command from '../../command';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy';
import { applicationNameNote } from '../../utils/messages';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags } from '@oclif/core';
import Command from '../../command';
import { Flags, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getCliUx, stripIndent } from '../../utils/lazy';

View File

@ -15,7 +15,7 @@
* limitations under the License.
*/
import Command from '../../command';
import { Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags, Args } from '@oclif/core';
import Command from '../../command';
import { Flags, Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
import {
@ -121,15 +120,19 @@ export default class SshCmd extends Command {
// Remote connection
const { getProxyConfig } = await import('../../utils/helpers');
const { getOnlineTargetDeviceUuid } = await import('../../utils/patterns');
const {
getOnlineTargetDeviceUuid,
checkLoggedIn,
checkNotUsingOfflineMode,
} = await import('../../utils/patterns');
const sdk = getBalenaSdk();
const proxyConfig = getProxyConfig();
const useProxy = !!proxyConfig && !options.noproxy;
// this will be a tunnelled SSH connection...
await Command.checkNotUsingOfflineMode();
await Command.checkLoggedIn();
await checkNotUsingOfflineMode();
await checkLoggedIn();
const deviceUuid = await getOnlineTargetDeviceUuid(
sdk,
params.fleetOrDevice,

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags, Args } from '@oclif/core';
import Command from '../../command';
import { Flags, Args, Command } from '@oclif/core';
import { ExpectedError } from '../../errors';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, getCliUx, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
import { applicationIdInfo } from '../../utils/messages';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Args } from '@oclif/core';
import Command from '../../command';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
import { applicationIdInfo } from '../../utils/messages';

View File

@ -15,7 +15,7 @@
* limitations under the License.
*/
import Command from '../../command';
import { Command } from '@oclif/core';
import { ExpectedError } from '../../errors';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags, Args } from '@oclif/core';
import Command from '../../command';
import { Flags, Args, Command } from '@oclif/core';
import {
NoPortsDefinedError,
InvalidPortMappingError,
@ -94,7 +93,9 @@ export default class TunnelCmd extends Command {
public async run() {
const { args: params, flags: options } = await this.parse(TunnelCmd);
const logger = await Command.getLogger();
const Logger = await import('../../utils/logger');
const logger = Logger.getLogger();
const sdk = getBalenaSdk();
const logConnection = (

View File

@ -15,7 +15,7 @@
* limitations under the License.
*/
import Command from '../../command';
import { Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { stripIndent, getChalk, getVisuals } from '../../utils/lazy';

View File

@ -15,8 +15,7 @@
* limitations under the License.
*/
import { Flags } from '@oclif/core';
import Command from '../../command';
import { Flags, Command } from '@oclif/core';
import { stripIndent } from '../../utils/lazy';
export interface JsonVersions {

View File

@ -15,7 +15,7 @@
* limitations under the License.
*/
import Command from '../../command';
import { Command } from '@oclif/core';
import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy';
export default class WhoamiCmd extends Command {

View File

@ -15,50 +15,20 @@
* limitations under the License.
*/
import { Command } from '@oclif/core';
import {
InsufficientPrivilegesError,
NotAvailableInOfflineModeError,
} from './errors';
import { stripIndent } from './utils/lazy';
type Hook,
type Command,
// ux
} from '@oclif/core';
import { InsufficientPrivilegesError } from '../errors';
import { checkLoggedIn, checkNotUsingOfflineMode } from '../utils/patterns';
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;
let trackResolve: (result: Promise<any>) => void;
/**
* 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;
/**
* 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;
/**
* Require an internet connection to run.
* When set to true, command will exit with an error
* if user is running in offline mode (BALENARC_OFFLINE_MODE).
*/
public static offlineCompatible = false;
/**
* 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;
// note: trackPromise is subject to a Bluebird.timeout, defined in events.ts
export const trackPromise = new Promise((resolve) => {
trackResolve = resolve;
});
/**
* Throw InsufficientPrivilegesError if not root on Mac/Linux
@ -69,102 +39,81 @@ export default abstract class BalenaCommand extends Command {
* - check should only be done conditionally
* - other code needs to execute before check
*/
protected static async checkElevatedPrivileges() {
const checkElevatedPrivileges = async () => {
const isElevated = await (await import('is-elevated'))();
if (!isElevated) {
throw new InsufficientPrivilegesError(
'You need root/admin privileges to run this command',
);
}
}
};
/**
* 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).
*
* @throws {NotLoggedInError}
* 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 async checkLoggedIn() {
await (await import('./utils/patterns')).checkLoggedIn();
}
const DEFAULT_ROOT = false;
/**
* Throw NotLoggedInError if not logged in when condition true.
*
* @param {boolean} doCheck - will check if true.
* @throws {NotLoggedInError}
* Require authentication to run.
* When set to true, command will exit with an error
* if user is not already logged in.
*/
public static async checkLoggedInIf(doCheck: boolean) {
if (doCheck) {
await this.checkLoggedIn();
}
}
const DEFAULT_AUTHENTICATED = false;
/**
* Throw NotAvailableInOfflineModeError if in offline mode.
*
* Called automatically if `onlineOnly=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).
*
* @throws {NotAvailableInOfflineModeError}
* Require an internet connection to run.
* When set to true, command will exit with an error
* if user is running in offline mode (BALENARC_OFFLINE_MODE).
*/
public static checkNotUsingOfflineMode() {
if (process.env.BALENARC_OFFLINE_MODE) {
throw new NotAvailableInOfflineModeError(stripIndent`
This command requires an internet connection, and cannot be used in offline mode.
To leave offline mode, unset the BALENARC_OFFLINE_MODE environment variable.
`);
}
}
const DEFAULT_OFFLINE_COMPATIBLE = false;
/**
* Read stdin contents and make available to command.
* This is an oclif 'prerun' hook. This hook runs after the command line is
* parsed by oclif, but before the command's run() function is called.
* See: https://oclif.io/docs/hooks
*
* This approach could be improved in the future to automatically set argument
* values from stdin based in configuration, minimising command implementation.
* This hook is used to track CLI command signatures (usage analytics).
* A command signature is something like "env add NAME [VALUE]". That's
* literally so: 'NAME' and 'VALUE' are NOT replaced with actual values.
*/
protected async getStdin() {
this.stdin = await (await import('get-stdin'))();
const hook: Hook<'prerun'> = async function (options) {
try {
if (
(options.Command as Command.Class & { root: boolean }).root ??
DEFAULT_ROOT
) {
await checkElevatedPrivileges();
}
/**
* Get a logger instance.
*/
protected static async getLogger() {
return (await import('./utils/logger')).getLogger();
if (
(options.Command as Command.Class & { authenticated: boolean })
.authenticated ??
DEFAULT_AUTHENTICATED
) {
await checkLoggedIn();
}
protected async init() {
const ctr = this.constructor as typeof BalenaCommand;
if (
!(
(options.Command as Command.Class & { offlineCompatible: boolean })
.offlineCompatible ?? DEFAULT_OFFLINE_COMPATIBLE
)
) {
await checkNotUsingOfflineMode();
}
} catch (error) {
this.error(error);
}
const events = await import('../events');
const cmd = options.Command.id;
if (ctr.root) {
await BalenaCommand.checkElevatedPrivileges();
}
// Intentionally do not await for the track promise here, in order to
// run the command tracking and the command itself in parallel.
trackResolve(events.trackCommand(cmd));
};
if (ctr.authenticated) {
await BalenaCommand.checkLoggedIn();
}
if (!ctr.offlineCompatible) {
BalenaCommand.checkNotUsingOfflineMode();
}
if (ctr.readStdin) {
await this.getStdin();
}
}
}
export default hook;

View File

@ -1,44 +0,0 @@
/**
* @license
* Copyright 2019-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 type { Hook } from '@oclif/core';
let trackResolve: (result: Promise<any>) => void;
// note: trackPromise is subject to a Bluebird.timeout, defined in events.ts
export const trackPromise = new Promise((resolve) => {
trackResolve = resolve;
});
/**
* This is an oclif 'prerun' hook. This hook runs after the command line is
* parsed by oclif, but before the command's run() function is called.
* See: https://oclif.io/docs/hooks
*
* This hook is used to track CLI command signatures (usage analytics).
* A command signature is something like "env add NAME [VALUE]". That's
* literally so: 'NAME' and 'VALUE' are NOT replaced with actual values.
*/
const hook: Hook<'prerun'> = async function (options) {
const events = await import('../../events');
const cmd = options.Command.id;
// Intentionally do not await for the track promise here, in order to
// run the command tracking and the command itself in parallel.
trackResolve(events.trackCommand(cmd));
};
export default hook;

View File

@ -24,7 +24,12 @@ import type {
PineTypedResult,
} from 'balena-sdk';
import { instanceOf, NotLoggedInError, ExpectedError } from '../errors';
import {
instanceOf,
NotLoggedInError,
ExpectedError,
NotAvailableInOfflineModeError,
} from '../errors';
import { getBalenaSdk, getVisuals, stripIndent, getCliForm } from './lazy';
import validation = require('./validation');
import { delay } from './helpers';
@ -89,6 +94,41 @@ export async function checkLoggedIn(): Promise<void> {
}
}
/**
* Throw NotLoggedInError if not logged in when condition true.
*
* @param {boolean} doCheck - will check if true.
* @throws {NotLoggedInError}
*/
export const checkLoggedInIf = async (doCheck: boolean) => {
if (doCheck) {
await checkLoggedIn();
}
};
/**
* Throw NotAvailableInOfflineModeError if in offline mode.
*
* Called automatically if `onlineOnly=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).
*
* @throws {NotAvailableInOfflineModeError}
*/
export const checkNotUsingOfflineMode = async () => {
if (process.env.BALENARC_OFFLINE_MODE) {
throw new NotAvailableInOfflineModeError(stripIndent`
This command requires an internet connection, and cannot be used in offline mode.
To leave offline mode, unset the BALENARC_OFFLINE_MODE environment variable.
`);
}
};
export function askLoginType() {
return getCliForm().ask<'web' | 'credentials' | 'token' | 'register'>({
message: 'How would you like to login?',