From ba318f293989c641a4ebcff29ea3970f00a80dc0 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Wed, 11 Nov 2015 15:00:02 -0400 Subject: [PATCH] Implement device move command This command allows to user to move a device to another application he owns. --- build/actions/device.js | 19 +++++++++++++++++++ build/app.js | 2 ++ build/utils/patterns.js | 14 +++++++------- doc/cli.markdown | 18 ++++++++++++++++++ lib/actions/device.coffee | 28 ++++++++++++++++++++++++++++ lib/app.coffee | 1 + lib/utils/patterns.coffee | 14 ++++++++------ package.json | 2 +- 8 files changed, 84 insertions(+), 14 deletions(-) diff --git a/build/actions/device.js b/build/actions/device.js index 3b828242..78c5019d 100644 --- a/build/actions/device.js +++ b/build/actions/device.js @@ -139,6 +139,25 @@ } }; + exports.move = { + signature: 'device move ', + description: 'move a device to another application', + help: 'Use this command to move a device to another application you own.\n\nIf you omit the application, you\'ll get asked for it interactively.\n\nExamples:\n\n $ resin device move 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9\n $ resin device move 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9 --application MyNewApp', + permission: 'user', + options: [commandOptions.optionalApplication], + action: function(params, options, done) { + return resin.models.device.get(params.uuid).then(function(device) { + return options.application || patterns.selectApplication(function(application) { + return _.all([application.device_type === device.device_type, device.application_name !== application.app_name]); + }); + }).tap(function(application) { + return resin.models.device.move(params.uuid, application); + }).then(function(application) { + return console.info(params.uuid + " was moved to " + application); + }).nodeify(done); + } + }; + exports.init = { signature: 'device init', description: 'initialise a device with resin os', diff --git a/build/app.js b/build/app.js index 6b140b76..f133dac5 100644 --- a/build/app.js +++ b/build/app.js @@ -70,6 +70,8 @@ capitano.command(actions.device.register); + capitano.command(actions.device.move); + capitano.command(actions.device.info); capitano.command(actions.notes.set); diff --git a/build/utils/patterns.js b/build/utils/patterns.js index 9d41ae0c..8ce9937f 100644 --- a/build/utils/patterns.js +++ b/build/utils/patterns.js @@ -42,17 +42,17 @@ }); }; - exports.selectApplication = function() { + exports.selectApplication = function(filter) { return resin.models.application.hasAny().then(function(hasAnyApplications) { if (!hasAnyApplications) { throw new Error('You don\'t have any applications'); } - return resin.models.application.getAll().then(function(applications) { - return form.ask({ - message: 'Select an application', - type: 'list', - choices: _.pluck(applications, 'app_name') - }); + return resin.models.application.getAll(); + }).filter(filter || _.constant(true)).then(function(applications) { + return form.ask({ + message: 'Select an application', + type: 'list', + choices: _.pluck(applications, 'app_name') }); }); }; diff --git a/doc/cli.markdown b/doc/cli.markdown index 610e58ce..9408ccc9 100644 --- a/doc/cli.markdown +++ b/doc/cli.markdown @@ -37,6 +37,7 @@ Now you have access to all the commands referenced below. - [device rm <uuid>](#device-rm-60-uuid-62-) - [device identify <uuid>](#device-identify-60-uuid-62-) - [device rename <uuid> [newName]](#device-rename-60-uuid-62-newname-) + - [device move <uuid>](#device-move-60-uuid-62-) - [device init](#device-init) - Environment Variables @@ -288,6 +289,23 @@ Examples: $ resin device rename 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9 MyPi $ resin device rename 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9 +## device move <uuid> + +Use this command to move a device to another application you own. + +If you omit the application, you'll get asked for it interactively. + +Examples: + + $ resin device move 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9 + $ resin device move 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9 --application MyNewApp + +### Options + +#### --application, --a,app, --a,app <application> + +application name + ## device init Use this command to download the OS image of a certain application and write it to an SD Card. diff --git a/lib/actions/device.coffee b/lib/actions/device.coffee index ecb51392..325287c2 100644 --- a/lib/actions/device.coffee +++ b/lib/actions/device.coffee @@ -181,6 +181,34 @@ exports.rename = events.send('device.rename', device: params.uuid) .nodeify(done) +exports.move = + signature: 'device move ' + description: 'move a device to another application' + help: ''' + Use this command to move a device to another application you own. + + If you omit the application, you'll get asked for it interactively. + + Examples: + + $ resin device move 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9 + $ resin device move 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9 --application MyNewApp + ''' + permission: 'user' + options: [ commandOptions.optionalApplication ] + action: (params, options, done) -> + resin.models.device.get(params.uuid).then (device) -> + return options.application or patterns.selectApplication (application) -> + return _.all [ + application.device_type is device.device_type + device.application_name isnt application.app_name + ] + .tap (application) -> + return resin.models.device.move(params.uuid, application) + .then (application) -> + console.info("#{params.uuid} was moved to #{application}") + .nodeify(done) + exports.init = signature: 'device init' description: 'initialise a device with resin os' diff --git a/lib/app.coffee b/lib/app.coffee index 24f859e7..caf101a3 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -47,6 +47,7 @@ capitano.command(actions.device.init) capitano.command(actions.device.remove) capitano.command(actions.device.identify) capitano.command(actions.device.register) +capitano.command(actions.device.move) capitano.command(actions.device.info) # ---------- Notes Module ---------- diff --git a/lib/utils/patterns.coffee b/lib/utils/patterns.coffee index 308023c3..e87f0aa4 100644 --- a/lib/utils/patterns.coffee +++ b/lib/utils/patterns.coffee @@ -24,16 +24,18 @@ exports.confirm = (yesOption, message) -> if not confirmed throw new Error('Aborted') -exports.selectApplication = -> +exports.selectApplication = (filter) -> resin.models.application.hasAny().then (hasAnyApplications) -> if not hasAnyApplications throw new Error('You don\'t have any applications') - resin.models.application.getAll().then (applications) -> - return form.ask - message: 'Select an application' - type: 'list' - choices: _.pluck(applications, 'app_name') + return resin.models.application.getAll() + .filter(filter or _.constant(true)) + .then (applications) -> + return form.ask + message: 'Select an application' + type: 'list' + choices: _.pluck(applications, 'app_name') exports.selectOrCreateApplication = -> resin.models.application.hasAny().then (hasAnyApplications) -> diff --git a/package.json b/package.json index aa7009c6..ea5108c9 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,7 @@ "resin-image": "^1.1.4", "resin-image-manager": "^3.2.2", "resin-pine": "^1.3.0", - "resin-sdk": "^4.0.0", + "resin-sdk": "^4.1.1", "resin-settings-client": "^3.1.0", "resin-vcs": "^2.0.0", "rimraf": "^2.4.3",