From 4b20ba435d7649328627b0a73c72a3459ce96523 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Thu, 20 Nov 2014 14:00:39 -0400 Subject: [PATCH] Implement key info command --- lib/actions/keys.coffee | 17 +++++++++++++++++ lib/app.coffee | 1 + lib/config.coffee | 1 + lib/models/key.coffee | 5 +++++ 4 files changed, 24 insertions(+) create mode 100644 lib/models/key.coffee diff --git a/lib/actions/keys.coffee b/lib/actions/keys.coffee index af951209..4390a5c0 100644 --- a/lib/actions/keys.coffee +++ b/lib/actions/keys.coffee @@ -1,6 +1,9 @@ +_ = require('lodash') server = require('../server/server') authHooks = require('../hooks/auth') table = require('../table/table') +helpers = require('../helpers/helpers') +keyModel = require('../models/key') config = require('../config') exports.list = authHooks.failIfNotLoggedIn -> @@ -10,3 +13,17 @@ exports.list = authHooks.failIfNotLoggedIn -> delete key.public_key return key , [ 'ID', 'Title' ] + +exports.info = authHooks.failIfNotLoggedIn (id) -> + + # TODO: We don't have a way to query a single ssh key yet. + # As a workaround, we request all of them, and filter + # the one we need. Fix once we have a better way. + server.get config.urls.keys, (error, response, keys) -> + throw error if error? + key = _.findWhere(keys, { id }) + if not key? + throw new Error("Key #{id} doesn't exists") + + key.public_key = '\n' + helpers.formatLongString(key.public_key, config.sshKeyWidth) + console.log(table.vertical(key, _.identity, [ 'ID', 'Title', 'Public Key' ])) diff --git a/lib/app.coffee b/lib/app.coffee index 6b4f79c4..929a5ee6 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -35,6 +35,7 @@ yargs.command('version', version.version) # ---------- Keys Module ---------- keys = require('./actions/keys') yargs.command('keys', keys.list) +yargs.command('key ', keys.info) data.prefix.set config.dataPrefix, (error) -> throw error if error? diff --git a/lib/config.coffee b/lib/config.coffee index 1bbc94e9..7d0e6dd7 100644 --- a/lib/config.coffee +++ b/lib/config.coffee @@ -11,6 +11,7 @@ config = # TODO: Check if not running on UNIX environment # and add a custom path accordingly dataPrefix: path.join(process.env.HOME, '.resin') + sshKeyWidth: 43 config.urls = signup: '/signup' diff --git a/lib/models/key.coffee b/lib/models/key.coffee new file mode 100644 index 00000000..356e36a9 --- /dev/null +++ b/lib/models/key.coffee @@ -0,0 +1,5 @@ +canvas = require('./_canvas') + +exports.getAll = -> + return canvas.get + resource: 'user__has__public_key'