mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-19 05:37:51 +00:00
Merge pull request #159 from resin-io/jviotti/refactor/device
Refactor device actions to use promises
This commit is contained in:
commit
89bd861d8e
@ -1,5 +1,5 @@
|
||||
(function() {
|
||||
var Promise, _, async, capitano, commandOptions, deviceConfig, form, htmlToText, image, inject, manager, pine, registerDevice, resin, vcs, visuals;
|
||||
var Promise, _, async, capitano, commandOptions, deviceConfig, form, helpers, htmlToText, image, inject, manager, pine, registerDevice, resin, vcs, visuals;
|
||||
|
||||
Promise = require('bluebird');
|
||||
|
||||
@ -31,6 +31,8 @@
|
||||
|
||||
htmlToText = require('html-to-text');
|
||||
|
||||
helpers = require('../utils/helpers');
|
||||
|
||||
commandOptions = require('./command-options');
|
||||
|
||||
exports.list = {
|
||||
@ -40,19 +42,14 @@
|
||||
options: [commandOptions.optionalApplication],
|
||||
permission: 'user',
|
||||
action: function(params, options, done) {
|
||||
var getFunction;
|
||||
if (options.application != null) {
|
||||
getFunction = _.partial(resin.models.device.getAllByApplication, options.application);
|
||||
} else {
|
||||
getFunction = resin.models.device.getAll;
|
||||
}
|
||||
return getFunction(function(error, devices) {
|
||||
if (error != null) {
|
||||
return done(error);
|
||||
return Promise["try"](function() {
|
||||
if (options.application != null) {
|
||||
return resin.models.device.getAllByApplication(options.application);
|
||||
}
|
||||
console.log(visuals.table.horizontal(devices, ['id', 'name', 'device_type', 'is_online', 'application_name', 'status', 'last_seen']));
|
||||
return done(null, devices);
|
||||
});
|
||||
return resin.models.device.getAll();
|
||||
}).tap(function(devices) {
|
||||
return console.log(visuals.table.horizontal(devices, ['id', 'name', 'device_type', 'is_online', 'application_name', 'status', 'last_seen']));
|
||||
}).nodeify(done);
|
||||
}
|
||||
};
|
||||
|
||||
@ -78,24 +75,12 @@
|
||||
options: [commandOptions.yes],
|
||||
permission: 'user',
|
||||
action: function(params, options, done) {
|
||||
return async.waterfall([
|
||||
function(callback) {
|
||||
if (options.yes) {
|
||||
return callback(null, true);
|
||||
} else {
|
||||
return form.ask({
|
||||
message: 'Are you sure you want to delete the device?',
|
||||
type: 'confirm',
|
||||
"default": false
|
||||
}).nodeify(callback);
|
||||
}
|
||||
}, function(confirmed, callback) {
|
||||
if (!confirmed) {
|
||||
return callback();
|
||||
}
|
||||
return resin.models.device.remove(params.uuid).nodeify(callback);
|
||||
return helpers.confirm(options.yes, 'Are you sure you want to delete the device?').then(function(confirmed) {
|
||||
if (!confirmed) {
|
||||
return;
|
||||
}
|
||||
], done);
|
||||
return resin.models.device.remove(params.uuid);
|
||||
}).nodeify(done);
|
||||
}
|
||||
};
|
||||
|
||||
@ -115,19 +100,15 @@
|
||||
help: 'Use this command to rename a device.\n\nIf you omit the name, you\'ll get asked for it interactively.\n\nExamples:\n\n $ resin device rename 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9 MyPi\n $ resin device rename 7cf02a62a3a84440b1bb5579a3d57469148943278630b17e7fc6c4f7b465c9',
|
||||
permission: 'user',
|
||||
action: function(params, options, done) {
|
||||
return async.waterfall([
|
||||
function(callback) {
|
||||
if (!_.isEmpty(params.newName)) {
|
||||
return callback(null, params.newName);
|
||||
}
|
||||
return form.ask({
|
||||
message: 'How do you want to name this device?',
|
||||
type: 'input'
|
||||
}).nodeify(callback);
|
||||
}, function(newName, callback) {
|
||||
return resin.models.device.rename(params.uuid, newName).nodeify(callback);
|
||||
return Promise["try"](function() {
|
||||
if (!_.isEmpty(params.newName)) {
|
||||
return params.newName;
|
||||
}
|
||||
], done);
|
||||
return form.ask({
|
||||
message: 'How do you want to name this device?',
|
||||
type: 'input'
|
||||
});
|
||||
}).then(_.partial(resin.models.device.rename, params.uuid)).nodeify(done);
|
||||
}
|
||||
};
|
||||
|
||||
@ -137,9 +118,7 @@
|
||||
help: 'Use this command to get the list of all supported devices\n\nExamples:\n\n $ resin devices supported',
|
||||
permission: 'user',
|
||||
action: function(params, options, done) {
|
||||
return resin.models.device.getSupportedDeviceTypes().each(function(device) {
|
||||
return console.log(device);
|
||||
}).nodeify(done);
|
||||
return resin.models.device.getSupportedDeviceTypes().each(console.log).nodeify(done);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -13,6 +13,7 @@ pine = require('resin-pine')
|
||||
deviceConfig = require('resin-device-config')
|
||||
form = require('resin-cli-form')
|
||||
htmlToText = require('html-to-text')
|
||||
helpers = require('../utils/helpers')
|
||||
|
||||
commandOptions = require('./command-options')
|
||||
|
||||
@ -34,13 +35,12 @@ exports.list =
|
||||
options: [ commandOptions.optionalApplication ]
|
||||
permission: 'user'
|
||||
action: (params, options, done) ->
|
||||
if options.application?
|
||||
getFunction = _.partial(resin.models.device.getAllByApplication, options.application)
|
||||
else
|
||||
getFunction = resin.models.device.getAll
|
||||
Promise.try ->
|
||||
if options.application?
|
||||
return resin.models.device.getAllByApplication(options.application)
|
||||
return resin.models.device.getAll()
|
||||
|
||||
getFunction (error, devices) ->
|
||||
return done(error) if error?
|
||||
.tap (devices) ->
|
||||
console.log visuals.table.horizontal devices, [
|
||||
'id'
|
||||
'name'
|
||||
@ -50,8 +50,7 @@ exports.list =
|
||||
'status'
|
||||
'last_seen'
|
||||
]
|
||||
|
||||
return done(null, devices)
|
||||
.nodeify(done)
|
||||
|
||||
exports.info =
|
||||
signature: 'device <uuid>'
|
||||
@ -87,7 +86,6 @@ exports.info =
|
||||
'is_web_accessible'
|
||||
'note'
|
||||
]
|
||||
|
||||
.nodeify(done)
|
||||
|
||||
exports.remove =
|
||||
@ -107,22 +105,10 @@ exports.remove =
|
||||
options: [ commandOptions.yes ]
|
||||
permission: 'user'
|
||||
action: (params, options, done) ->
|
||||
async.waterfall [
|
||||
|
||||
(callback) ->
|
||||
if options.yes
|
||||
return callback(null, true)
|
||||
else
|
||||
form.ask
|
||||
message: 'Are you sure you want to delete the device?'
|
||||
type: 'confirm'
|
||||
default: false
|
||||
.nodeify(callback)
|
||||
|
||||
(confirmed, callback) ->
|
||||
return callback() if not confirmed
|
||||
resin.models.device.remove(params.uuid).nodeify(callback)
|
||||
], done
|
||||
helpers.confirm(options.yes, 'Are you sure you want to delete the device?').then (confirmed) ->
|
||||
return if not confirmed
|
||||
resin.models.device.remove(params.uuid)
|
||||
.nodeify(done)
|
||||
|
||||
exports.identify =
|
||||
signature: 'device identify <uuid>'
|
||||
@ -155,21 +141,15 @@ exports.rename =
|
||||
'''
|
||||
permission: 'user'
|
||||
action: (params, options, done) ->
|
||||
async.waterfall [
|
||||
Promise.try ->
|
||||
return params.newName if not _.isEmpty(params.newName)
|
||||
|
||||
(callback) ->
|
||||
if not _.isEmpty(params.newName)
|
||||
return callback(null, params.newName)
|
||||
form.ask
|
||||
message: 'How do you want to name this device?'
|
||||
type: 'input'
|
||||
|
||||
form.ask
|
||||
message: 'How do you want to name this device?'
|
||||
type: 'input'
|
||||
.nodeify(callback)
|
||||
|
||||
(newName, callback) ->
|
||||
resin.models.device.rename(params.uuid, newName).nodeify(callback)
|
||||
|
||||
], done
|
||||
.then(_.partial(resin.models.device.rename, params.uuid))
|
||||
.nodeify(done)
|
||||
|
||||
exports.supported =
|
||||
signature: 'devices supported'
|
||||
@ -183,9 +163,7 @@ exports.supported =
|
||||
'''
|
||||
permission: 'user'
|
||||
action: (params, options, done) ->
|
||||
resin.models.device.getSupportedDeviceTypes().each (device) ->
|
||||
console.log(device)
|
||||
.nodeify(done)
|
||||
resin.models.device.getSupportedDeviceTypes().each(console.log).nodeify(done)
|
||||
|
||||
exports.await =
|
||||
signature: 'device await <uuid>'
|
||||
|
Loading…
Reference in New Issue
Block a user