2014-11-20 18:00:39 +00:00
|
|
|
_ = require('lodash')
|
2014-12-09 18:35:45 +00:00
|
|
|
_.str = require('underscore.string')
|
2014-12-19 17:06:58 +00:00
|
|
|
async = require('async')
|
|
|
|
fs = require('fs')
|
2015-01-08 12:04:37 +00:00
|
|
|
resin = require('resin-sdk')
|
2014-11-20 18:00:39 +00:00
|
|
|
helpers = require('../helpers/helpers')
|
2015-01-21 13:50:19 +00:00
|
|
|
visuals = require('resin-cli-visuals')
|
2015-01-15 17:10:14 +00:00
|
|
|
commandOptions = require('./command-options')
|
2014-11-20 17:02:29 +00:00
|
|
|
|
2015-01-15 17:10:14 +00:00
|
|
|
exports.list =
|
|
|
|
signature: 'keys'
|
|
|
|
description: 'list all ssh keys'
|
|
|
|
help: '''
|
|
|
|
Use this command to list all your SSH keys.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
$ resin keys
|
|
|
|
'''
|
2015-01-16 12:34:59 +00:00
|
|
|
permission: 'user'
|
|
|
|
action: (params, options, done) ->
|
2015-01-15 17:10:14 +00:00
|
|
|
resin.models.key.getAll (error, keys) ->
|
|
|
|
return done(error) if error?
|
2015-01-22 17:06:02 +00:00
|
|
|
console.log visuals.widgets.table.horizontal keys, [ 'id', 'title' ]
|
2015-01-15 17:10:14 +00:00
|
|
|
return 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
|
|
|
|
'''
|
2015-01-16 12:34:59 +00:00
|
|
|
permission: 'user'
|
|
|
|
action: (params, options, done) ->
|
2015-01-15 17:10:14 +00:00
|
|
|
resin.models.key.get params.id, (error, key) ->
|
|
|
|
return done(error) if error?
|
2015-02-03 18:54:07 +00:00
|
|
|
|
|
|
|
# TODO: Move this chop functionality to resin.models.key
|
2015-01-15 17:10:14 +00:00
|
|
|
key.public_key = '\n' + _.str.chop(key.public_key, resin.settings.get('sshKeyWidth')).join('\n')
|
2015-01-22 17:06:02 +00:00
|
|
|
console.log(visuals.widgets.table.vertical(key, [ 'id', 'title', 'public_key' ]))
|
2015-01-15 17:10:14 +00:00
|
|
|
return 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 ]
|
2015-01-16 12:34:59 +00:00
|
|
|
permission: 'user'
|
|
|
|
action: (params, options, done) ->
|
2015-01-21 13:50:19 +00:00
|
|
|
visuals.patterns.remove 'key', options.yes, (callback) ->
|
2015-01-15 17:10:14 +00:00
|
|
|
resin.models.key.remove(params.id, callback)
|
|
|
|
, 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
|
|
|
|
'''
|
2015-01-16 12:34:59 +00:00
|
|
|
permission: 'user'
|
|
|
|
action: (params, options, done) ->
|
2015-01-15 17:10:14 +00:00
|
|
|
async.waterfall [
|
|
|
|
|
|
|
|
(callback) ->
|
|
|
|
if params.path?
|
|
|
|
fs.readFile(params.path, encoding: 'utf8', callback)
|
|
|
|
else
|
|
|
|
helpers.readStdin(callback)
|
|
|
|
|
|
|
|
(key, callback) ->
|
|
|
|
resin.models.key.create(params.name, key, callback)
|
|
|
|
|
|
|
|
], done
|