From 2ef56a9a3f1e0c7243a3b098703971e7a6ccf4f9 Mon Sep 17 00:00:00 2001 From: Pagan Gazzard Date: Tue, 18 Apr 2017 20:19:04 -0700 Subject: [PATCH] Added a `--device-api-key` option to the `device register` command. Change-Type: minor --- build/actions/device.js | 24 +++++++++++++++--------- doc/cli.markdown | 6 ++++++ lib/actions/device.coffee | 35 +++++++++++++++++++++++++---------- package.json | 2 +- 4 files changed, 47 insertions(+), 20 deletions(-) diff --git a/build/actions/device.js b/build/actions/device.js index 3df195a4..29d9aab0 100644 --- a/build/actions/device.js +++ b/build/actions/device.js @@ -84,7 +84,7 @@ exports.supported = { exports.register = { signature: 'device register ', description: 'register a device', - help: 'Use this command to register a device to an application.\n\nExamples:\n\n $ resin device register MyApp', + help: 'Use this command to register a device to an application.\n\nExamples:\n\n $ resin device register MyApp\n $ resin device register MyApp --uuid \n $ resin device register MyApp --uuid --device-api-key ', permission: 'user', options: [ { @@ -92,19 +92,25 @@ exports.register = { description: 'custom uuid', parameter: 'uuid', alias: 'u' + }, { + signature: 'deviceApiKey', + description: 'custom device key', + parameter: 'device-api-key', + alias: 'k' } ], action: function(params, options, done) { - var Promise, resin; + var Promise, ref, ref1, resin; Promise = require('bluebird'); resin = require('resin-sdk-preconfigured'); - return resin.models.application.get(params.application).then(function(application) { - return Promise["try"](function() { - return options.uuid || resin.models.device.generateUniqueKey(); - }).then(function(uuid) { - console.info("Registering to " + application.app_name + ": " + uuid); - return resin.models.device.register(application.app_name, uuid); - }); + return Promise.join(resin.models.application.get(params.application), (ref = options.uuid) != null ? ref : resin.models.device.generateUniqueKey(), (ref1 = options.deviceApiKey) != null ? ref1 : resin.models.device.generateUniqueKey(), function(application, uuid, deviceApiKey) { + console.info("Registering to " + application.app_name + ": " + uuid); + if (options.deviceApiKey == null) { + console.info("Using generated device api key: " + deviceApiKey); + } else { + console.info('Using provided device api key'); + } + return resin.models.device.register(application.app_name, uuid, deviceApiKey); }).get('uuid').nodeify(done); } }; diff --git a/doc/cli.markdown b/doc/cli.markdown index d1b39ec5..fb95897b 100644 --- a/doc/cli.markdown +++ b/doc/cli.markdown @@ -334,6 +334,8 @@ Use this command to register a device to an application. Examples: $ resin device register MyApp + $ resin device register MyApp --uuid + $ resin device register MyApp --uuid --device-api-key ### Options @@ -341,6 +343,10 @@ Examples: custom uuid +#### --deviceApiKey, -k <device-api-key> + +custom device key + ## device rm <uuid> Use this command to remove a device from resin.io. diff --git a/lib/actions/device.coffee b/lib/actions/device.coffee index 8e4ddb70..6a6ab11b 100644 --- a/lib/actions/device.coffee +++ b/lib/actions/device.coffee @@ -134,25 +134,40 @@ exports.register = Examples: $ resin device register MyApp + $ resin device register MyApp --uuid + $ resin device register MyApp --uuid --device-api-key ''' permission: 'user' options: [ - signature: 'uuid' - description: 'custom uuid' - parameter: 'uuid' - alias: 'u' + { + signature: 'uuid' + description: 'custom uuid' + parameter: 'uuid' + alias: 'u' + } + { + signature: 'deviceApiKey' + description: 'custom device key' + parameter: 'device-api-key' + alias: 'k' + } ] action: (params, options, done) -> Promise = require('bluebird') resin = require('resin-sdk-preconfigured') - resin.models.application.get(params.application).then (application) -> - - Promise.try -> - return options.uuid or resin.models.device.generateUniqueKey() - .then (uuid) -> + Promise.join( + resin.models.application.get(params.application) + options.uuid ? resin.models.device.generateUniqueKey() + options.deviceApiKey ? resin.models.device.generateUniqueKey() + (application, uuid, deviceApiKey) -> console.info("Registering to #{application.app_name}: #{uuid}") - return resin.models.device.register(application.app_name, uuid) + if not options.deviceApiKey? + console.info("Using generated device api key: #{deviceApiKey}") + else + console.info('Using provided device api key') + return resin.models.device.register(application.app_name, uuid, deviceApiKey) + ) .get('uuid') .nodeify(done) diff --git a/package.json b/package.json index 7474a172..07125530 100644 --- a/package.json +++ b/package.json @@ -105,4 +105,4 @@ "optionalDependencies": { "removedrive": "^1.0.0" } -} \ No newline at end of file +}