From 09a57889024ca42ffca41db084f8cd1437d76ca0 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Mon, 17 Aug 2015 09:28:10 -0400 Subject: [PATCH] Refactor keys action to use promises --- build/actions/keys.js | 61 +++++++++++++++-------------------------- lib/actions/keys.coffee | 50 ++++++++++++--------------------- 2 files changed, 40 insertions(+), 71 deletions(-) diff --git a/build/actions/keys.js b/build/actions/keys.js index 91a51f40..7c27f165 100644 --- a/build/actions/keys.js +++ b/build/actions/keys.js @@ -1,14 +1,12 @@ (function() { - var _, async, capitano, commandOptions, form, fs, resin, visuals; + var Promise, _, capitano, commandOptions, fs, helpers, resin, visuals; + + Promise = require('bluebird'); + + fs = Promise.promisifyAll(require('fs')); _ = require('lodash'); - _.str = require('underscore.string'); - - async = require('async'); - - fs = require('fs'); - resin = require('resin-sdk'); capitano = require('capitano'); @@ -17,7 +15,7 @@ commandOptions = require('./command-options'); - form = require('resin-cli-form'); + helpers = require('../utils/helpers'); exports.list = { signature: 'keys', @@ -51,24 +49,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 key?', - type: 'confirm', - "default": false - }).nodeify(callback); - } - }, function(confirmed, callback) { - if (!confirmed) { - return callback(); - } - return resin.models.key.remove(params.id).nodeify(callback); + return helpers.confirm(options.yes, 'Are you sure you want to delete the key?').then(function(confirmed) { + if (!confirmed) { + return; } - ], done); + return resin.models.key.remove(params.id); + }).nodeify(done); } }; @@ -78,21 +64,18 @@ help: 'Use this command to associate a new SSH key with your account.\n\nIf `path` is omitted, the command will attempt\nto read the SSH key from stdin.\n\nExamples:\n\n $ resin key add Main ~/.ssh/id_rsa.pub\n $ cat ~/.ssh/id_rsa.pub | resin key add Main', permission: 'user', action: function(params, options, done) { - return async.waterfall([ - function(callback) { - if (params.path != null) { - return fs.readFile(params.path, { - encoding: 'utf8' - }, callback); - } else { - return capitano.utils.getStdin(function(data) { - return callback(null, data); - }); - } - }, function(key, callback) { - return resin.models.key.create(params.name, key).nodeify(callback); + return Promise["try"](function() { + if (params.path != null) { + return fs.readFileAsync(params.path, { + encoding: 'utf8' + }); } - ], done); + return Promise.fromNode(function(callback) { + return capitano.utils.getStdin(function(data) { + return callback(null, data); + }); + }); + }).then(_.partial(resin.models.key.create, params.name)).nodeify(done); } }; diff --git a/lib/actions/keys.coffee b/lib/actions/keys.coffee index c8e92201..e511171d 100644 --- a/lib/actions/keys.coffee +++ b/lib/actions/keys.coffee @@ -1,12 +1,11 @@ +Promise = require('bluebird') +fs = Promise.promisifyAll(require('fs')) _ = require('lodash') -_.str = require('underscore.string') -async = require('async') -fs = require('fs') resin = require('resin-sdk') capitano = require('capitano') visuals = require('resin-cli-visuals') commandOptions = require('./command-options') -form = require('resin-cli-form') +helpers = require('../utils/helpers') exports.list = signature: 'keys' @@ -21,7 +20,10 @@ exports.list = permission: 'user' action: (params, options, done) -> resin.models.key.getAll().then (keys) -> - console.log visuals.table.horizontal keys, [ 'id', 'title' ] + console.log visuals.table.horizontal keys, [ + 'id' + 'title' + ] .nodeify(done) exports.info = @@ -65,22 +67,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 key?' - type: 'confirm' - default: false - .nodeify(callback) - - (confirmed, callback) -> - return callback() if not confirmed - resin.models.key.remove(params.id).nodeify(callback) - ], done + helpers.confirm(options.yes, 'Are you sure you want to delete the key?').then (confirmed) -> + return if not confirmed + resin.models.key.remove(params.id) + .nodeify(done) exports.add = signature: 'key add [path]' @@ -98,16 +88,12 @@ exports.add = ''' permission: 'user' action: (params, options, done) -> - async.waterfall [ + Promise.try -> + return fs.readFileAsync(params.path, encoding: 'utf8') if params.path? - (callback) -> - if params.path? - fs.readFile(params.path, encoding: 'utf8', callback) - else - capitano.utils.getStdin (data) -> - return callback(null, data) + Promise.fromNode (callback) -> + capitano.utils.getStdin (data) -> + return callback(null, data) - (key, callback) -> - resin.models.key.create(params.name, key).nodeify(callback) - - ], done + .then(_.partial(resin.models.key.create, params.name)) + .nodeify(done)