balena-cli/lib/actions/keys.coffee
Juan Cruz Viotti 210680c9c9 Lazy load command actions dependencies
In my system (MBPr 13), printing the current version takes over 2
seconds:

```sh
$ time ./bin/resin version
2.4.0
./bin/resin version  1.37s user 0.19s system 73% cpu 2.130 total
```

The CLI takes almost all of these time to parse the dependency tree
before returning control over the actually called command.

To mitigate this problem, we only require the NPM dependencies a command
requires when executing such command, and thus prevent dependencies from
being required and parsed unnecessary.

After this improvement, printing the original example (`resin version`)
returns in less than a second (2x improvement):

```sh
$ time ./bin/resin version
2.4.0
./bin/resin version  0.88s user 0.09s system 102% cpu 0.938 total
```
2015-12-07 11:48:54 -03:00

113 lines
2.7 KiB
CoffeeScript

commandOptions = require('./command-options')
exports.list =
signature: 'keys'
description: 'list all ssh keys'
help: '''
Use this command to list all your SSH keys.
Examples:
$ resin keys
'''
permission: 'user'
action: (params, options, done) ->
resin = require('resin-sdk')
visuals = require('resin-cli-visuals')
resin.models.key.getAll().then (keys) ->
console.log visuals.table.horizontal keys, [
'id'
'title'
]
.nodeify(done)
exports.info =
signature: 'key <id>'
description: 'list a single ssh key'
help: '''
Use this command to show information about a single SSH key.
Examples:
$ resin key 17
'''
permission: 'user'
action: (params, options, done) ->
resin = require('resin-sdk')
visuals = require('resin-cli-visuals')
resin.models.key.get(params.id).then (key) ->
console.log visuals.table.vertical key, [
'id'
'title'
]
# Since the public key string is long, it might
# wrap to lines below, causing the table layout to break.
# See https://github.com/resin-io/resin-cli/issues/151
console.log('\n' + key.public_key)
.nodeify(done)
exports.remove =
signature: 'key rm <id>'
description: 'remove a ssh key'
help: '''
Use this command to remove a SSH key from resin.io.
Notice this command asks for confirmation interactively.
You can avoid this by passing the `--yes` boolean option.
Examples:
$ resin key rm 17
$ resin key rm 17 --yes
'''
options: [ commandOptions.yes ]
permission: 'user'
action: (params, options, done) ->
resin = require('resin-sdk')
events = require('resin-cli-events')
patterns = require('../utils/patterns')
patterns.confirm(options.yes, 'Are you sure you want to delete the key?').then ->
resin.models.key.remove(params.id)
.tap ->
events.send('publicKey.delete', id: params.id)
.nodeify(done)
exports.add =
signature: 'key add <name> [path]'
description: 'add a SSH key to resin.io'
help: '''
Use this command to associate a new SSH key with your account.
If `path` is omitted, the command will attempt
to read the SSH key from stdin.
Examples:
$ resin key add Main ~/.ssh/id_rsa.pub
$ cat ~/.ssh/id_rsa.pub | resin key add Main
'''
permission: 'user'
action: (params, options, done) ->
_ = require('lodash')
Promise = require('bluebird')
fs = Promise.promisifyAll(require('fs'))
capitano = require('capitano')
resin = require('resin-sdk')
events = require('resin-cli-events')
Promise.try ->
return fs.readFileAsync(params.path, encoding: 'utf8') if params.path?
Promise.fromNode (callback) ->
capitano.utils.getStdin (data) ->
return callback(null, data)
.then(_.partial(resin.models.key.create, params.name))
.tap ->
events.send('publicKey.create')
.nodeify(done)