Implement device rename command

This commit is contained in:
Juan Cruz Viotti 2014-12-19 10:05:54 -04:00
parent 6cfe2bdc49
commit ff152c987d
4 changed files with 69 additions and 0 deletions

View File

@ -1,3 +1,5 @@
_ = require('lodash')
async = require('async')
resin = require('../resin')
ui = require('../ui')
permissions = require('../permissions/permissions')
@ -48,3 +50,20 @@ exports.remove = permissions.user (params, options) ->
exports.identify = permissions.user (params) ->
resin.models.device.identify params.uuid, (error) ->
resin.errors.handle(error) if error?
# TODO: This action doesn't return any error
# if trying to rename a device that does not
# exists. This is being fixed server side.
exports.rename = permissions.user (params) ->
async.waterfall [
(callback) ->
if params.name? and not _.isEmpty(params.name)
return callback(null, params.name)
ui.widgets.ask('How do you want to name this device?', callback)
(name, callback) ->
resin.models.device.rename(params.id, name, callback)
], (error) ->
resin.errors.handle(error) if error?

View File

@ -195,6 +195,20 @@ capitano.command
'''
action: actions.device.list
capitano.command
signature: 'device rename <id> [name]'
description: 'rename a resin device'
help: '''
Use this command to rename a device.
If you omit the name, you'll get asked for it interactively.
Examples:
$ resin device rename 317 MyPi
$ resin device rename 317
'''
action: actions.device.rename
capitano.command
signature: 'device <id>'
description: 'list a single device'

View File

@ -116,3 +116,27 @@ exports.remove = (id, callback) ->
#
exports.identify = (uuid, callback) ->
server.post(settings.get('urls.identify'), { uuid }, _.unary(callback))
# Rename device
#
# @param {String, Number} id device id
# @param {String} name the device new name
# @param {Function} callback callback(error)
#
# @example Rename device
# resin.models.device.rename 317, 'NewName', (error) ->
# throw error if error?
# console.log("Device has been renamed!")
#
exports.rename = (id, name, callback) ->
return pine.patch
resource: 'device'
id: id
data:
name: name
.then ->
return callback()
.catch (error) ->
return callback(error)

View File

@ -38,3 +38,15 @@ exports.confirmRemoval = (name, callback) ->
}
], (response) ->
return callback(null, response.confirmed)
exports.ask = (question, callback) ->
inquirer.prompt [
{
type: 'input'
name: 'answer'
message: question
validate: (input) ->
return _.isString(input) and not _.isEmpty(input)
}
], (response) ->
return callback(null, response.answer)