mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-04-07 11:26:41 +00:00
Merge pull request #158 from resin-io/jviotti/refactor/keys
Refactor keys action to use promises
This commit is contained in:
commit
031168ceed
@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -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 <name> [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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user