From ad940824a600042b2850eb9c537fb24162b910fd Mon Sep 17 00:00:00 2001 From: Eugene Mirotin Date: Fri, 9 Jun 2017 13:44:58 +0300 Subject: [PATCH] list detected drives with resin os available-drives --- CHANGELOG.md | 1 + build/actions/device.js | 2 +- build/actions/os.js | 42 +++++++++++++++++++++++++++++++++++---- build/app.js | 2 ++ lib/actions/device.coffee | 2 +- lib/actions/os.coffee | 31 +++++++++++++++++++++++++++-- lib/app.coffee | 1 + 7 files changed, 73 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ad42dd9..5b35c911 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Allow OS version selection when doing `resin device init` - Actually tolerate the `--yes` param to `resin device init` - Allows passing `--drive` to `resin device init` +- List detected drives with `resin os available-drives` ### Fixed diff --git a/build/actions/device.js b/build/actions/device.js index 6b35ffe0..76168bad 100644 --- a/build/actions/device.js +++ b/build/actions/device.js @@ -273,7 +273,7 @@ exports.init = { parameter: 'os-version' }), { signature: 'drive', - description: 'the drive to write the image to, like /dev/sdb. Careful with this as you can erase your hard drive.', + description: 'the drive to write the image to, like /dev/sdb. Careful with this as you can erase your hard drive. Check `resin os available-drives` for available options.', parameter: 'drive', alias: 'd' } diff --git a/build/actions/os.js b/build/actions/os.js index 541232e2..ab15d0f7 100644 --- a/build/actions/os.js +++ b/build/actions/os.js @@ -15,10 +15,12 @@ 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. */ -var commandOptions, formatVersion, initWarningMessage, resolveVersion; +var _, commandOptions, formatVersion, initWarningMessage, resolveVersion; commandOptions = require('./command-options'); +_ = require('lodash'); + formatVersion = function(v, isRecommended) { var result; result = "v" + v; @@ -147,8 +149,7 @@ exports.configure = { } ], action: function(params, options, done) { - var _, form, helpers, init, resin; - _ = require('lodash'); + var form, helpers, init, resin; resin = require('resin-sdk-preconfigured'); form = require('resin-cli-form'); init = require('resin-device-init'); @@ -178,6 +179,39 @@ exports.configure = { initWarningMessage = 'Note: Initializing the device may ask for administrative permissions\nbecause we need to access the raw devices directly.'; +exports.availableDrives = { + signature: 'os available-drives', + description: 'list available drives', + action: function() { + var Promise, chalk, driveListAsync, drivelist, formatDrive, getDrives; + Promise = require('bluebird'); + drivelist = require('drivelist'); + driveListAsync = Promise.promisify(drivelist.list); + chalk = require('chalk'); + formatDrive = function(drive) { + var size; + size = drive.size / 1000000000; + return drive.device + " (" + (size.toFixed(1)) + " GB) - " + drive.description; + }; + getDrives = function() { + return driveListAsync().then(function(drives) { + return _.reject(drives, { + system: true + }); + }); + }; + return getDrives().then(function(drives) { + if (!drives.length) { + console.error((chalk.red('x')) + " No available drives were detected, plug one in!"); + return; + } + return drives.forEach(function(drive) { + return console.log(formatDrive(drive)); + }); + }); + } +}; + exports.initialize = { signature: 'os initialize ', description: 'initialize an os image', @@ -192,7 +226,7 @@ exports.initialize = { required: 'You have to specify a device type' }, { signature: 'drive', - description: 'drive', + description: 'drive to write the image to. Check `resin os available-drives` for available options.', parameter: 'drive', alias: 'd' } diff --git a/build/app.js b/build/app.js index 73564169..84f7b1f5 100644 --- a/build/app.js +++ b/build/app.js @@ -168,6 +168,8 @@ capitano.command(actions.env.remove); capitano.command(actions.os.versions); +capitano.command(actions.os.availableDrives); + capitano.command(actions.os.download); capitano.command(actions.os.configure); diff --git a/lib/actions/device.coffee b/lib/actions/device.coffee index 5cda9565..64479cfd 100644 --- a/lib/actions/device.coffee +++ b/lib/actions/device.coffee @@ -376,7 +376,7 @@ exports.init = signature: 'drive' description: 'the drive to write the image to, like /dev/sdb. Careful with this as you can erase your hard drive. - ' + Check `resin os available-drives` for available options.' parameter: 'drive' alias: 'd' } diff --git a/lib/actions/os.coffee b/lib/actions/os.coffee index 5fe33667..e1ddcdc2 100644 --- a/lib/actions/os.coffee +++ b/lib/actions/os.coffee @@ -15,6 +15,7 @@ limitations under the License. ### commandOptions = require('./command-options') +_ = require('lodash') formatVersion = (v, isRecommended) -> result = "v#{v}" @@ -161,7 +162,6 @@ exports.configure = alias: 'v' ] action: (params, options, done) -> - _ = require('lodash') resin = require('resin-sdk-preconfigured') form = require('resin-cli-form') init = require('resin-device-init') @@ -191,6 +191,33 @@ initWarningMessage = ''' because we need to access the raw devices directly. ''' +exports.availableDrives = + # TODO: dedupe with https://github.com/resin-io-modules/resin-cli-visuals/blob/master/lib/widgets/drive/index.coffee + signature: 'os available-drives' + description: 'list available drives' + action: -> + Promise = require('bluebird') + drivelist = require('drivelist') + driveListAsync = Promise.promisify(drivelist.list) + chalk = require('chalk') + + formatDrive = (drive) -> + size = drive.size / 1000000000 + return "#{drive.device} (#{size.toFixed(1)} GB) - #{drive.description}" + + getDrives = -> + driveListAsync().then (drives) -> + return _.reject(drives, system: true) + + getDrives() + .then (drives) -> + if not drives.length + console.error("#{chalk.red('x')} No available drives were detected, plug one in!") + return + drives.forEach (drive) -> + console.log(formatDrive(drive)) + + exports.initialize = signature: 'os initialize ' description: 'initialize an os image' @@ -215,7 +242,7 @@ exports.initialize = } { signature: 'drive' - description: 'drive' + description: 'drive to write the image to. Check `resin os available-drives` for available options.' parameter: 'drive' alias: 'd' } diff --git a/lib/app.coffee b/lib/app.coffee index 4de49f2c..f7d77852 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -136,6 +136,7 @@ capitano.command(actions.env.remove) # ---------- OS Module ---------- capitano.command(actions.os.versions) +capitano.command(actions.os.availableDrives) capitano.command(actions.os.download) capitano.command(actions.os.configure) capitano.command(actions.os.initialize)