balena-cli/lib/actions/keys.coffee

31 lines
1.0 KiB
CoffeeScript
Raw Normal View History

2014-11-20 18:00:39 +00:00
_ = require('lodash')
2014-11-20 17:02:29 +00:00
server = require('../server/server')
authHooks = require('../hooks/auth')
table = require('../table/table')
2014-11-20 18:00:39 +00:00
helpers = require('../helpers/helpers')
keyModel = require('../models/key')
2014-11-20 17:02:29 +00:00
config = require('../config')
exports.list = authHooks.failIfNotLoggedIn ->
server.get config.urls.keys, (error, response, keys) ->
throw error if error?
console.log table.horizontal keys, (key) ->
delete key.public_key
return key
, [ 'ID', 'Title' ]
2014-11-20 18:00:39 +00:00
exports.info = authHooks.failIfNotLoggedIn (id) ->
2014-11-21 13:23:02 +00:00
id = _.parseInt(id)
2014-11-20 18:00:39 +00:00
# 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' ]))