balena-cli/lib/actions/keys.coffee

56 lines
1.6 KiB
CoffeeScript
Raw Normal View History

2014-11-20 18:00:39 +00:00
_ = require('lodash')
_.str = require('underscore.string')
2014-12-19 17:06:58 +00:00
async = require('async')
fs = require('fs')
2014-11-26 16:07:10 +00:00
resin = require('../resin')
2014-11-20 18:00:39 +00:00
helpers = require('../helpers/helpers')
2014-12-05 16:00:15 +00:00
ui = require('../ui')
2014-12-22 16:41:14 +00:00
log = require('../log/log')
permissions = require('../permissions/permissions')
2014-12-22 16:47:12 +00:00
errors = require('../errors/errors')
2014-11-20 17:02:29 +00:00
exports.list = permissions.user ->
resin.models.key.getAll (error, keys) ->
2014-12-22 16:47:12 +00:00
errors.handle(error) if error?
log.out ui.widgets.table.horizontal keys, _.identity, [ 'ID', 'Title' ]
2014-11-20 18:00:39 +00:00
exports.info = permissions.user (params) ->
resin.models.key.get params.id, (error, key) ->
2014-12-22 16:47:12 +00:00
errors.handle(error) if error?
2014-11-20 18:00:39 +00:00
key.public_key = '\n' + _.str.chop(key.public_key, resin.settings.get('sshKeyWidth')).join('\n')
2014-12-22 16:41:14 +00:00
log.out(ui.widgets.table.vertical(key, _.identity, [ 'ID', 'Title', 'Public Key' ]))
2014-11-21 17:56:11 +00:00
exports.remove = permissions.user (params, options) ->
ui.patterns.remove 'key', options.yes, (callback) ->
resin.models.key.remove(params.id, callback)
2014-12-22 16:47:12 +00:00
, errors.handle
2014-12-19 17:06:58 +00:00
exports.add = permissions.user (params) ->
async.waterfall [
(callback) ->
if params.path?
fs.readFile(params.path, encoding: 'utf8', callback)
else
helpers.readStdin(callback)
2014-12-19 17:06:58 +00:00
(key, callback) ->
key = key.trim()
resin.models.key.create(params.name, key, callback)
2014-12-19 17:06:58 +00:00
], (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'
2015-01-06 16:46:31 +00:00
error.message = "File is a directory: #{error.path}"
2014-12-19 17:06:58 +00:00
if error.code is 'ENOENT'
2015-01-06 16:46:31 +00:00
error = new Error("File not found: #{error.path}")
2014-12-19 17:06:58 +00:00
2014-12-22 16:47:12 +00:00
errors.handle(error)