diff --git a/automation/capitanodoc/capitanodoc.ts b/automation/capitanodoc/capitanodoc.ts index 29303e0c..2181b30f 100644 --- a/automation/capitanodoc/capitanodoc.ts +++ b/automation/capitanodoc/capitanodoc.ts @@ -149,7 +149,7 @@ const capitanoDoc = { }, { title: 'Utilities', - files: ['build/actions/util.js'], + files: ['build/actions-oclif/util/available-drives.js'], }, ], }; diff --git a/doc/cli.markdown b/doc/cli.markdown index d9e16680..7b33857d 100644 --- a/doc/cli.markdown +++ b/doc/cli.markdown @@ -2681,5 +2681,7 @@ the device IP or hostname ## util available-drives -Use this command to list your machine's drives usable for writing the OS image to. -Skips the system drives. +List available drives which are usable for writing an OS image to. +Does not list system drives. + +### Options diff --git a/lib/actions-oclif/util/available-drives.ts b/lib/actions-oclif/util/available-drives.ts new file mode 100644 index 00000000..42b5c059 --- /dev/null +++ b/lib/actions-oclif/util/available-drives.ts @@ -0,0 +1,75 @@ +/** + * @license + * Copyright 2016-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 { flags } from '@oclif/command'; +import Command from '../../command'; +import * as cf from '../../utils/common-flags'; +import { stripIndent, getChalk, getVisuals } from '../../utils/lazy'; + +interface FlagsDef { + help: void; +} + +export default class UtilAvailableDrivesCmd extends Command { + public static description = stripIndent` + List available drives. + + List available drives which are usable for writing an OS image to. + Does not list system drives. + `; + + public static usage = 'util available-drives'; + + public static flags: flags.Input = { + help: cf.help, + }; + + public async run() { + this.parse(UtilAvailableDrivesCmd); + + const sdk = await import('etcher-sdk'); + + const adapter = new sdk.scanner.adapters.BlockDeviceAdapter(() => false); + const scanner = new sdk.scanner.Scanner([adapter]); + await scanner.start(); + + function prepareDriveInfo(drive: any) { + const size = drive.size / 1000000000; + return { + device: drive.device, + size: `${size.toFixed(1)} GB`, + description: drive.description, + }; + } + + if (scanner.drives.size === 0) { + console.error( + `${getChalk().red( + 'x', + )} No available drives were detected, plug one in!`, + ); + } else { + console.log( + getVisuals().table.horizontal( + Array.from(scanner.drives).map(prepareDriveInfo), + ['device', 'size', 'description'], + ), + ); + } + scanner.stop(); + } +} diff --git a/lib/actions/index.ts b/lib/actions/index.ts index 9b405b9e..5d0f1275 100644 --- a/lib/actions/index.ts +++ b/lib/actions/index.ts @@ -19,7 +19,6 @@ export * as help from './help'; export * as local from './local'; export * as os from './os'; export * as push from './push'; -export * as util from './util'; export { build } from './build'; diff --git a/lib/actions/util.ts b/lib/actions/util.ts deleted file mode 100644 index 80b6fae4..00000000 --- a/lib/actions/util.ts +++ /dev/null @@ -1,64 +0,0 @@ -/* - * decaffeinate suggestions: - * DS102: Remove unnecessary code created because of implicit returns - * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md - */ -/* -Copyright 2016-2017 Balena - -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 { CommandDefinition } from 'capitano'; -import { getChalk, getVisuals, stripIndent } from '../utils/lazy'; - -export const availableDrives: CommandDefinition<{}, {}> = { - signature: 'util available-drives', - description: 'list available drives', - help: stripIndent` - Use this command to list your machine's drives usable for writing the OS image to. - Skips the system drives. - `, - async action() { - const sdk = await import('etcher-sdk'); - - const adapter = new sdk.scanner.adapters.BlockDeviceAdapter(() => false); - const scanner = new sdk.scanner.Scanner([adapter]); - await scanner.start(); - - function formatDrive(drive: any) { - const size = drive.size / 1000000000; - return { - device: drive.device, - size: `${size.toFixed(1)} GB`, - description: drive.description, - }; - } - - if (scanner.drives.size === 0) { - console.error( - `${getChalk().red( - 'x', - )} No available drives were detected, plug one in!`, - ); - } else { - console.log( - getVisuals().table.horizontal( - Array.from(scanner.drives).map(formatDrive), - ['device', 'size', 'description'], - ), - ); - } - scanner.stop(); - }, -}; diff --git a/lib/app-capitano.ts b/lib/app-capitano.ts index bdc44dfa..c5365f03 100644 --- a/lib/app-capitano.ts +++ b/lib/app-capitano.ts @@ -66,9 +66,6 @@ capitano.command(actions.preload); capitano.command(actions.local.configure); capitano.command(actions.local.flash); -// ---------- Public utils ---------- -capitano.command(actions.util.availableDrives); - // ------------ Local build and deploy ------- capitano.command(actions.build); capitano.command(actions.deploy); diff --git a/lib/preparser.ts b/lib/preparser.ts index f66a50de..50e3ff2d 100644 --- a/lib/preparser.ts +++ b/lib/preparser.ts @@ -185,6 +185,7 @@ export const convertedCommands = [ 'tag:rm', 'tag:set', 'tunnel', + 'util:available-drives', 'version', 'whoami', ];