balena-cli/lib/actions/keys.coffee
2014-12-22 15:29:15 -04:00

59 lines
1.6 KiB
CoffeeScript

_ = require('lodash')
_.str = require('underscore.string')
async = require('async')
fs = require('fs')
resin = require('../resin')
helpers = require('../helpers/helpers')
ui = require('../ui')
log = require('../log/log')
permissions = require('../permissions/permissions')
errors = require('../errors/errors')
exports.list = permissions.user ->
resin.models.key.getAll (error, keys) ->
errors.handle(error) if error?
log.out ui.widgets.table.horizontal keys, (key) ->
delete key.public_key
return key
, [ 'ID', 'Title' ]
exports.info = permissions.user (params) ->
resin.models.key.get params.id, (error, key) ->
errors.handle(error) if error?
key.public_key = '\n' + _.str.chop(key.public_key, resin.settings.get('sshKeyWidth')).join('\n')
log.out(ui.widgets.table.vertical(key, _.identity, [ 'ID', 'Title', 'Public Key' ]))
exports.remove = permissions.user (params, options) ->
ui.patterns.remove 'key', options.yes, (callback) ->
resin.models.key.remove(params.id, callback)
, errors.handle
exports.add = permissions.user (params) ->
async.waterfall [
(callback) ->
if params.path?
fs.readFile(params.path, encoding: 'utf8', callback)
else
helpers.readStdin(callback)
(key, callback) ->
key = key.trim()
resin.models.key.create(params.name, key, callback)
], (error) ->
return if not error?
# TODO: Make handle() check the error type
# and accomodate as most as possible to prevent
# this types of checks in client code.
if error.code is 'EISDIR'
error.message = "File is a directory: #{params.path}"
if error.code is 'ENOENT'
error = new Error("File not found: #{params.path}")
errors.handle(error)