improve available drives listing

make the config a path to the file and not the stringified JSON
This commit is contained in:
Eugene Mirotin
2017-06-15 00:23:39 +03:00
parent 77a4c6fdc2
commit d2a9aee685
9 changed files with 66 additions and 33 deletions

View File

@ -274,12 +274,12 @@ exports.init = {
parameter: 'os-version' parameter: 'os-version'
}), { }), {
signature: 'drive', 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', parameter: 'drive',
alias: 'd' alias: 'd'
}, { }, {
signature: 'config', 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' parameter: 'config'
} }
], ],

View File

@ -202,19 +202,22 @@ exports.configure = {
alias: 'v' alias: 'v'
}, { }, {
signature: 'config', 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' parameter: 'config'
} }
], ],
action: function(params, options, done) { 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'); resin = require('resin-sdk-preconfigured');
init = require('resin-device-init'); init = require('resin-device-init');
helpers = require('../utils/helpers'); helpers = require('../utils/helpers');
console.info('Configuring operating system image'); console.info('Configuring operating system image');
return resin.models.device.get(params.uuid).then(function(device) { return resin.models.device.get(params.uuid).then(function(device) {
if (options.config) { 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); return buildConfig(params.image, device.device_type, options.advanced);
}).then(function(answers) { }).then(function(answers) {
@ -239,7 +242,7 @@ exports.initialize = {
required: 'You have to specify a device type' required: 'You have to specify a device type'
}, { }, {
signature: 'drive', 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', parameter: 'drive',
alias: 'd' alias: 'd'
} }

View File

@ -22,16 +22,22 @@ _ = require('lodash');
exports.availableDrives = { exports.availableDrives = {
signature: 'util available-drives', signature: 'util available-drives',
description: 'list 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() { action: function() {
var Promise, chalk, driveListAsync, drivelist, formatDrive, getDrives; var Promise, chalk, driveListAsync, drivelist, formatDrive, getDrives, visuals;
Promise = require('bluebird'); Promise = require('bluebird');
drivelist = require('drivelist'); drivelist = require('drivelist');
driveListAsync = Promise.promisify(drivelist.list); driveListAsync = Promise.promisify(drivelist.list);
chalk = require('chalk'); chalk = require('chalk');
visuals = require('resin-cli-visuals');
formatDrive = function(drive) { formatDrive = function(drive) {
var size; var size;
size = drive.size / 1000000000; 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() { getDrives = function() {
return driveListAsync().then(function(drives) { return driveListAsync().then(function(drives) {
@ -45,9 +51,7 @@ exports.availableDrives = {
console.error((chalk.red('x')) + " No available drives were detected, plug one in!"); console.error((chalk.red('x')) + " No available drives were detected, plug one in!");
return; return;
} }
return drives.forEach(function(drive) { return console.log(visuals.table.horizontal(drives.map(formatDrive), ['device', 'size', 'description']));
return console.log(formatDrive(drive));
});
}); });
} }
}; };

View File

@ -103,5 +103,9 @@ module.exports =
'lib/actions/build.coffee' 'lib/actions/build.coffee'
'lib/actions/deploy.coffee' 'lib/actions/deploy.coffee'
] ]
} },
{
title: 'Utilities',
files: [ 'lib/actions/util.coffee' ]
},
] ]

View File

@ -7,7 +7,7 @@ It requires collecting some preliminary information _once_.
The final command to provision the device looks like this: The final command to provision the device looks like this:
```bash ```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. 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. ## Collect all the required parameters.

View File

