2015-02-26 11:47:56 -04:00
( function ( ) {
2015-08-13 15:22:22 -04:00
var TOKEN _URL , _ , async , form , open , resin , settings , url , validEmail , visuals ;
2015-03-13 08:53:31 -04:00
open = require ( 'open' ) ;
2015-02-26 11:47:56 -04:00
2015-08-12 08:17:46 -04:00
_ = require ( 'lodash' ) ;
2015-02-26 11:47:56 -04:00
url = require ( 'url' ) ;
async = require ( 'async' ) ;
resin = require ( 'resin-sdk' ) ;
2015-04-08 08:25:27 -04:00
settings = require ( 'resin-settings-client' ) ;
2015-07-27 15:08:55 +03:00
form = require ( 'resin-cli-form' ) ;
2015-02-26 11:47:56 -04:00
2015-07-30 14:48:03 +03:00
visuals = require ( 'resin-cli-visuals' ) ;
2015-08-13 15:22:22 -04:00
validEmail = require ( 'valid-email' ) ;
2015-06-02 13:21:59 -04:00
TOKEN _URL = url . resolve ( settings . get ( 'dashboardUrl' ) , '/preferences' ) ;
2015-03-13 08:53:31 -04:00
2015-02-26 11:47:56 -04:00
exports . login = {
2015-03-11 14:37:24 -04:00
signature : 'login [token]' ,
2015-02-26 11:47:56 -04:00
description : 'login to resin.io' ,
2015-03-13 08:53:31 -04: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 11:47:56 -04:00
action : function ( params , options , done ) {
return async . waterfall ( [
function ( callback ) {
2015-05-11 09:42:25 -03:00
if ( params . token != null ) {
return callback ( null , params . token ) ;
}
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..." ) ;
2015-03-13 08:53:31 -04:00
return open ( TOKEN _URL , function ( error ) {
if ( error != null ) {
console . error ( "Unable to open a web browser in the current environment.\nPlease visit " + TOKEN _URL + " manually." ) ;
}
2015-07-27 15:08:55 +03:00
return form . ask ( {
message : 'What\'s your token? (visible in the preferences page)' ,
type : 'input'
} ) . nodeify ( callback ) ;
2015-03-13 08:53:31 -04:00
} ) ;
2015-03-11 14:37:24 -04:00
} , function ( token , callback ) {
2015-07-23 01:06:53 +03:00
return resin . auth . loginWithToken ( token ) . nodeify ( callback ) ;
2015-06-02 11:57:52 -04:00
} , function ( callback ) {
2015-07-23 01:06:53 +03:00
return resin . auth . whoami ( ) . nodeify ( callback ) ;
2015-06-02 11:57:52 -04:00
} , function ( username , callback ) {
console . info ( "Successfully logged in as: " + username ) ;
return callback ( ) ;
2015-02-26 11:47:56 -04:00
}
] , done ) ;
}
} ;
exports . logout = {
signature : 'logout' ,
description : 'logout from resin.io' ,
2015-03-03 10:14:16 -04:00
help : 'Use this command to logout from your resin.io account.o\n\nExamples:\n\n $ resin logout' ,
2015-02-26 11:47:56 -04:00
permission : 'user' ,
action : function ( params , options , done ) {
2015-07-23 01:06:53 +03:00
return resin . auth . logout ( ) . nodeify ( done ) ;
2015-02-26 11:47:56 -04:00
}
} ;
exports . signup = {
signature : 'signup' ,
description : 'signup to resin.io' ,
2015-03-03 10:14:16 -04: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 signup --email me@mycompany.com --username johndoe --password ***********\n\n $ resin whoami\n johndoe' ,
2015-02-26 11:47:56 -04:00
options : [
{
signature : 'email' ,
parameter : 'email' ,
description : 'user email' ,
alias : 'e'
} , {
signature : 'username' ,
parameter : 'username' ,
description : 'user name' ,
alias : 'u'
} , {
signature : 'password' ,
parameter : 'user password' ,
description : 'user password' ,
alias : 'p'
}
] ,
action : function ( params , options , done ) {
var hasOptionCredentials ;
hasOptionCredentials = ! _ . isEmpty ( options ) ;
if ( hasOptionCredentials ) {
if ( options . email == null ) {
return done ( new Error ( 'Missing email' ) ) ;
}
if ( options . username == null ) {
return done ( new Error ( 'Missing username' ) ) ;
}
if ( options . password == null ) {
return done ( new Error ( 'Missing password' ) ) ;
}
}
return async . waterfall ( [
function ( callback ) {
if ( hasOptionCredentials ) {
return callback ( null , options ) ;
}
2015-07-27 15:08:55 +03:00
return form . run ( [
{
message : 'Email:' ,
name : 'email' ,
2015-08-13 15:22:22 -04:00
type : 'input' ,
validate : function ( input ) {
if ( ! validEmail ( input ) ) {
return 'Email is not valid' ;
}
return true ;
}
2015-07-27 15:08:55 +03: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' ;
}
return true ;
}
}
] ) . nodeify ( callback ) ;
2015-02-26 11:47:56 -04:00
} , function ( credentials , callback ) {
2015-07-23 01:06:53 +03:00
return resin . auth . register ( credentials ) [ "return" ] ( credentials ) . nodeify ( callback ) ;
2015-02-26 11:47:56 -04:00
} , function ( credentials , callback ) {
2015-07-23 01:06:53 +03:00
return resin . auth . login ( credentials ) . nodeify ( callback ) ;
2015-02-26 11:47:56 -04:00
}
] , done ) ;
}
} ;
exports . whoami = {
signature : 'whoami' ,
description : 'get current username' ,
2015-03-03 10:14:16 -04:00
help : 'Use this command to find out the current logged in username.\n\nExamples:\n\n $ resin whoami' ,
2015-02-26 11:47:56 -04:00
permission : 'user' ,
action : function ( params , options , done ) {
2015-07-23 01:06:53 +03:00
return resin . auth . whoami ( ) . then ( function ( username ) {
2015-02-26 11:47:56 -04:00
if ( username == null ) {
2015-07-23 01:06:53 +03:00
throw new Error ( 'Username not found' ) ;
2015-02-26 11:47:56 -04:00
}
2015-07-29 20:46:37 +03:00
return resin . auth . getEmail ( ) . then ( function ( email ) {
2015-07-30 14:48:03 +03:00
return console . log ( visuals . table . vertical ( {
username : username ,
email : email
} , [ '$account information$' , 'username' , 'email' ] ) ) ;
2015-07-29 20:46:37 +03:00
} ) ;
2015-07-23 01:06:53 +03:00
} ) . nodeify ( done ) ;
2015-02-26 11:47:56 -04:00
}
} ;
} ) . call ( this ) ;