From 9cdde4f6c21ea1593de83107794a5b7323ed4cfe Mon Sep 17 00:00:00 2001 From: myarmolinsky Date: Wed, 2 Oct 2024 07:55:21 -0400 Subject: [PATCH] Remove custom override of oclif Command class in favor of `prerun` hook Change-type: patch --- package.json | 2 +- src/app.ts | 2 +- src/commands/api-key/generate.ts | 3 +- src/commands/api-key/revoke.ts | 3 +- src/commands/api-keys/index.ts | 3 +- src/commands/app/create.ts | 4 +- src/commands/block/create.ts | 4 +- src/commands/build/index.ts | 10 +- src/commands/config/generate.ts | 3 +- src/commands/config/inject.ts | 3 +- src/commands/config/read.ts | 2 +- src/commands/config/reconfigure.ts | 3 +- src/commands/config/write.ts | 3 +- src/commands/deploy/index.ts | 8 +- src/commands/device/deactivate.ts | 3 +- src/commands/device/identify.ts | 3 +- src/commands/device/index.ts | 3 +- src/commands/device/init.ts | 6 +- src/commands/device/list.ts | 2 +- src/commands/device/local-mode.ts | 3 +- src/commands/device/move.ts | 3 +- src/commands/device/os-update.ts | 3 +- src/commands/device/pin.ts | 3 +- src/commands/device/public-url.ts | 3 +- src/commands/device/purge.ts | 3 +- src/commands/device/reboot.ts | 3 +- src/commands/device/register.ts | 3 +- src/commands/device/rename.ts | 3 +- src/commands/device/restart.ts | 3 +- src/commands/device/rm.ts | 3 +- src/commands/device/shutdown.ts | 3 +- src/commands/device/start-service.ts | 3 +- src/commands/device/stop-service.ts | 3 +- src/commands/device/track-fleet.ts | 3 +- src/commands/devices/supported.ts | 4 +- src/commands/env/add.ts | 7 +- src/commands/env/rename.ts | 8 +- src/commands/env/rm.ts | 8 +- src/commands/envs/index.ts | 7 +- src/commands/fleet/create.ts | 4 +- src/commands/fleet/index.ts | 4 +- src/commands/fleet/pin.ts | 3 +- src/commands/fleet/purge.ts | 2 +- src/commands/fleet/rename.ts | 4 +- src/commands/fleet/restart.ts | 2 +- src/commands/fleet/rm.ts | 2 +- src/commands/fleet/track-latest.ts | 3 +- src/commands/fleets/index.ts | 3 +- src/commands/internal/osinit.ts | 3 +- src/commands/join/index.ts | 6 +- src/commands/key/add.ts | 7 +- src/commands/key/index.ts | 3 +- src/commands/key/rm.ts | 3 +- src/commands/keys/index.ts | 2 +- src/commands/leave/index.ts | 6 +- src/commands/local/configure.ts | 3 +- src/commands/local/flash.ts | 3 +- src/commands/login/index.ts | 3 +- src/commands/logout/index.ts | 2 +- src/commands/logs/index.ts | 6 +- src/commands/notes/index.ts | 11 +- src/commands/orgs/index.ts | 2 +- src/commands/os/build-config.ts | 3 +- src/commands/os/configure.ts | 7 +- src/commands/os/download.ts | 6 +- src/commands/os/initialize.ts | 3 +- src/commands/os/versions.ts | 3 +- src/commands/preload/index.ts | 4 +- src/commands/push/index.ts | 11 +- src/commands/release/finalize.ts | 2 +- src/commands/release/index.ts | 3 +- src/commands/release/invalidate.ts | 2 +- src/commands/release/validate.ts | 2 +- src/commands/releases/index.ts | 3 +- src/commands/scan/index.ts | 3 +- src/commands/settings/index.ts | 2 +- src/commands/ssh/index.ts | 13 +- src/commands/support/index.ts | 3 +- src/commands/tag/rm.ts | 3 +- src/commands/tag/set.ts | 3 +- src/commands/tags/index.ts | 2 +- src/commands/tunnel/index.ts | 7 +- src/commands/util/available-drives.ts | 2 +- src/commands/version/index.ts | 3 +- src/commands/whoami/index.ts | 2 +- src/hooks/prerun.ts | 223 ++++++++++---------------- src/hooks/prerun/track.ts | 44 ----- src/utils/patterns.ts | 42 ++++- 88 files changed, 263 insertions(+), 374 deletions(-) delete mode 100644 src/hooks/prerun/track.ts diff --git a/package.json b/package.json index 55e90fbb..9988d26e 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/src/app.ts b/src/app.ts index 87003b5e..e2c4f17d 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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]); } diff --git a/src/commands/api-key/generate.ts b/src/commands/api-key/generate.ts index 58cd0608..e869358d 100644 --- a/src/commands/api-key/generate.ts +++ b/src/commands/api-key/generate.ts @@ -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'; diff --git a/src/commands/api-key/revoke.ts b/src/commands/api-key/revoke.ts index 2b158e7a..a8c4756b 100644 --- a/src/commands/api-key/revoke.ts +++ b/src/commands/api-key/revoke.ts @@ -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'; diff --git a/src/commands/api-keys/index.ts b/src/commands/api-keys/index.ts index 498ceaf6..560c83fb 100644 --- a/src/commands/api-keys/index.ts +++ b/src/commands/api-keys/index.ts @@ -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'; diff --git a/src/commands/app/create.ts b/src/commands/app/create.ts index 5f333a92..c6f4a21c 100644 --- a/src/commands/app/create.ts +++ b/src/commands/app/create.ts @@ -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'; diff --git a/src/commands/block/create.ts b/src/commands/block/create.ts index 47ce1959..dc2768c7 100644 --- a/src/commands/block/create.ts +++ b/src/commands/block/create.ts @@ -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'; diff --git a/src/commands/build/index.ts b/src/commands/build/index.ts index 08db04af..fd0c9940 100644 --- a/src/commands/build/index.ts +++ b/src/commands/build/index.ts @@ -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 diff --git a/src/commands/config/generate.ts b/src/commands/config/generate.ts index 25f7d7cd..7bb6b4a6 100644 --- a/src/commands/config/generate.ts +++ b/src/commands/config/generate.ts @@ -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 { diff --git a/src/commands/config/inject.ts b/src/commands/config/inject.ts index 9640776e..f34e71d0 100644 --- a/src/commands/config/inject.ts +++ b/src/commands/config/inject.ts @@ -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'; diff --git a/src/commands/config/read.ts b/src/commands/config/read.ts index 32fdf9ee..d32b511d 100644 --- a/src/commands/config/read.ts +++ b/src/commands/config/read.ts @@ -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'; diff --git a/src/commands/config/reconfigure.ts b/src/commands/config/reconfigure.ts index d00e7005..24003f8e 100644 --- a/src/commands/config/reconfigure.ts +++ b/src/commands/config/reconfigure.ts @@ -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'; diff --git a/src/commands/config/write.ts b/src/commands/config/write.ts index 8147169f..34cd3b8b 100644 --- a/src/commands/config/write.ts +++ b/src/commands/config/write.ts @@ -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'; diff --git a/src/commands/deploy/index.ts b/src/commands/deploy/index.ts index cf00b220..1e87ad74 100644 --- a/src/commands/deploy/index.ts +++ b/src/commands/deploy/index.ts @@ -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; diff --git a/src/commands/device/deactivate.ts b/src/commands/device/deactivate.ts index 52c9ff8c..e8e50611 100644 --- a/src/commands/device/deactivate.ts +++ b/src/commands/device/deactivate.ts @@ -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'; diff --git a/src/commands/device/identify.ts b/src/commands/device/identify.ts index 93b7e74d..57bef0af 100644 --- a/src/commands/device/identify.ts +++ b/src/commands/device/identify.ts @@ -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'; diff --git a/src/commands/device/index.ts b/src/commands/device/index.ts index a9428c64..86e2c0a0 100644 --- a/src/commands/device/index.ts +++ b/src/commands/device/index.ts @@ -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'; diff --git a/src/commands/device/init.ts b/src/commands/device/init.ts index 41edcb04..c48f4559 100644 --- a/src/commands/device/init.ts +++ b/src/commands/device/init.ts @@ -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 diff --git a/src/commands/device/list.ts b/src/commands/device/list.ts index 66592a65..ce492a0a 100644 --- a/src/commands/device/list.ts +++ b/src/commands/device/list.ts @@ -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'; diff --git a/src/commands/device/local-mode.ts b/src/commands/device/local-mode.ts index aa7be85a..e26ae15d 100644 --- a/src/commands/device/local-mode.ts +++ b/src/commands/device/local-mode.ts @@ -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'; diff --git a/src/commands/device/move.ts b/src/commands/device/move.ts index 59fbd63f..93393b32 100644 --- a/src/commands/device/move.ts +++ b/src/commands/device/move.ts @@ -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'; diff --git a/src/commands/device/os-update.ts b/src/commands/device/os-update.ts index e335d151..665c5d87 100644 --- a/src/commands/device/os-update.ts +++ b/src/commands/device/os-update.ts @@ -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'; diff --git a/src/commands/device/pin.ts b/src/commands/device/pin.ts index e27b294b..9e4382c4 100644 --- a/src/commands/device/pin.ts +++ b/src/commands/device/pin.ts @@ -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'; diff --git a/src/commands/device/public-url.ts b/src/commands/device/public-url.ts index 91fd9314..c1ff9bb5 100644 --- a/src/commands/device/public-url.ts +++ b/src/commands/device/public-url.ts @@ -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'; diff --git a/src/commands/device/purge.ts b/src/commands/device/purge.ts index 7c548406..00bd54d0 100644 --- a/src/commands/device/purge.ts +++ b/src/commands/device/purge.ts @@ -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'; diff --git a/src/commands/device/reboot.ts b/src/commands/device/reboot.ts index 1c4e5231..2a7e0e90 100644 --- a/src/commands/device/reboot.ts +++ b/src/commands/device/reboot.ts @@ -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'; diff --git a/src/commands/device/register.ts b/src/commands/device/register.ts index 6bfe5829..de756408 100644 --- a/src/commands/device/register.ts +++ b/src/commands/device/register.ts @@ -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'; diff --git a/src/commands/device/rename.ts b/src/commands/device/rename.ts index 9f0b12cf..0f56b8d4 100644 --- a/src/commands/device/rename.ts +++ b/src/commands/device/rename.ts @@ -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'; diff --git a/src/commands/device/restart.ts b/src/commands/device/restart.ts index b5fb5eef..92d1d90d 100644 --- a/src/commands/device/restart.ts +++ b/src/commands/device/restart.ts @@ -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 { diff --git a/src/commands/device/rm.ts b/src/commands/device/rm.ts index e7cb221d..1ce7434d 100644 --- a/src/commands/device/rm.ts +++ b/src/commands/device/rm.ts @@ -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'; diff --git a/src/commands/device/shutdown.ts b/src/commands/device/shutdown.ts index 8b9eeef6..79b1974e 100644 --- a/src/commands/device/shutdown.ts +++ b/src/commands/device/shutdown.ts @@ -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'; diff --git a/src/commands/device/start-service.ts b/src/commands/device/start-service.ts index 27c80727..5749c2a2 100644 --- a/src/commands/device/start-service.ts +++ b/src/commands/device/start-service.ts @@ -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'; diff --git a/src/commands/device/stop-service.ts b/src/commands/device/stop-service.ts index e7900b6a..652d1ec4 100644 --- a/src/commands/device/stop-service.ts +++ b/src/commands/device/stop-service.ts @@ -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'; diff --git a/src/commands/device/track-fleet.ts b/src/commands/device/track-fleet.ts index e5f35a32..cbf976b2 100644 --- a/src/commands/device/track-fleet.ts +++ b/src/commands/device/track-fleet.ts @@ -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'; diff --git a/src/commands/devices/supported.ts b/src/commands/devices/supported.ts index 9a96f427..964d5b35 100644 --- a/src/commands/devices/supported.ts +++ b/src/commands/devices/supported.ts @@ -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'; diff --git a/src/commands/env/add.ts b/src/commands/env/add.ts index fc99a309..8c6fccba 100644 --- a/src/commands/env/add.ts +++ b/src/commands/env/add.ts @@ -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]; diff --git a/src/commands/env/rename.ts b/src/commands/env/rename.ts index 11ae1ea0..dcdd59f4 100644 --- a/src/commands/env/rename.ts +++ b/src/commands/env/rename.ts @@ -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), diff --git a/src/commands/env/rm.ts b/src/commands/env/rm.ts index 39538d55..afe03928 100644 --- a/src/commands/env/rm.ts +++ b/src/commands/env/rm.ts @@ -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( diff --git a/src/commands/envs/index.ts b/src/commands/envs/index.ts index 553189d3..6d0802f1 100644 --- a/src/commands/envs/index.ts +++ b/src/commands/envs/index.ts @@ -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'); diff --git a/src/commands/fleet/create.ts b/src/commands/fleet/create.ts index 46e958ad..da773dec 100644 --- a/src/commands/fleet/create.ts +++ b/src/commands/fleet/create.ts @@ -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'; diff --git a/src/commands/fleet/index.ts b/src/commands/fleet/index.ts index 7c382550..ebba3444 100644 --- a/src/commands/fleet/index.ts +++ b/src/commands/fleet/index.ts @@ -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'; diff --git a/src/commands/fleet/pin.ts b/src/commands/fleet/pin.ts index bcbfdc8a..3d6df74d 100644 --- a/src/commands/fleet/pin.ts +++ b/src/commands/fleet/pin.ts @@ -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'; diff --git a/src/commands/fleet/purge.ts b/src/commands/fleet/purge.ts index 386d0aab..a6601ff1 100644 --- a/src/commands/fleet/purge.ts +++ b/src/commands/fleet/purge.ts @@ -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'; diff --git a/src/commands/fleet/rename.ts b/src/commands/fleet/rename.ts index a2b09196..cd99434c 100644 --- a/src/commands/fleet/rename.ts +++ b/src/commands/fleet/rename.ts @@ -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'; diff --git a/src/commands/fleet/restart.ts b/src/commands/fleet/restart.ts index 83d560c6..e81067b0 100644 --- a/src/commands/fleet/restart.ts +++ b/src/commands/fleet/restart.ts @@ -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'; diff --git a/src/commands/fleet/rm.ts b/src/commands/fleet/rm.ts index e81168b2..c115f67f 100644 --- a/src/commands/fleet/rm.ts +++ b/src/commands/fleet/rm.ts @@ -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` diff --git a/src/commands/fleet/track-latest.ts b/src/commands/fleet/track-latest.ts index 75fee330..588592ee 100644 --- a/src/commands/fleet/track-latest.ts +++ b/src/commands/fleet/track-latest.ts @@ -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'; diff --git a/src/commands/fleets/index.ts b/src/commands/fleets/index.ts index 91b2fba6..c895a956 100644 --- a/src/commands/fleets/index.ts +++ b/src/commands/fleets/index.ts @@ -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; diff --git a/src/commands/internal/osinit.ts b/src/commands/internal/osinit.ts index dfc9bc9a..4a420ae8 100644 --- a/src/commands/internal/osinit.ts +++ b/src/commands/internal/osinit.ts @@ -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. diff --git a/src/commands/join/index.ts b/src/commands/join/index.ts index af09affc..2e3ccc7f 100644 --- a/src/commands/join/index.ts +++ b/src/commands/join/index.ts @@ -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, diff --git a/src/commands/key/add.ts b/src/commands/key/add.ts index 24e4d4fb..51e075d4 100644 --- a/src/commands/key/add.ts +++ b/src/commands/key/add.ts @@ -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.'); } diff --git a/src/commands/key/index.ts b/src/commands/key/index.ts index fa343133..ebe90ef2 100644 --- a/src/commands/key/index.ts +++ b/src/commands/key/index.ts @@ -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'; diff --git a/src/commands/key/rm.ts b/src/commands/key/rm.ts index aecad823..b6f31712 100644 --- a/src/commands/key/rm.ts +++ b/src/commands/key/rm.ts @@ -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'; diff --git a/src/commands/keys/index.ts b/src/commands/keys/index.ts index af77abfb..800aa6ff 100644 --- a/src/commands/keys/index.ts +++ b/src/commands/keys/index.ts @@ -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'; diff --git a/src/commands/leave/index.ts b/src/commands/leave/index.ts index a94d9a77..9ff81399 100644 --- a/src/commands/leave/index.ts +++ b/src/commands/leave/index.ts @@ -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); } } diff --git a/src/commands/local/configure.ts b/src/commands/local/configure.ts index 0aace8f2..7e72fb60 100644 --- a/src/commands/local/configure.ts +++ b/src/commands/local/configure.ts @@ -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'; diff --git a/src/commands/local/flash.ts b/src/commands/local/flash.ts index 1e6b137d..592e0993 100644 --- a/src/commands/local/flash.ts +++ b/src/commands/local/flash.ts @@ -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'; diff --git a/src/commands/login/index.ts b/src/commands/login/index.ts index 653836c5..7fb7de77 100644 --- a/src/commands/login/index.ts +++ b/src/commands/login/index.ts @@ -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'; diff --git a/src/commands/logout/index.ts b/src/commands/logout/index.ts index ff14cb9c..313db52f 100644 --- a/src/commands/logout/index.ts +++ b/src/commands/logout/index.ts @@ -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 { diff --git a/src/commands/logs/index.ts b/src/commands/logs/index.ts index 7a6f9056..8c3cbf22 100644 --- a/src/commands/logs/index.ts +++ b/src/commands/logs/index.ts @@ -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, diff --git a/src/commands/notes/index.ts b/src/commands/notes/index.ts index 6cf6de27..d27654ca 100644 --- a/src/commands/notes/index.ts +++ b/src/commands/notes/index.ts @@ -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 ?? ''); } } diff --git a/src/commands/orgs/index.ts b/src/commands/orgs/index.ts index 62ec99cf..bd210d5b 100644 --- a/src/commands/orgs/index.ts +++ b/src/commands/orgs/index.ts @@ -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'; diff --git a/src/commands/os/build-config.ts b/src/commands/os/build-config.ts index 0e88a58d..caac821a 100644 --- a/src/commands/os/build-config.ts +++ b/src/commands/os/build-config.ts @@ -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'; diff --git a/src/commands/os/configure.ts b/src/commands/os/configure.ts index 1fd978cb..cb9669ef 100644 --- a/src/commands/os/configure.ts +++ b/src/commands/os/configure.ts @@ -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(); } /** diff --git a/src/commands/os/download.ts b/src/commands/os/download.ts index 9c902cca..122d479d 100644 --- a/src/commands/os/download.ts +++ b/src/commands/os/download.ts @@ -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' diff --git a/src/commands/os/initialize.ts b/src/commands/os/initialize.ts index 8e815e59..76c5063f 100644 --- a/src/commands/os/initialize.ts +++ b/src/commands/os/initialize.ts @@ -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'; diff --git a/src/commands/os/versions.ts b/src/commands/os/versions.ts index 7b813b78..60ec4936 100644 --- a/src/commands/os/versions.ts +++ b/src/commands/os/versions.ts @@ -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'; diff --git a/src/commands/preload/index.ts b/src/commands/preload/index.ts index 74d27ac8..cad1c732 100644 --- a/src/commands/preload/index.ts +++ b/src/commands/preload/index.ts @@ -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, diff --git a/src/commands/push/index.ts b/src/commands/push/index.ts index a1a10521..520c2511 100644 --- a/src/commands/push/index.ts +++ b/src/commands/push/index.ts @@ -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'), diff --git a/src/commands/release/finalize.ts b/src/commands/release/finalize.ts index bcae1b6b..e1f6c229 100644 --- a/src/commands/release/finalize.ts +++ b/src/commands/release/finalize.ts @@ -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'; diff --git a/src/commands/release/index.ts b/src/commands/release/index.ts index 9f0a6960..1b9db22e 100644 --- a/src/commands/release/index.ts +++ b/src/commands/release/index.ts @@ -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'; diff --git a/src/commands/release/invalidate.ts b/src/commands/release/invalidate.ts index b4c68070..26cfee60 100644 --- a/src/commands/release/invalidate.ts +++ b/src/commands/release/invalidate.ts @@ -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'; diff --git a/src/commands/release/validate.ts b/src/commands/release/validate.ts index eca3ce68..83c269da 100644 --- a/src/commands/release/validate.ts +++ b/src/commands/release/validate.ts @@ -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'; diff --git a/src/commands/releases/index.ts b/src/commands/releases/index.ts index c5510113..09d049e5 100644 --- a/src/commands/releases/index.ts +++ b/src/commands/releases/index.ts @@ -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'; diff --git a/src/commands/scan/index.ts b/src/commands/scan/index.ts index dbe68a14..ec170e3c 100644 --- a/src/commands/scan/index.ts +++ b/src/commands/scan/index.ts @@ -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'; diff --git a/src/commands/settings/index.ts b/src/commands/settings/index.ts index 71d3d116..f5ca58f0 100644 --- a/src/commands/settings/index.ts +++ b/src/commands/settings/index.ts @@ -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'; diff --git a/src/commands/ssh/index.ts b/src/commands/ssh/index.ts index f44e2ee6..9c990da5 100644 --- a/src/commands/ssh/index.ts +++ b/src/commands/ssh/index.ts @@ -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, diff --git a/src/commands/support/index.ts b/src/commands/support/index.ts index 58550cb2..50d4e420 100644 --- a/src/commands/support/index.ts +++ b/src/commands/support/index.ts @@ -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'; diff --git a/src/commands/tag/rm.ts b/src/commands/tag/rm.ts index b3cf0aa7..3c1585cc 100644 --- a/src/commands/tag/rm.ts +++ b/src/commands/tag/rm.ts @@ -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'; diff --git a/src/commands/tag/set.ts b/src/commands/tag/set.ts index 306e90de..60575a32 100644 --- a/src/commands/tag/set.ts +++ b/src/commands/tag/set.ts @@ -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'; diff --git a/src/commands/tags/index.ts b/src/commands/tags/index.ts index cb13e7cc..79b90229 100644 --- a/src/commands/tags/index.ts +++ b/src/commands/tags/index.ts @@ -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'; diff --git a/src/commands/tunnel/index.ts b/src/commands/tunnel/index.ts index 299943a7..fe8556e8 100644 --- a/src/commands/tunnel/index.ts +++ b/src/commands/tunnel/index.ts @@ -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 = ( diff --git a/src/commands/util/available-drives.ts b/src/commands/util/available-drives.ts index 705e157e..3de865cf 100644 --- a/src/commands/util/available-drives.ts +++ b/src/commands/util/available-drives.ts @@ -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'; diff --git a/src/commands/version/index.ts b/src/commands/version/index.ts index 9e1c3c86..c7f5919c 100644 --- a/src/commands/version/index.ts +++ b/src/commands/version/index.ts @@ -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 { diff --git a/src/commands/whoami/index.ts b/src/commands/whoami/index.ts index 7c01b0f2..19ff60e5 100644 --- a/src/commands/whoami/index.ts +++ b/src/commands/whoami/index.ts @@ -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 { diff --git a/src/hooks/prerun.ts b/src/hooks/prerun.ts index 4c5d66bd..3e3e1d47 100644 --- a/src/hooks/prerun.ts +++ b/src/hooks/prerun.ts @@ -15,156 +15,105 @@ * 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) => 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; +// note: trackPromise is subject to a Bluebird.timeout, defined in events.ts +export const trackPromise = new Promise((resolve) => { + trackResolve = resolve; +}); - /** - * 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; - - /** - * 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', - ); - } +/** + * 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 + */ +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} - */ - public static async checkLoggedIn() { - await (await import('./utils/patterns')).checkLoggedIn(); - } +/** + * 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. + */ +const DEFAULT_ROOT = false; - /** - * Throw NotLoggedInError if not logged in when condition true. - * - * @param {boolean} doCheck - will check if true. - * @throws {NotLoggedInError} - */ - public static async checkLoggedInIf(doCheck: boolean) { - if (doCheck) { - await this.checkLoggedIn(); - } - } +/** + * Require authentication to run. + * When set to true, command will exit with an error + * if user is not already logged in. + */ +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} - */ - 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. - `); - } - } +/** + * 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). + */ +const DEFAULT_OFFLINE_COMPATIBLE = false; - /** - * 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'))(); - } +/** + * 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. + */ - /** - * Get a logger instance. - */ - protected static async getLogger() { - return (await import('./utils/logger')).getLogger(); - } - - protected async init() { - const ctr = this.constructor as typeof BalenaCommand; - - if (ctr.root) { - await BalenaCommand.checkElevatedPrivileges(); +const hook: Hook<'prerun'> = async function (options) { + try { + if ( + (options.Command as Command.Class & { root: boolean }).root ?? + DEFAULT_ROOT + ) { + await checkElevatedPrivileges(); } - if (ctr.authenticated) { - await BalenaCommand.checkLoggedIn(); + if ( + (options.Command as Command.Class & { authenticated: boolean }) + .authenticated ?? + DEFAULT_AUTHENTICATED + ) { + await checkLoggedIn(); } - if (!ctr.offlineCompatible) { - BalenaCommand.checkNotUsingOfflineMode(); - } - - if (ctr.readStdin) { - await this.getStdin(); + 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; + + // 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; diff --git a/src/hooks/prerun/track.ts b/src/hooks/prerun/track.ts deleted file mode 100644 index 24152a1b..00000000 --- a/src/hooks/prerun/track.ts +++ /dev/null @@ -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) => 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; diff --git a/src/utils/patterns.ts b/src/utils/patterns.ts index ebae0aa6..6dcbc516 100644 --- a/src/utils/patterns.ts +++ b/src/utils/patterns.ts @@ -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 { } } +/** + * 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?',