balena-cli/lib/actions/keys.coffee
2014-11-26 13:26:01 -04:00

35 lines
1.3 KiB
CoffeeScript

_ = require('lodash')
resin = require('../resin')
authHooks = require('../hooks/auth')
patterns = require('../patterns/patterns')
helpers = require('../helpers/helpers')
config = require('../config')
exports.list = authHooks.failIfNotLoggedIn ->
resin.server.get config.urls.keys, (error, response, keys) ->
resin.errors.handle(error) if error?
resin.log.out resin.ui.widgets.table.horizontal keys, (key) ->
delete key.public_key
return key
, [ 'ID', 'Title' ]
exports.info = authHooks.failIfNotLoggedIn (id) ->
id = _.parseInt(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.
resin.server.get config.urls.keys, (error, response, keys) ->
resin.errors.handle(error) if error?
key = _.findWhere(keys, { id })
if not key?
resin.errors.handle(new resin.errors.NotFound("key #{id}"))
key.public_key = '\n' + helpers.formatLongString(key.public_key, config.sshKeyWidth)
resin.log.out(resin.ui.widgets.table.vertical(key, _.identity, [ 'ID', 'Title', 'Public Key' ]))
exports.remove = authHooks.failIfNotLoggedIn (id, program) ->
patterns.remove 'key', program.parent.yes, (callback) ->
resin.server.delete("/user/keys/#{id}", callback)
, resin.errors.handle