balena-cli/lib/actions/keys.coffee

39 lines
1.3 KiB
CoffeeScript
Raw Normal View History

2014-11-20 14:00:39 -04:00
_ = require('lodash')
2014-11-20 13:02:29 -04:00
server = require('../server/server')
authHooks = require('../hooks/auth')
2014-11-21 11:40:37 -04:00
log = require('../log/log')
2014-11-21 13:56:11 -04:00
patterns = require('../patterns/patterns')
2014-11-20 13:02:29 -04:00
table = require('../table/table')
2014-11-20 14:00:39 -04:00
helpers = require('../helpers/helpers')
2014-11-24 14:08:58 -04:00
errors = require('../errors/errors')
2014-11-20 14:00:39 -04:00
keyModel = require('../models/key')
2014-11-20 13:02:29 -04:00
config = require('../config')
exports.list = authHooks.failIfNotLoggedIn ->
server.get config.urls.keys, (error, response, keys) ->
2014-11-24 14:08:58 -04:00
errors.handle(error) if error?
2014-11-21 11:40:37 -04:00
log.out table.horizontal keys, (key) ->
2014-11-20 13:02:29 -04:00
delete key.public_key
return key
, [ 'ID', 'Title' ]
2014-11-20 14:00:39 -04:00
exports.info = authHooks.failIfNotLoggedIn (id) ->
2014-11-21 09:23:02 -04:00
id = _.parseInt(id)
2014-11-20 14:00:39 -04: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) ->
2014-11-24 14:08:58 -04:00
errors.handle(error) if error?
2014-11-20 14:00:39 -04:00
key = _.findWhere(keys, { id })
if not key?
2014-11-24 14:08:58 -04:00
errors.handle(new Error("Key #{id} doesn't exists"))
2014-11-20 14:00:39 -04:00
key.public_key = '\n' + helpers.formatLongString(key.public_key, config.sshKeyWidth)
2014-11-21 11:40:37 -04:00
log.out(table.vertical(key, _.identity, [ 'ID', 'Title', 'Public Key' ]))
2014-11-21 13:56:11 -04:00
exports.remove = authHooks.failIfNotLoggedIn (id, program) ->
patterns.remove 'key', program.parent.yes, (callback) ->
server.delete("/user/keys/#{id}", callback)
2014-11-24 14:08:58 -04:00
, errors.handle