Implement key add command

This commit is contained in:
Juan Cruz Viotti 2014-12-19 13:06:58 -04:00
parent ff152c987d
commit 006f3e8aca
4 changed files with 53 additions and 1 deletions

View File

@ -1,5 +1,7 @@
_ = require('lodash')
_.str = require('underscore.string')
async = require('async')
fs = require('fs')
resin = require('../resin')
helpers = require('../helpers/helpers')
ui = require('../ui')
@ -32,3 +34,32 @@ exports.remove = permissions.user (params, options) ->
url = _.template(resin.settings.get('urls.sshKey'), id: params.id)
resin.server.delete(url, callback)
, resin.errors.handle
exports.add = permissions.user (params) ->
async.waterfall [
(callback) ->
fs.readFile(params.path, encoding: 'utf8', callback)
(contents, callback) ->
contents = contents.trim()
url = resin.settings.get('urls.keys')
data =
title: params.name
key: contents
resin.server.post(url, data, callback)
], (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'
error.message = "File is a directory: #{params.path}"
if error.code is 'ENOENT'
error = new resin.errors.FileNotFound(params.path)
resin.errors.handle(error)

View File

@ -276,6 +276,11 @@ capitano.command
'''
action: actions.keys.list
capitano.command
signature: 'key add <name> <path>'
description: 'add a SSH key to resin.io'
action: actions.keys.add
capitano.command
signature: 'key <id>'
description: 'list a single ssh key'

View File

@ -110,6 +110,22 @@ exports.NotAny = class NotAny extends TypedError
# Error code
code: 0
exports.FileNotFound = class FileNotFound extends TypedError
# Construct an File Not Found error
#
# @param {String} filename name of the file that was not found
#
# @example File Not Found error
# throw new resin.errors.FileNotFound('/foo')
# Error: File not found: /foo
#
constructor: (filename) ->
@message = "File not found: #{filename}"
# Error code
code: 1
# Handle error instances
#
# Prints the message to stderr and aborts the program with the corresponding error code, or 0 if none.

View File

@ -73,6 +73,6 @@
"conf.js": "~0.1.1",
"coffee-script": "~1.8.0",
"git-cli": "~0.8.2",
"capitano": "~1.0.0"
"capitano": "~1.0.1"
}
}