diff --git a/lib/actions/keys.coffee b/lib/actions/keys.coffee index 46745f09..33327f72 100644 --- a/lib/actions/keys.coffee +++ b/lib/actions/keys.coffee @@ -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) -> diff --git a/lib/app.coffee b/lib/app.coffee index e52bc6f0..86237edb 100644 --- a/lib/app.coffee +++ b/lib/app.coffee @@ -288,9 +288,20 @@ capitano.command ''' action: actions.keys.list +# TODO: Write help page for this command capitano.command - signature: 'key add ' + signature: 'key add [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 diff --git a/lib/helpers/helpers.coffee b/lib/helpers/helpers.coffee index b0c61b84..1d6a4030 100644 --- a/lib/helpers/helpers.coffee +++ b/lib/helpers/helpers.coffee @@ -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)