@ -107,7 +107,6 @@ environment variable (in the same standard URL format).
- [os download <type>](#os-download-60-type-62-) - [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 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 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-) - [os initialize <image>](#os-initialize-60-image-62-)
- Config - Config
@ -142,6 +141,10 @@ environment variable (in the same standard URL format).
- [build [source]](#build-source-) - [build [source]](#build-source-)
- [deploy <appName> [image]](#deploy-60-appname-62-image-) - [deploy <appName> [image]](#deploy-60-appname-62-image-)
- Utilities
- [util available-drives](#util-available-drives)
# Application # Application
## app create <name> ## app create <name>
@ -490,11 +493,11 @@ or 'menu' (will show the interactive menu)
#### --drive, -d <drive> #### --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> #### --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 # Environment Variables
@ -911,11 +914,7 @@ show advanced commands
#### --config <config> #### --config <config>
stringified JSON with the device config, see `resin os build-config` path to the config JSON file, see `resin os build-config`
## os available-drives
undefined
## os initialize <image> ## os initialize <image>
@ -940,7 +939,7 @@ device type (Check available types with `resin devices supported`)
#### --drive, -d <drive> #### --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 # Config
@ -1474,3 +1473,10 @@ Don't use docker layer caching when building
Run an emulated build using Qemu 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.

View File

@ -379,15 +379,15 @@ exports.init =
_.assign({}, commandOptions.osVersion, { signature: 'os-version', parameter: 'os-version' }) _.assign({}, commandOptions.osVersion, { signature: 'os-version', parameter: 'os-version' })
{ {
signature: 'drive' 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. 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' parameter: 'drive'
alias: 'd' alias: 'd'
} }
{ {
signature: 'config' 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' parameter: 'config'
} }
] ]

View File

@ -218,11 +218,14 @@ exports.configure =
} }
{ {
signature: 'config' 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' parameter: 'config'
} }
] ]
action: (params, options, done) -> action: (params, options, done) ->
fs = require('fs')
Promise = require('bluebird')
readFileAsync = Promise.promisify(fs.readFile)
resin = require('resin-sdk-preconfigured') resin = require('resin-sdk-preconfigured')
init = require('resin-device-init') init = require('resin-device-init')
helpers = require('../utils/helpers') helpers = require('../utils/helpers')
@ -230,8 +233,9 @@ exports.configure =
console.info('Configuring operating system image') console.info('Configuring operating system image')
resin.models.device.get(params.uuid).then (device) -> resin.models.device.get(params.uuid).then (device) ->
if options.config if options.config
return JSON.parse(options.config) return readFileAsync(options.config, 'utf8')
buildConfig(params.image, device.device_type, options.advanced) .then(JSON.parse)
return buildConfig(params.image, device.device_type, options.advanced)
.then (answers) -> .then (answers) ->
init.configure(params.image, params.uuid, answers).then(helpers.osProgressHandler) init.configure(params.image, params.uuid, answers).then(helpers.osProgressHandler)
.nodeify(done) .nodeify(done)
@ -265,7 +269,7 @@ exports.initialize =
} }
{ {
signature: 'drive' 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' parameter: 'drive'
alias: 'd' alias: 'd'
} }

View File

@ -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 # TODO: dedupe with https://github.com/resin-io-modules/resin-cli-visuals/blob/master/lib/widgets/drive/index.coffee
signature: 'util available-drives' signature: 'util available-drives'
description: 'list 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: -> action: ->
Promise = require('bluebird') Promise = require('bluebird')
drivelist = require('drivelist') drivelist = require('drivelist')
driveListAsync = Promise.promisify(drivelist.list) driveListAsync = Promise.promisify(drivelist.list)
chalk = require('chalk') chalk = require('chalk')
visuals = require('resin-cli-visuals')
formatDrive = (drive) -> formatDrive = (drive) ->
size = drive.size / 1000000000 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 = -> getDrives = ->
driveListAsync().then (drives) -> driveListAsync().then (drives) ->
@ -39,5 +48,9 @@ exports.availableDrives =
if not drives.length if not drives.length
console.error("#{chalk.red('x')} No available drives were detected, plug one in!") console.error("#{chalk.red('x')} No available drives were detected, plug one in!")
return return
drives.forEach (drive) ->
console.log(formatDrive(drive)) console.log visuals.table.horizontal drives.map(formatDrive), [
'device'
'size'
'description'
]