2015-02-26 15:47:56 +00:00
( function ( ) {
2015-03-19 15:45:07 +00:00
var SSH _KEY _WIDTH , _ , async , capitano , commandOptions , fs , resin , visuals ;
2015-02-26 15:47:56 +00:00
_ = require ( 'lodash' ) ;
_ . str = require ( 'underscore.string' ) ;
async = require ( 'async' ) ;
fs = require ( 'fs' ) ;
resin = require ( 'resin-sdk' ) ;
capitano = require ( 'capitano' ) ;
visuals = require ( 'resin-cli-visuals' ) ;
commandOptions = require ( './command-options' ) ;
exports . list = {
signature : 'keys' ,
description : 'list all ssh keys' ,
2015-03-03 14:14:16 +00:00
help : 'Use this command to list all your SSH keys.\n\nExamples:\n\n $ resin keys' ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
action : function ( params , options , done ) {
2015-07-22 22:06:53 +00:00
return resin . models . key . getAll ( ) . then ( function ( keys ) {
return console . log ( visuals . widgets . table . horizontal ( keys , [ 'id' , 'title' ] ) ) ;
} ) . nodeify ( done ) ;
2015-02-26 15:47:56 +00:00
}
} ;
2015-03-19 15:45:07 +00:00
SSH _KEY _WIDTH = 43 ;
2015-02-26 15:47:56 +00:00
exports . info = {
signature : 'key <id>' ,
description : 'list a single ssh key' ,
2015-03-03 14:14:16 +00:00
help : 'Use this command to show information about a single SSH key.\n\nExamples:\n\n $ resin key 17' ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
action : function ( params , options , done ) {
2015-07-22 22:06:53 +00:00
return resin . models . key . get ( params . id ) . then ( function ( key ) {
return console . log ( visuals . widgets . table . vertical ( key , [ 'id' , 'title' , 'public_key' ] ) ) ;
} ) . nodeify ( done ) ;
2015-02-26 15:47:56 +00:00
}
} ;
exports . remove = {
signature : 'key rm <id>' ,
description : 'remove a ssh key' ,
2015-03-03 14:14:16 +00:00
help : 'Use this command to remove a SSH key from resin.io.\n\nNotice this command asks for confirmation interactively.\nYou can avoid this by passing the `--yes` boolean option.\n\nExamples:\n\n $ resin key rm 17\n $ resin key rm 17 --yes' ,
2015-02-26 15:47:56 +00:00
options : [ commandOptions . yes ] ,
permission : 'user' ,
action : function ( params , options , done ) {
return visuals . patterns . remove ( 'key' , options . yes , function ( callback ) {
2015-07-22 22:06:53 +00:00
return resin . models . key . remove ( params . id ) . nodeify ( callback ) ;
2015-02-26 15:47:56 +00:00
} , done ) ;
}
} ;
exports . add = {
signature : 'key add <name> [path]' ,
description : 'add a SSH key to resin.io' ,
2015-03-03 14:14:16 +00:00
help : 'Use this command to associate a new SSH key with your account.\n\nIf `path` is omitted, the command will attempt\nto read the SSH key from stdin.\n\nExamples:\n\n $ resin key add Main ~/.ssh/id_rsa.pub\n $ cat ~/.ssh/id_rsa.pub | resin key add Main' ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
action : function ( params , options , done ) {
return async . waterfall ( [
function ( callback ) {
if ( params . path != null ) {
return fs . readFile ( params . path , {
encoding : 'utf8'
} , callback ) ;
} else {
return capitano . utils . getStdin ( function ( data ) {
return callback ( null , data ) ;
} ) ;
}
} , function ( key , callback ) {
2015-07-22 22:06:53 +00:00
return resin . models . key . create ( params . name , key ) . nodeify ( callback ) ;
2015-02-26 15:47:56 +00:00
}
] , done ) ;
}
} ;
} ) . call ( this ) ;