From d2a9aee685873f11a346de1436a47569c65bc87f Mon Sep 17 00:00:00 2001 From: Eugene Mirotin Date: Thu, 15 Jun 2017 00:23:39 +0300 Subject: [PATCH] improve available drives listing make the config a path to the file and not the stringified JSON --- build/actions/device.js | 4 ++-- build/actions/os.js | 11 +++++++---- build/actions/util.js | 14 +++++++++----- capitanodoc.coffee | 6 +++++- doc/automated-init.md | 3 +-- doc/cli.markdown | 24 +++++++++++++++--------- lib/actions/device.coffee | 6 +++--- lib/actions/os.coffee | 12 ++++++++---- lib/actions/util.coffee | 19 ++++++++++++++++--- 9 files changed, 66 insertions(+), 33 deletions(-) diff --git a/build/actions/device.js b/build/actions/device.js index 88caf0fd..e315fba3 100644 --- a/build/actions/device.js +++ b/build/actions/device.js @@ -274,12 +274,12 @@ 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. Check `resin os available-drives` for available options.', + description: 'the drive to write the image to, like `/dev/sdb` or `/dev/mmcblk0`. Careful with this as you can erase your hard drive. Check `resin util available-drives` for available options.', parameter: 'drive', alias: 'd' }, { signature: 'config', - description: 'stringified JSON with the device config, see `resin os build-config`', + description: 'path to the config JSON file, see `resin os build-config`', parameter: 'config' } ], diff --git a/build/actions/os.js b/build/actions/os.js index d445646a..7289d00f 100644 --- a/build/actions/os.js +++ b/build/actions/os.js @@ -202,19 +202,22 @@ exports.configure = { alias: 'v' }, { signature: 'config', - description: 'stringified JSON with the device config, see `resin os build-config`', + description: 'path to the config JSON file, see `resin os build-config`', parameter: 'config' } ], action: function(params, options, done) { - var helpers, init, resin; + var Promise, fs, helpers, init, readFileAsync, resin; + fs = require('fs'); + Promise = require('bluebird'); + readFileAsync = Promise.promisify(fs.readFile); resin = require('resin-sdk-preconfigured'); init = require('resin-device-init'); helpers = require('../utils/helpers'); console.info('Configuring operating system image'); return resin.models.device.get(params.uuid).then(function(device) { if (options.config) { - return JSON.parse(options.config); + return readFileAsync(options.config, 'utf8').then(JSON.parse); } return buildConfig(params.image, device.device_type, options.advanced); }).then(function(answers) { @@ -239,7 +242,7 @@ exports.initialize = { required: 'You have to specify a device type' }, { signature: 'drive', - description: 'drive to write the image to. Check `resin os available-drives` for available options.', + description: 'drive to write the image to. Check `resin util available-drives` for available options.', parameter: 'drive', alias: 'd' } diff --git a/build/actions/util.js b/build/actions/util.js index a7403b5e..8cbc1b87 100644 --- a/build/actions/util.js +++ b/build/actions/util.js @@ -22,16 +22,22 @@ _ = require('lodash'); exports.availableDrives = { signature: 'util available-drives', description: 'list available drives', + help: "Use this command to list your machine's drives usable for writing the OS image to.\nSkips the system drives.", action: function() { - var Promise, chalk, driveListAsync, drivelist, formatDrive, getDrives; + var Promise, chalk, driveListAsync, drivelist, formatDrive, getDrives, visuals; Promise = require('bluebird'); drivelist = require('drivelist'); driveListAsync = Promise.promisify(drivelist.list); chalk = require('chalk'); + visuals = require('resin-cli-visuals'); formatDrive = function(drive) { var size; size = drive.size / 1000000000; - return drive.device + " (" + (size.toFixed(1)) + " GB) - " + drive.description; + return { + device: drive.device, + size: (size.toFixed(1)) + " GB", + description: drive.description + }; }; getDrives = function() { return driveListAsync().then(function(drives) { @@ -45,9 +51,7 @@ exports.availableDrives = { console.error((chalk.red('x')) + " No available drives were detected, plug one in!"); return; } - return drives.forEach(function(drive) { - return console.log(formatDrive(drive)); - }); + return console.log(visuals.table.horizontal(drives.map(formatDrive), ['device', 'size', 'description'])); }); } }; diff --git a/capitanodoc.coffee b/capitanodoc.coffee index 11b2fa46..d21c665b 100644 --- a/capitanodoc.coffee +++ b/capitanodoc.coffee @@ -103,5 +103,9 @@ module.exports = 'lib/actions/build.coffee' 'lib/actions/deploy.coffee' ] - } + }, + { + title: 'Utilities', + files: [ 'lib/actions/util.coffee' ] + }, ] diff --git a/doc/automated-init.md b/doc/automated-init.md index 00fc7f97..c3fcd3fd 100644 --- a/doc/automated-init.md +++ b/doc/automated-init.md @@ -7,7 +7,7 @@ It requires collecting some preliminary information _once_. The final command to provision the device looks like this: ```bash -resin device init --app APP_ID --os-version OS_VERSION --drive DRIVE --config "$(cat CONFIG_FILE)" --yes +resin device init --app APP_ID --os-version OS_VERSION --drive DRIVE --config CONFIG_FILE --yes ``` @@ -15,7 +15,6 @@ You can run this command as many times as you need, putting the new medium (SD c But before you can run it you need to collect the parameters and build the configuration file. Keep reading to figure out how to do it. -Also note how the config is _a string_, and not a path to the file, so we're using `"$(cat CONFIG_FILE)"` here. ## Collect all the required parameters. diff --git a/doc/cli.markdown b/doc/cli.markdown index d5b05768..b701156b 100644 --- a/doc/cli.markdown +++ b/doc/cli.markdown @@ -107,7 +107,6 @@ environment variable (in the same standard URL format). - [os download <type>](#os-download-60-type-62-) - [os build-config <image> <device-type>](#os-build-config-60-image-62-60-device-type-62-) - [os configure <image> <uuid>](#os-configure-60-image-62-60-uuid-62-) - - [os available-drives](#os-available-drives) - [os initialize <image>](#os-initialize-60-image-62-) - Config @@ -142,6 +141,10 @@ environment variable (in the same standard URL format). - [build [source]](#build-source-) - [deploy <appName> [image]](#deploy-60-appname-62-image-) +- Utilities + + - [util available-drives](#util-available-drives) + # Application ## app create <name> @@ -490,11 +493,11 @@ or 'menu' (will show the interactive menu) #### --drive, -d <drive> -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. +the drive to write the image to, like `/dev/sdb` or `/dev/mmcblk0`. Careful with this as you can erase your hard drive. Check `resin util available-drives` for available options. #### --config <config> -stringified JSON with the device config, see `resin os build-config` +path to the config JSON file, see `resin os build-config` # Environment Variables @@ -911,11 +914,7 @@ show advanced commands #### --config <config> -stringified JSON with the device config, see `resin os build-config` - -## os available-drives - -undefined +path to the config JSON file, see `resin os build-config` ## os initialize <image> @@ -940,7 +939,7 @@ device type (Check available types with `resin devices supported`) #### --drive, -d <drive> -drive to write the image to. Check `resin os available-drives` for available options. +drive to write the image to. Check `resin util available-drives` for available options. # Config @@ -1474,3 +1473,10 @@ Don't use docker layer caching when building Run an emulated build using Qemu +# Utilities + +## util available-drives + +Use this command to list your machine's drives usable for writing the OS image to. +Skips the system drives. + diff --git a/lib/actions/device.coffee b/lib/actions/device.coffee index 0e5b83a1..951cd0ec 100644 --- a/lib/actions/device.coffee +++ b/lib/actions/device.coffee @@ -379,15 +379,15 @@ exports.init = _.assign({}, commandOptions.osVersion, { signature: 'os-version', parameter: 'os-version' }) { signature: 'drive' - description: 'the drive to write the image to, like /dev/sdb. + description: 'the drive to write the image to, like `/dev/sdb` or `/dev/mmcblk0`. Careful with this as you can erase your hard drive. - Check `resin os available-drives` for available options.' + Check `resin util available-drives` for available options.' parameter: 'drive' alias: 'd' } { signature: 'config' - description: 'stringified JSON with the device config, see `resin os build-config`' + description: 'path to the config JSON file, see `resin os build-config`' parameter: 'config' } ] diff --git a/lib/actions/os.coffee b/lib/actions/os.coffee index 6dc6353d..fd7f0397 100644 --- a/lib/actions/os.coffee +++ b/lib/actions/os.coffee @@ -218,11 +218,14 @@ exports.configure = } { signature: 'config' - description: 'stringified JSON with the device config, see `resin os build-config`' + description: 'path to the config JSON file, see `resin os build-config`' parameter: 'config' } ] action: (params, options, done) -> + fs = require('fs') + Promise = require('bluebird') + readFileAsync = Promise.promisify(fs.readFile) resin = require('resin-sdk-preconfigured') init = require('resin-device-init') helpers = require('../utils/helpers') @@ -230,8 +233,9 @@ exports.configure = console.info('Configuring operating system image') resin.models.device.get(params.uuid).then (device) -> if options.config - return JSON.parse(options.config) - buildConfig(params.image, device.device_type, options.advanced) + return readFileAsync(options.config, 'utf8') + .then(JSON.parse) + return buildConfig(params.image, device.device_type, options.advanced) .then (answers) -> init.configure(params.image, params.uuid, answers).then(helpers.osProgressHandler) .nodeify(done) @@ -265,7 +269,7 @@ exports.initialize = } { signature: 'drive' - description: 'drive to write the image to. Check `resin os available-drives` for available options.' + description: 'drive to write the image to. Check `resin util available-drives` for available options.' parameter: 'drive' alias: 'd' } diff --git a/lib/actions/util.coffee b/lib/actions/util.coffee index f74d3fb0..d6fb95fe 100644 --- a/lib/actions/util.coffee +++ b/lib/actions/util.coffee @@ -20,15 +20,24 @@ exports.availableDrives = # TODO: dedupe with https://github.com/resin-io-modules/resin-cli-visuals/blob/master/lib/widgets/drive/index.coffee signature: 'util available-drives' description: 'list available drives' + help: """ + Use this command to list your machine's drives usable for writing the OS image to. + Skips the system drives. + """ action: -> Promise = require('bluebird') drivelist = require('drivelist') driveListAsync = Promise.promisify(drivelist.list) chalk = require('chalk') + visuals = require('resin-cli-visuals') formatDrive = (drive) -> size = drive.size / 1000000000 - return "#{drive.device} (#{size.toFixed(1)} GB) - #{drive.description}" + return { + device: drive.device + size: "#{size.toFixed(1)} GB" + description: drive.description + } getDrives = -> driveListAsync().then (drives) -> @@ -39,5 +48,9 @@ exports.availableDrives = 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)) + + console.log visuals.table.horizontal drives.map(formatDrive), [ + 'device' + 'size' + 'description' + ]