Implement stdin support for key add command

This commit is contained in:
Juan Cruz Viotti 2014-12-22 10:30:21 -04:00
parent a8678f419b
commit 893d836937
3 changed files with 37 additions and 5 deletions

View File

@ -39,15 +39,18 @@ exports.add = permissions.user (params) ->
async.waterfall [
(callback) ->
fs.readFile(params.path, encoding: 'utf8', callback)
if params.path?
fs.readFile(params.path, encoding: 'utf8', callback)
else
helpers.readStdin(callback)
(contents, callback) ->
contents = contents.trim()
(key, callback) ->
key = key.trim()
url = resin.settings.get('urls.keys')
data =
title: params.name
key: contents
key: key
resin.server.post(url, data, callback)
], (error) ->

View File

@ -288,9 +288,20 @@ capitano.command
'''
action: actions.keys.list
# TODO: Write help page for this command
capitano.command
signature: 'key add <name> <path>'
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
'''
action: actions.keys.add
capitano.command

View File

@ -6,3 +6,21 @@ exports.isDeviceUUIDValid = (uuid, callback) ->
return callback?(error) if error?
uuidExists = _.findWhere(devices, { uuid })?
return callback(null, uuidExists)
# TODO: Find a sane way to test streams
exports.readStdin = (callback) ->
stdin = process.stdin
stdin.resume()
stdin.setEncoding('utf8')
result = []
stdin.on('error', callback)
stdin.on 'data', (chunk) ->
result.push(chunk)
stdin.on 'end', ->
result = result.join()
return callback(null, result)