From 80e0f20301bdaca35257dac43b96bb9603797aed Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Thu, 12 Mar 2015 12:03:59 -0400 Subject: [PATCH] Make device init attempt to get application id from current directory --- build/actions/command-options.js | 13 ++++++++++--- build/actions/device.js | 12 ++++++++++-- lib/actions/command-options.coffee | 7 ++++++- lib/actions/device.coffee | 8 +++++++- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/build/actions/command-options.js b/build/actions/command-options.js index f71e1602..46a7677f 100644 --- a/build/actions/command-options.js +++ b/build/actions/command-options.js @@ -1,4 +1,8 @@ (function() { + var _; + + _ = require('lodash'); + exports.yes = { signature: 'yes', description: 'confirm non interactively', @@ -6,14 +10,17 @@ alias: 'y' }; - exports.application = { + exports.optionalApplication = { signature: 'application', parameter: 'application', description: 'application id', - alias: ['a', 'app'], - required: 'You have to specify an application' + alias: ['a', 'app'] }; + exports.application = _.defaults({ + required: 'You have to specify an application' + }, exports.optionalApplication); + exports.network = { signature: 'network', parameter: 'network', diff --git a/build/actions/device.js b/build/actions/device.js index 49a40350..4b10f2c3 100644 --- a/build/actions/device.js +++ b/build/actions/device.js @@ -1,5 +1,5 @@ (function() { - var _, async, commandOptions, osAction, path, resin, visuals; + var _, async, commandOptions, osAction, path, resin, vcs, visuals; _ = require('lodash-contrib'); @@ -11,6 +11,8 @@ visuals = require('resin-cli-visuals'); + vcs = require('resin-vcs'); + commandOptions = require('./command-options'); osAction = require('./os'); @@ -110,12 +112,18 @@ signature: 'device init [device]', description: 'initialise a device with resin os', help: 'Use this command to download the OS image of a certain application and write it to an SD Card.\n\nNote that this command requires admin privileges.\n\nIf `device` is omitted, you will be prompted to select a device interactively.\n\nNotice this command asks for confirmation interactively.\nYou can avoid this by passing the `--yes` boolean option.\n\nYou can quiet the progress bar by passing the `--quiet` boolean option.\n\nYou may have to unmount the device before attempting this operation.\n\nYou need to configure the network type and other settings:\n\nEthernet:\n You can setup the device OS to use ethernet by setting the `--network` option to "ethernet".\n\nWifi:\n You can setup the device OS to use wifi by setting the `--network` option to "wifi".\n If you set "network" to "wifi", you will need to specify the `--ssid` and `--key` option as well.\n\nYou can omit network related options to be asked about them interactively.\n\nExamples:\n\n $ resin device init --application 91\n $ resin device init --application 91 --network ethernet\n $ resin device init /dev/disk2 --application 91 --network wifi --ssid MyNetwork --key secret', - options: [commandOptions.application, commandOptions.network, commandOptions.wifiSsid, commandOptions.wifiKey], + options: [commandOptions.optionalApplication, commandOptions.network, commandOptions.wifiSsid, commandOptions.wifiKey], permission: 'user', action: function(params, options, done) { params.id = options.application; return async.waterfall([ function(callback) { + if (options.application != null) { + return callback(null, options.application); + } + return vcs.getApplicationId(process.cwd(), callback); + }, function(applicationId, callback) { + params.id = applicationId; if (params.device != null) { return callback(null, params.device); } diff --git a/lib/actions/command-options.coffee b/lib/actions/command-options.coffee index cdf02e30..4a3e5b1f 100644 --- a/lib/actions/command-options.coffee +++ b/lib/actions/command-options.coffee @@ -1,15 +1,20 @@ +_ = require('lodash') + exports.yes = signature: 'yes' description: 'confirm non interactively' boolean: true alias: 'y' -exports.application = +exports.optionalApplication = signature: 'application' parameter: 'application' description: 'application id' alias: [ 'a', 'app' ] + +exports.application = _.defaults required: 'You have to specify an application' +, exports.optionalApplication exports.network = signature: 'network' diff --git a/lib/actions/device.coffee b/lib/actions/device.coffee index c39269e6..9024b666 100644 --- a/lib/actions/device.coffee +++ b/lib/actions/device.coffee @@ -3,6 +3,7 @@ path = require('path') async = require('async') resin = require('resin-sdk') visuals = require('resin-cli-visuals') +vcs = require('resin-vcs') commandOptions = require('./command-options') osAction = require('./os') @@ -181,7 +182,7 @@ exports.init = $ resin device init /dev/disk2 --application 91 --network wifi --ssid MyNetwork --key secret ''' options: [ - commandOptions.application + commandOptions.optionalApplication commandOptions.network commandOptions.wifiSsid commandOptions.wifiKey @@ -194,6 +195,11 @@ exports.init = async.waterfall([ (callback) -> + return callback(null, options.application) if options.application? + vcs.getApplicationId(process.cwd(), callback) + + (applicationId, callback) -> + params.id = applicationId return callback(null, params.device) if params.device? visuals.patterns.selectDrive(callback)