2015-02-26 15:47:56 +00:00
( function ( ) {
2015-08-31 21:39:48 +00:00
var Promise , TOKEN _URL , _ , events , form , open , resin , settings , url , validEmail , visuals ;
2015-03-13 12:53:31 +00:00
2015-08-13 18:50:36 +00:00
Promise = require ( 'bluebird' ) ;
open = Promise . promisify ( require ( 'open' ) ) ;
2015-02-26 15:47:56 +00:00
2015-08-12 12:17:46 +00:00
_ = require ( 'lodash' ) ;
2015-02-26 15:47:56 +00:00
url = require ( 'url' ) ;
resin = require ( 'resin-sdk' ) ;
2015-04-08 12:25:27 +00:00
settings = require ( 'resin-settings-client' ) ;
2015-07-27 12:08:55 +00:00
form = require ( 'resin-cli-form' ) ;
2015-02-26 15:47:56 +00:00
2015-07-30 11:48:03 +00:00
visuals = require ( 'resin-cli-visuals' ) ;
2015-08-13 19:22:22 +00:00
validEmail = require ( 'valid-email' ) ;
2015-08-31 21:39:48 +00:00
events = require ( 'resin-cli-events' ) ;
2015-06-02 17:21:59 +00:00
TOKEN _URL = url . resolve ( settings . get ( 'dashboardUrl' ) , '/preferences' ) ;
2015-03-13 12:53:31 +00:00
2015-02-26 15:47:56 +00:00
exports . login = {
2015-03-11 18:37:24 +00:00
signature : 'login [token]' ,
2015-02-26 15:47:56 +00:00
description : 'login to resin.io' ,
2015-03-13 12:53:31 +00:00
help : "Use this command to login to your resin.io account.\n\nTo login, you need your token, which is accesible from the preferences page:\n\n " + TOKEN _URL + "\n\nExamples:\n\n $ resin login\n $ resin login \"eyJ0eXAiOiJKV1Qi...\"" ,
2015-02-26 15:47:56 +00:00
action : function ( params , options , done ) {
2015-08-13 18:50:36 +00:00
return Promise [ "try" ] ( function ( ) {
if ( params . token != null ) {
return params . token ;
2015-02-26 15:47:56 +00:00
}
2015-08-13 18:50:36 +00:00
console . info ( "To login to the Resin CLI, you need your unique token, which is accesible from\nthe preferences page at " + TOKEN _URL + "\n\nAttempting to open a browser at that location..." ) ;
return open ( TOKEN _URL ) [ "catch" ] ( function ( ) {
return console . error ( "Unable to open a web browser in the current environment.\nPlease visit " + TOKEN _URL + " manually." ) ;
} ) . then ( function ( ) {
return form . ask ( {
message : 'What\'s your token? (visible in the preferences page)' ,
type : 'input'
} ) ;
} ) ;
} ) . then ( resin . auth . loginWithToken ) . then ( resin . auth . whoami ) . tap ( function ( username ) {
2015-08-31 21:39:48 +00:00
console . info ( "Successfully logged in as: " + username ) ;
return events . send ( 'user.login' ) ;
2015-08-13 18:50:36 +00:00
} ) . nodeify ( done ) ;
2015-02-26 15:47:56 +00:00
}
} ;
exports . logout = {
signature : 'logout' ,
description : 'logout from resin.io' ,
2015-03-03 14:14:16 +00:00
help : 'Use this command to logout from your resin.io account.o\n\nExamples:\n\n $ resin logout' ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
action : function ( params , options , done ) {
2015-08-31 21:39:48 +00:00
return resin . auth . logout ( ) . then ( function ( ) {
return events . send ( 'user.logout' ) ;
} ) . nodeify ( done ) ;
2015-02-26 15:47:56 +00:00
}
} ;
exports . signup = {
signature : 'signup' ,
description : 'signup to resin.io' ,
2015-08-13 18:50:36 +00:00
help : 'Use this command to signup for a resin.io account.\n\nIf signup is successful, you\'ll be logged in to your new user automatically.\n\nExamples:\n\n $ resin signup\n Email: me@mycompany.com\n Username: johndoe\n Password: ***********\n\n $ resin whoami\n johndoe' ,
2015-02-26 15:47:56 +00:00
action : function ( params , options , done ) {
2015-08-13 18:50:36 +00:00
return form . run ( [
{
message : 'Email:' ,
name : 'email' ,
type : 'input' ,
validate : function ( input ) {
if ( ! validEmail ( input ) ) {
return 'Email is not valid' ;
}
return true ;
2015-02-26 15:47:56 +00:00
}
2015-08-13 18:50:36 +00:00
} , {
message : 'Username:' ,
name : 'username' ,
type : 'input'
} , {
message : 'Password:' ,
name : 'password' ,
type : 'password' ,
validate : function ( input ) {
if ( input . length < 8 ) {
return 'Password should be 8 characters long' ;
2015-07-27 12:08:55 +00:00
}
2015-08-13 18:50:36 +00:00
return true ;
}
2015-02-26 15:47:56 +00:00
}
2015-08-31 21:39:48 +00:00
] ) . then ( resin . auth . register ) . then ( resin . auth . loginWithToken ) . tap ( function ( ) {
return events . send ( 'user.signup' ) ;
} ) . nodeify ( done ) ;
2015-02-26 15:47:56 +00:00
}
} ;
exports . whoami = {
signature : 'whoami' ,
2015-08-13 18:50:36 +00:00
description : 'get current username and email address' ,
help : 'Use this command to find out the current logged in username and email address.\n\nExamples:\n\n $ resin whoami' ,
2015-02-26 15:47:56 +00:00
permission : 'user' ,
action : function ( params , options , done ) {
2015-08-13 18:50:36 +00:00
return Promise . props ( {
username : resin . auth . whoami ( ) ,
email : resin . auth . getEmail ( )
} ) . then ( function ( results ) {
return console . log ( visuals . table . vertical ( results , [ '$account information$' , 'username' , 'email' ] ) ) ;
2015-07-22 22:06:53 +00:00
} ) . nodeify ( done ) ;
2015-02-26 15:47:56 +00:00
}
} ;
} ) . call ( this ) ;